diff --git a/addon.xml b/addon.xml index 4672d746..77240714 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/resources/lib/LibraryMonitor.py b/resources/lib/LibraryMonitor.py new file mode 100644 index 00000000..01a8bc58 --- /dev/null +++ b/resources/lib/LibraryMonitor.py @@ -0,0 +1,89 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from traceback import print_exc +import xbmc +import xbmcgui +import threading +import Utils as utils +from ReadKodiDB import ReadKodiDB +from ClientInformation import ClientInformation + +class LibraryMonitor(threading.Thread): + + event = None + exit = False + liPath = None + liPathLast = None + WINDOW = xbmcgui.Window(10000) + + clientInfo = ClientInformation() + addonName = clientInfo.getAddonName() + + def __init__(self, *args): + + self.event = threading.Event() + threading.Thread.__init__(self, *args) + + def logMsg(self, msg, lvl=1): + + className = self.__class__.__name__ + utils.logMsg("%s %s" % (self.addonName, className), msg, int(lvl)) + + def stop(self): + self.logMsg("LibraryMonitor - stop called",0) + self.exit = True + self.event.set() + + def run(self): + self.logMsg("LIBRARY MONITOR running ") + WINDOW = self.WINDOW + lastListItemLabel = None + + while (self.exit != True): + + # monitor listitem props when videolibrary is active + if (xbmc.getCondVisibility("[Window.IsActive(videolibrary) | Window.IsActive(movieinformation)] + !Window.IsActive(fullscreenvideo)")): + + self.liPath = xbmc.getInfoLabel("ListItem.Path") + liLabel = xbmc.getInfoLabel("ListItem.Label") + if ((liLabel != lastListItemLabel) and xbmc.getCondVisibility("!Container.Scrolling")): + + self.liPathLast = self.liPath + lastListItemLabel = liLabel + + # update the listitem stuff + try: + self.setRatingsInfo() + except Exception as e: + self.logMsg("ERROR in LibraryMonitor ! --> " + str(e), 0) + + else: + #reset window props + WINDOW.clearProperty("EmbySkinHelper.ListItemRottenTomatoes") + WINDOW.clearProperty('EmbySkinHelper.ListItemRottenTomatoesSummary') + WINDOW.clearProperty('EmbySkinHelper.ListItemMetaScore') + + xbmc.sleep(150) + + def setRatingsInfo(self): + WINDOW = self.WINDOW + + embyId = self.liPath.split("/")[-2] + criticrating = ReadKodiDB().getCriticRatingByEmbyId(embyId) + if criticrating: + WINDOW.setProperty('EmbySkinHelper.ListItemRottenTomatoes', criticrating) + else: + WINDOW.clearProperty('EmbySkinHelper.ListItemRottenTomatoes') + + criticratingsummary = ReadKodiDB().getCriticRatingSummaryByEmbyId(embyId) + if criticratingsummary: + WINDOW.setProperty('EmbySkinHelper.ListItemRottenTomatoesSummary', criticratingsummary) + else: + WINDOW.clearProperty('EmbySkinHelper.ListItemRottenTomatoesSummary') + + metascore = ReadKodiDB().getMetaScoreRatingByEmbyId(embyId) + if metascore: + WINDOW.setProperty('EmbySkinHelper.ListItemMetaScore', metascore) + else: + WINDOW.clearProperty('EmbySkinHelper.ListItemMetaScore') diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 78938d54..a9401409 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -137,9 +137,12 @@ class LibrarySync(threading.Thread): cursor = connection.cursor() #Add the special emby table - cursor.execute("CREATE TABLE IF NOT EXISTS emby(emby_id TEXT, kodi_id INTEGER, media_type TEXT, checksum TEXT, parent_id INTEGER, kodi_file_id INTEGER)") + cursor.execute("CREATE TABLE IF NOT EXISTS emby(emby_id TEXT, kodi_id INTEGER, media_type TEXT, checksum TEXT, parent_id INTEGER, kodi_file_id INTEGER, rotten_tomatoes TEXT, rotten_tomatoes_summary TEXT, metascore TEXT)") try: cursor.execute("ALTER TABLE emby ADD COLUMN kodi_file_id INTEGER") + cursor.execute("ALTER TABLE emby ADD COLUMN rotten_tomatoes TEXT") + cursor.execute("ALTER TABLE emby ADD COLUMN rotten_tomatoes_summary TEXT") + cursor.execute("ALTER TABLE emby ADD COLUMN metascore TEXT") except: pass connection.commit() diff --git a/resources/lib/ReadEmbyDB.py b/resources/lib/ReadEmbyDB.py index 6bb9a6c0..eba73d13 100644 --- a/resources/lib/ReadEmbyDB.py +++ b/resources/lib/ReadEmbyDB.py @@ -269,7 +269,7 @@ class ReadEmbyDB(): def getFullItem(self, id): result = {} - url = "{server}/mediabrowser/Users/{UserId}/Items/%s?format=json&Fields=Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview" % id + url = "{server}/mediabrowser/Users/{UserId}/Items/%s?format=json&Fields=Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview,CriticRating,CriticRatingSummary" % id jsondata = self.doUtils.downloadUrl(url) if jsondata: diff --git a/resources/lib/ReadKodiDB.py b/resources/lib/ReadKodiDB.py index 5a4e9aff..9e96e117 100644 --- a/resources/lib/ReadKodiDB.py +++ b/resources/lib/ReadKodiDB.py @@ -129,4 +129,52 @@ class ReadKodiDB(): cursor.execute("SELECT kodi_id, emby_id, checksum FROM emby WHERE media_type='song'") allsongs = cursor.fetchall() #this will return a list with tuples of all items returned from the database - return allsongs \ No newline at end of file + return allsongs + + def getCriticRatingByEmbyId(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + cursor.execute("SELECT rotten_tomatoes FROM emby WHERE emby_id=?",(id,)) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None + + def getCriticRatingSummaryByEmbyId(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + cursor.execute("SELECT rotten_tomatoes_summary FROM emby WHERE emby_id=?",(id,)) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None + + def getMetaScoreRatingByEmbyId(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + cursor.execute("SELECT metascore FROM emby WHERE emby_id=?",(id,)) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None \ No newline at end of file diff --git a/resources/lib/WriteKodiVideoDB.py b/resources/lib/WriteKodiVideoDB.py index e717e5bf..5a1a3e46 100644 --- a/resources/lib/WriteKodiVideoDB.py +++ b/resources/lib/WriteKodiVideoDB.py @@ -107,6 +107,9 @@ class WriteKodiVideoDB(): tagline = API().getTagline(MBitem) votecount = MBitem.get('VoteCount') rating = MBitem.get('CommunityRating') + criticrating = MBitem.get('CriticRating') + criticratingsummary = MBitem.get('CriticRatingSummary') + metascorerating = MBitem.get('Metascore') writer = " / ".join(people.get('Writer')) year = MBitem.get('ProductionYear') imdb = API().getProvider(MBitem, "imdb") @@ -177,9 +180,9 @@ class WriteKodiVideoDB(): query = "UPDATE movie SET c00 = ?, c01 = ?, c02 = ?, c03 = ?, c04 = ?, c05 = ?, c06 = ?, c07 = ?, c09 = ?, c10 = ?, c11 = ?, c12 = ?, c14 = ?, c15 = ?, c16 = ?, c18 = ?, c19 = ?, c21 = ? WHERE idMovie = ?" cursor.execute(query, (title, plot, shortplot, tagline, votecount, rating, writer, year, imdb, sorttitle, runtime, mpaa, genre, director, title, studio, trailerUrl, country, movieid)) - # Update the checksum in emby table - query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" - cursor.execute(query, (checksum, embyId)) + # Update the checksum in emby table and critic ratings + query = "UPDATE emby SET checksum = ?, rotten_tomatoes = ?, rotten_tomatoes_summary = ?, metascore = ? WHERE emby_id = ?" + cursor.execute(query, (checksum, criticrating, criticratingsummary, metascorerating, embyId)) ##### OR ADD THE MOVIE ##### else: @@ -220,8 +223,8 @@ class WriteKodiVideoDB(): self.AddTagToMedia(movieid, viewTag, "movie", cursor) # Create the reference in emby table - query = "INSERT INTO emby(emby_id, kodi_id, kodi_file_id, media_type, checksum) values(?, ?, ?, ?, ?)" - cursor.execute(query, (embyId, movieid, fileid, "movie", checksum)) + query = "INSERT INTO emby(emby_id, kodi_id, kodi_file_id, media_type, checksum, rotten_tomatoes, rotten_tomatoes_summary, metascore) values(?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(query, (embyId, movieid, fileid, "movie", checksum, criticrating, criticratingsummary, metascorerating)) # Update or insert actors diff --git a/service.py b/service.py index 966fa671..c4b26da6 100644 --- a/service.py +++ b/service.py @@ -23,7 +23,7 @@ from UserClient import UserClient from Player import Player from WebSocketClient import WebSocketThread from LibrarySync import LibrarySync - +from LibraryMonitor import LibraryMonitor class Service(): @@ -103,6 +103,9 @@ class Service(): player = Player() ws = WebSocketThread() library = LibrarySync() + librarymonitor = LibraryMonitor() + xbmc.log("START LIBRARY MONITOR") + librarymonitor.start() # Sync and progress report lastProgressUpdate = datetime.today() @@ -264,6 +267,9 @@ class Service(): if (self.newUserClient is not None): user.stopClient() + xbmc.log("STOP LIBRARY MONITOR") + librarymonitor.stop() + self.logMsg("======== STOP %s ========" % self.addonName, 0) #start the service