jellyfin-kodi/contextmenu.py

209 lines
6.3 KiB
Python
Raw Normal View History

2016-04-04 21:21:05 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
2016-07-24 21:12:19 +00:00
import logging
2016-04-04 21:21:05 +00:00
import os
import sys
import xbmc
import xbmcaddon
2016-06-19 18:30:54 +00:00
#################################################################################################
2016-09-09 03:13:25 +00:00
_ADDON = xbmcaddon.Addon(id='plugin.video.emby')
_CWD = _ADDON.getAddonInfo('path').decode('utf-8')
_BASE_LIB = xbmc.translatePath(os.path.join(_CWD, 'resources', 'lib')).decode('utf-8')
sys.path.append(_BASE_LIB)
2016-06-19 18:30:54 +00:00
#################################################################################################
2016-04-04 21:21:05 +00:00
2016-06-20 00:24:42 +00:00
import api
2016-09-09 03:13:25 +00:00
import loghandler
2016-04-04 21:21:05 +00:00
import read_embyserver as embyserver
import embydb_functions as embydb
import musicutils as musicutils
2016-09-09 03:13:25 +00:00
from utils import settings, dialog, language as lang, kodiSQL
2016-07-24 21:08:53 +00:00
#################################################################################################
loghandler.config()
log = logging.getLogger("EMBY.contextmenu")
2016-04-04 21:21:05 +00:00
2016-06-20 00:24:42 +00:00
#################################################################################################
2016-04-04 21:21:05 +00:00
2016-09-09 03:13:25 +00:00
OPTIONS = {
2016-06-20 00:24:42 +00:00
2016-09-09 03:13:25 +00:00
'Refresh': lang(30410),
'Delete': lang(30409),
'Addon': lang(30408),
'AddFav': lang(30405),
'RemoveFav': lang(30406),
'RateSong': lang(30407)
}
class ContextMenu(object):
_selected_option = None
def __init__(self):
self.emby = embyserver.Read_EmbyServer()
self.kodi_id = xbmc.getInfoLabel('ListItem.DBID').decode('utf-8')
self.item_type = self._get_item_type()
self.item_id = self._get_item_id(self.kodi_id, self.item_type)
2016-06-20 00:24:42 +00:00
2016-09-09 03:13:25 +00:00
log.info("Found item_id: %s item_type: %s", self.item_id, self.item_type)
2016-04-04 21:21:05 +00:00
2016-09-09 03:13:25 +00:00
if self.item_id:
2016-09-09 03:13:25 +00:00
self.item = self.emby.getItem(self.item_id)
self.api = api.API(self.item)
if self._select_menu():
self._action_menu()
xbmc.sleep(500)
xbmc.executebuiltin('Container.Refresh')
@classmethod
def _get_item_type(cls):
item_type = xbmc.getInfoLabel('ListItem.DBTYPE').decode('utf-8')
if not item_type:
if xbmc.getCondVisibility('Container.Content(albums)'):
item_type = "album"
elif xbmc.getCondVisibility('Container.Content(artists)'):
item_type = "artist"
elif xbmc.getCondVisibility('Container.Content(songs)'):
item_type = "song"
elif xbmc.getCondVisibility('Container.Content(pictures)'):
item_type = "picture"
else:
log.info("item_type is unknown")
return item_type
@classmethod
def _get_item_id(cls, kodi_id, item_type):
item_id = xbmc.getInfoLabel('ListItem.Property(embyid)')
if not item_id and kodi_id and item_type:
conn = kodiSQL('emby')
cursor = conn.cursor()
emby_db = embydb.Embydb_Functions(cursor)
item = emby_db.getItem_byKodiId(kodi_id, item_type)
cursor.close()
try:
item_id = item[0]
except TypeError:
pass
return item_id
def _select_menu(self):
# Display select dialog
userdata = self.api.getUserData()
2016-06-20 00:24:42 +00:00
options = []
2016-09-09 03:13:25 +00:00
if userdata['Favorite']:
2016-06-20 00:24:42 +00:00
# Remove from emby favourites
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['RemoveFav'])
2016-06-20 00:24:42 +00:00
else:
# Add to emby favourites
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['AddFav'])
2016-06-20 00:24:42 +00:00
2016-09-09 03:13:25 +00:00
if self.item_type == "song":
2016-06-20 00:24:42 +00:00
# Set custom song rating
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['RateSong'])
2016-06-20 00:24:42 +00:00
# Refresh item
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['Refresh'])
2016-06-20 00:24:42 +00:00
# Delete item
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['Delete'])
2016-06-20 00:24:42 +00:00
# Addon settings
2016-09-09 03:13:25 +00:00
options.append(OPTIONS['Addon'])
resp = dialog(type_="select", heading=lang(30401), list=options)
if resp > -1:
2016-09-09 03:13:25 +00:00
self._selected_option = options[resp]
return self._selected_option
def _action_menu(self):
selected = self._selected_option
if selected == OPTIONS['Refresh']:
self.emby.refreshItem(self.item_id)
elif selected == OPTIONS['AddFav']:
self.emby.updateUserRating(self.item_id, favourite=True)
elif selected == OPTIONS['RemoveFav']:
self.emby.updateUserRating(self.item_id, favourite=False)
elif selected == OPTIONS['RateSong']:
self._rate_song()
elif selected == OPTIONS['Addon']:
xbmc.executebuiltin('Addon.OpenSettings(plugin.video.emby)')
elif selected == OPTIONS['Delete']:
self._delete_item()
def _rate_song(self):
conn = kodiSQL('music')
cursor = conn.cursor()
query = "SELECT rating FROM song WHERE idSong = ?"
cursor.execute(query, (self.kodi_id,))
try:
value = cursor.fetchone()[0]
current_value = int(round(float(value), 0))
except TypeError:
pass
else:
new_value = dialog("numeric", 0, lang(30411), str(current_value))
if new_value > -1:
new_value = int(new_value)
if new_value > 5:
new_value = 5
if settings('enableUpdateSongRating') == "true":
musicutils.updateRatingToFile(new_value, self.api.getFilePath())
query = "UPDATE song SET rating = ? WHERE idSong = ?"
cursor.execute(query, (new_value, self.kodi_id,))
conn.commit()
finally:
cursor.close()
def _delete_item(self):
delete = True
if settings('skipContextMenu') != "true":
if not dialog(type_="yesno", heading="{emby}", line1=lang(33041)):
log.info("User skipped deletion for: %s", self.item_id)
delete = False
if delete:
log.info("Deleting request: %s", self.item_id)
self.emby.deleteItem(self.item_id)
# Kodi contextmenu item to configure the emby settings
if __name__ == '__main__':
log.info("plugin.video.emby contextmenu started")
ContextMenu()
log.info("plugin.video.emby contextmenu stopped")