From b20308d68f3a7e1119120822b76e198eb2be7984 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Tue, 22 Mar 2016 16:17:06 +0100 Subject: [PATCH] Playback updates if an item is resumed --- resources/lib/kodidb_functions.py | 82 +++++++++++++++++++++++++++++++ resources/lib/kodimonitor.py | 17 +++++-- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index 34021952..6bb37d8a 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -755,6 +755,88 @@ class Kodidb_Functions(): ids.append(row[0]) return ids + def getIdFromTitle(self, itemdetails): + """ + Returns the Kodi id (e.g. idMovie, idEpisode) from the item's + title (c00), if there is exactly ONE found for the itemtype. + (False otherwise) + + itemdetails is the data['item'] response from Kodi + + itemdetails for movies: + { + "title":"Kung Fu Panda", + "type":"movie", + "year":2008 + } + + itemdetails for episodes: + { + "episode":5 + "season":5, + "showtitle":"Girls", + "title":"Queen for Two Days", + "type":"episode" + } + """ + try: + type = itemdetails['type'] + except: + return False + + if type == 'movie': + query = ' '.join(( + "SELECT idMovie", + "FROM movie", + "WHERE c00 = ?" + )) + try: + rows = self.cursor.execute(query, (itemdetails['title'],)) + except: + return False + elif type == 'episode': + query = ' '.join(( + "SELECT idShow", + "FROM tvshow", + "WHERE c00 = ?" + )) + try: + rows = self.cursor.execute(query, (itemdetails['showtitle'],)) + except: + return False + ids = [] + for row in rows: + ids.append(row[0]) + if len(ids) > 1: + # No unique match possible + return False + showid = ids[0] + + query = ' '.join(( + "SELECT idEpisode", + "FROM episode", + "WHERE c12 = ? AND c13 = ? AND idShow = ?" + )) + try: + rows = self.cursor.execute( + query, + (itemdetails['season'], + itemdetails['episode'], + showid)) + except: + return False + else: + return False + + ids = [] + for row in rows: + ids.append(row[0]) + if len(ids) > 1: + # No unique match possible + return False + else: + return ids[0] + def getUnplayedItems(self): """ VIDEOS diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index d16bb26a..502d86d4 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -10,6 +10,7 @@ import xbmcgui import downloadutils import embydb_functions as embydb +import kodidb_functions as kodidb import playbackutils as pbutils import utils from PlexFunctions import scrobble @@ -154,11 +155,21 @@ class KodiMonitor(xbmc.Monitor): # Try to get a Kodi ID item = data.get('item') try: - kodiid = item['id'] type = item['type'] - except (KeyError, TypeError): - log("Item is invalid for Plex playstate update.", 0) + except: + log("Item is invalid for PMS playstate update.", 0) return + try: + kodiid = item['id'] + except (KeyError, TypeError): + log('Kodi did not give us a Kodi item id, trying to get from item ' + 'title', 0) + # Try to get itemid with the element's title + with kodidb.GetKodiDB('video') as kodi_db: + kodiid = kodi_db.getIdFromTitle(item) + if kodiid is False: + log("Item is invalid for PMS playstate update.", 0) + return # Get Plex' item id with embydb.GetEmbyDB() as emby_db: