jellyfin-kodi/contextmenu.py

168 lines
6.1 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
2016-06-20 00:24:42 +00:00
import api
2016-04-04 21:21:05 +00:00
import artwork
import downloadutils
import librarysync
import read_embyserver as embyserver
import embydb_functions as embydb
import kodidb_functions as kodidb
import musicutils as musicutils
2016-06-20 00:24:42 +00:00
from utils import Logging, settings, language as lang, kodiSQL
2016-06-19 18:30:54 +00:00
log = Logging('ContextMenu').log
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-06-20 00:24:42 +00:00
# Kodi contextmenu item to configure the emby settings
2016-04-04 21:21:05 +00:00
if __name__ == '__main__':
2016-06-20 00:24:42 +00:00
kodiId = xbmc.getInfoLabel('ListItem.DBID').decode('utf-8')
itemType = xbmc.getInfoLabel('ListItem.DBTYPE').decode('utf-8')
itemId = ""
2016-04-04 21:21:05 +00:00
2016-06-20 00:24:42 +00:00
if not itemType:
if xbmc.getCondVisibility("Container.Content(albums)"):
itemType = "album"
elif xbmc.getCondVisibility("Container.Content(artists)"):
itemType = "artist"
elif xbmc.getCondVisibility("Container.Content(songs)"):
itemType = "song"
elif xbmc.getCondVisibility("Container.Content(pictures)"):
itemType = "picture"
else:
log("ItemType is unknown.")
2016-06-20 00:24:42 +00:00
if (not kodiId or kodiId == "-1") and xbmc.getInfoLabel("ListItem.Property(embyid)"):
itemId = xbmc.getInfoLabel("ListItem.Property(embyid)")
2016-04-04 21:21:05 +00:00
2016-06-20 00:24:42 +00:00
elif kodiId and itemType:
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)
2016-06-20 00:24:42 +00:00
item = emby_db.getItem_byKodiId(kodiId, itemType)
2016-04-04 21:21:05 +00:00
embycursor.close()
2016-06-20 00:24:42 +00:00
try:
itemId = item[0]
except TypeError:
pass
2016-04-04 21:21:05 +00:00
log("Found ItemId: %s ItemType: %s" % (itemId, itemType), 1)
2016-06-20 00:24:42 +00:00
if itemId:
2016-04-04 21:21:05 +00:00
dialog = xbmcgui.Dialog()
2016-06-20 00:24:42 +00:00
emby = embyserver.Read_EmbyServer()
item = emby.getItem(itemId)
2016-04-04 21:21:05 +00:00
API = api.API(item)
userdata = API.getUserData()
likes = userdata['Likes']
favourite = userdata['Favorite']
2016-06-20 00:24:42 +00:00
options = []
if favourite:
# Remove from emby favourites
2016-06-19 18:30:54 +00:00
options.append(lang(30406))
2016-06-20 00:24:42 +00:00
else:
# Add to emby favourites
options.append(lang(30405))
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
2016-06-20 00:24:42 +00:00
# Refresh item
options.append(lang(30410))
# Delete item
2016-06-19 18:30:54 +00:00
options.append(lang(30409))
2016-06-20 00:24:42 +00:00
# Addon settings
2016-06-19 18:30:54 +00:00
options.append(lang(30408))
2016-04-04 21:21:05 +00:00
2016-06-20 00:24:42 +00:00
# Display select dialog and process results
resp = xbmcgui.Dialog().select(lang(30401), options)
if resp > -1:
selected = options[resp]
2016-06-20 00:24:42 +00:00
if selected == lang(30410):
# Refresh item
2016-06-20 00:24:42 +00:00
emby.refreshItem(itemId)
elif selected == lang(30405):
# Add favourite
2016-06-20 00:24:42 +00:00
emby.updateUserRating(itemId, favourite=True)
elif selected == lang(30406):
# Delete favourite
2016-06-20 00:24:42 +00:00
emby.updateUserRating(itemId, favourite=False)
elif selected == lang(30407):
# Update song rating
2016-06-19 18:30:54 +00:00
kodiconn = kodiSQL('music')
2016-04-04 21:21:05 +00:00
kodicursor = kodiconn.cursor()
query = "SELECT rating FROM song WHERE idSong = ?"
kodicursor.execute(query, (kodiId,))
try:
value = kodicursor.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, API.getFilePath())
query = "UPDATE song SET rating = ? WHERE idSong = ?"
kodicursor.execute(query, (new_value, kodiId,))
kodiconn.commit()
'''if settings('enableExportSongRating') == "true":
like, favourite, deletelike = musicutils.getEmbyRatingFromKodiRating(new_value)
emby.updateUserRating(itemId, like, favourite, deletelike)'''
finally:
kodicursor.close()
elif selected == lang(30408):
# Open addon settings
2016-04-04 21:21:05 +00:00
xbmc.executebuiltin("Addon.OpenSettings(plugin.video.emby)")
elif selected == lang(30409):
# delete item from the server
2016-04-04 21:21:05 +00:00
delete = True
2016-06-19 18:30:54 +00:00
if settings('skipContextMenu') != "true":
resp = dialog.yesno(
heading=lang(29999),
line1=lang(33041))
2016-04-04 21:21:05 +00:00
if not resp:
2016-06-20 00:24:42 +00:00
log("User skipped deletion for: %s." % itemId, 1)
2016-04-04 21:21:05 +00:00
delete = False
if delete:
log("Deleting request: %s" % itemId, 0)
emby.deleteItem(itemId)
2016-04-04 21:21:05 +00:00
xbmc.sleep(500)
xbmc.executebuiltin('Container.Refresh')