jellyfin-kodi/contextmenu.py

163 lines
7 KiB
Python
Raw Normal View History

2016-04-04 21:21:05 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
import os
import sys
import urlparse
import xbmc
import xbmcaddon
import xbmcgui
2016-06-19 18:30:54 +00:00
#################################################################################################
_addon = xbmcaddon.Addon(id='plugin.video.emby')
_addon_path = _addon.getAddonInfo('path').decode('utf-8')
_base_resource = xbmc.translatePath(os.path.join(_addon_path, 'resources', 'lib')).decode('utf-8')
sys.path.append(_base_resource)
#################################################################################################
2016-04-04 21:21:05 +00:00
import artwork
import clientinfo
import downloadutils
import librarysync
import read_embyserver as embyserver
import embydb_functions as embydb
import kodidb_functions as kodidb
import musicutils as musicutils
import api
2016-06-19 18:30:54 +00:00
from utils import Logging, settings language as lang, kodiSQL
#################################################################################################
2016-04-04 21:21:05 +00:00
2016-06-19 18:30:54 +00:00
log = Logging('ContextMenu').log
2016-04-04 21:21:05 +00:00
#Kodi contextmenu item to configure the emby settings
#for now used to set ratings but can later be used to sync individual items etc.
if __name__ == '__main__':
itemid = xbmc.getInfoLabel("ListItem.DBID").decode("utf-8")
itemtype = xbmc.getInfoLabel("ListItem.DBTYPE").decode("utf-8")
emby = embyserver.Read_EmbyServer()
embyid = ""
if not itemtype and xbmc.getCondVisibility("Container.Content(albums)"): itemtype = "album"
if not itemtype and xbmc.getCondVisibility("Container.Content(artists)"): itemtype = "artist"
if not itemtype and xbmc.getCondVisibility("Container.Content(songs)"): itemtype = "song"
if not itemtype and xbmc.getCondVisibility("Container.Content(pictures)"): itemtype = "picture"
if (not itemid or itemid == "-1") and xbmc.getInfoLabel("ListItem.Property(embyid)"):
embyid = xbmc.getInfoLabel("ListItem.Property(embyid)")
else:
2016-06-19 18:30:54 +00:00
embyconn = kodiSQL('emby')
2016-04-04 21:21:05 +00:00
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
item = emby_db.getItem_byKodiId(itemid, itemtype)
embycursor.close()
if item: embyid = item[0]
2016-06-19 18:30:54 +00:00
log("Contextmenu opened for embyid: %s - itemtype: %s" %(embyid,itemtype))
2016-04-04 21:21:05 +00:00
if embyid:
item = emby.getItem(embyid)
API = api.API(item)
userdata = API.getUserData()
likes = userdata['Likes']
favourite = userdata['Favorite']
options=[]
if likes == True:
#clear like for the item
2016-06-19 18:30:54 +00:00
options.append(lang(30402))
2016-04-04 21:21:05 +00:00
if likes == False or likes == None:
#Like the item
2016-06-19 18:30:54 +00:00
options.append(lang(30403))
2016-04-04 21:21:05 +00:00
if likes == True or likes == None:
#Dislike the item
2016-06-19 18:30:54 +00:00
options.append(lang(30404))
2016-04-04 21:21:05 +00:00
if favourite == False:
#Add to emby favourites
2016-06-19 18:30:54 +00:00
options.append(lang(30405))
2016-04-04 21:21:05 +00:00
if favourite == True:
#Remove from emby favourites
2016-06-19 18:30:54 +00:00
options.append(lang(30406))
2016-04-04 21:21:05 +00:00
if itemtype == "song":
#Set custom song rating
2016-06-19 18:30:54 +00:00
options.append(lang(30407))
2016-04-04 21:21:05 +00:00
#delete item
2016-06-19 18:30:54 +00:00
options.append(lang(30409))
2016-04-04 21:21:05 +00:00
#addon settings
2016-06-19 18:30:54 +00:00
options.append(lang(30408))
2016-04-04 21:21:05 +00:00
#display select dialog and process results
2016-06-19 18:30:54 +00:00
header = lang(30401)
2016-04-04 21:21:05 +00:00
ret = xbmcgui.Dialog().select(header, options)
if ret != -1:
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30402):
2016-04-04 21:21:05 +00:00
emby.updateUserRating(embyid, deletelike=True)
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30403):
2016-04-04 21:21:05 +00:00
emby.updateUserRating(embyid, like=True)
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30404):
2016-04-04 21:21:05 +00:00
emby.updateUserRating(embyid, like=False)
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30405):
2016-04-04 21:21:05 +00:00
emby.updateUserRating(embyid, favourite=True)
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30406):
2016-04-04 21:21:05 +00:00
emby.updateUserRating(embyid, favourite=False)
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30407):
kodiconn = kodiSQL('music')
2016-04-04 21:21:05 +00:00
kodicursor = kodiconn.cursor()
query = ' '.join(("SELECT rating", "FROM song", "WHERE idSong = ?" ))
kodicursor.execute(query, (itemid,))
currentvalue = int(round(float(kodicursor.fetchone()[0]),0))
newvalue = xbmcgui.Dialog().numeric(0, "Set custom song rating (0-5)", str(currentvalue))
if newvalue:
newvalue = int(newvalue)
if newvalue > 5: newvalue = "5"
2016-06-19 18:30:54 +00:00
if settings('enableUpdateSongRating') == "true":
2016-04-04 21:21:05 +00:00
musicutils.updateRatingToFile(newvalue, API.getFilePath())
2016-06-19 18:30:54 +00:00
if settings('enableExportSongRating') == "true":
2016-04-04 21:21:05 +00:00
like, favourite, deletelike = musicutils.getEmbyRatingFromKodiRating(newvalue)
emby.updateUserRating(embyid, like, favourite, deletelike)
query = ' '.join(( "UPDATE song","SET rating = ?", "WHERE idSong = ?" ))
kodicursor.execute(query, (newvalue,itemid,))
kodiconn.commit()
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30408):
2016-04-04 21:21:05 +00:00
#Open addon settings
xbmc.executebuiltin("Addon.OpenSettings(plugin.video.emby)")
2016-06-19 18:30:54 +00:00
if options[ret] == lang(30409):
2016-04-04 21:21:05 +00:00
#delete item from the server
delete = True
2016-06-19 18:30:54 +00:00
if settings('skipContextMenu') != "true":
2016-04-04 21:21:05 +00:00
resp = xbmcgui.Dialog().yesno(
heading="Confirm delete",
line1=("Delete file from Emby Server? This will "
"also delete the file(s) from disk!"))
if not resp:
2016-06-19 18:30:54 +00:00
log("User skipped deletion for: %s." % embyid, 1)
2016-04-04 21:21:05 +00:00
delete = False
if delete:
import downloadutils
doUtils = downloadutils.DownloadUtils()
url = "{server}/emby/Items/%s?format=json" % embyid
2016-06-19 18:30:54 +00:00
log("Deleting request: %s" % embyid, 0)
2016-04-04 21:21:05 +00:00
doUtils.downloadUrl(url, action_type="DELETE")
2016-06-19 18:30:54 +00:00
'''if settings('skipContextMenu') != "true":
2016-04-04 21:21:05 +00:00
if xbmcgui.Dialog().yesno(
heading="Confirm delete",
line1=("Delete file on Emby Server? This will "
"also delete the file(s) from disk!")):
import downloadutils
doUtils = downloadutils.DownloadUtils()
doUtils.downloadUrl("{server}/emby/Items/%s?format=json" % embyid, action_type="DELETE")'''
xbmc.sleep(500)
xbmc.executebuiltin("Container.Update")