From 9041f16ff9827ea5a3e81bf6ba9943190a06a5e7 Mon Sep 17 00:00:00 2001 From: xnappo Date: Sat, 16 May 2015 10:58:48 -0500 Subject: [PATCH] Clean up shows emptied by web socket --- resources/lib/ReadKodiDB.py | 51 +++++++++++++++++++++++++++++++- resources/lib/WebSocketClient.py | 16 +++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/resources/lib/ReadKodiDB.py b/resources/lib/ReadKodiDB.py index a12b5feb..5a4e9aff 100644 --- a/resources/lib/ReadKodiDB.py +++ b/resources/lib/ReadKodiDB.py @@ -60,7 +60,56 @@ class ReadKodiDB(): return result[0] else: return None - + + def getShowIdByEmbyId(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + cursor.execute("SELECT parent_id FROM emby WHERE emby_id=?",(id,)) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None + + def getTypeByEmbyId(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + cursor.execute("SELECT media_type FROM emby WHERE emby_id=?",(id,)) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None + + def getShowTotalCount(self, id, connection=None, cursor=None): + if not connection: + connection = utils.KodiSQL() + cursor = connection.cursor() + closeCon = True + else: + closeCon = False + command = "SELECT totalCount FROM tvshowcounts WHERE idShow=%s" % str(id) + cursor.execute(command) + result = cursor.fetchone() + if closeCon: + connection.close() + if result: + return result[0] + else: + return None + def getKodiMusicArtists(self, connection, cursor): #returns all artists in Kodi db cursor.execute("SELECT kodi_id, emby_id, checksum FROM emby WHERE media_type='artist'") diff --git a/resources/lib/WebSocketClient.py b/resources/lib/WebSocketClient.py index aa65b468..0a611c82 100644 --- a/resources/lib/WebSocketClient.py +++ b/resources/lib/WebSocketClient.py @@ -21,6 +21,7 @@ from PlaybackUtils import PlaybackUtils from LibrarySync import LibrarySync from WriteKodiVideoDB import WriteKodiVideoDB from ReadEmbyDB import ReadEmbyDB +from ReadKodiDB import ReadKodiDB _MODE_BASICPLAY=12 @@ -253,9 +254,22 @@ class WebSocketThread(threading.Thread): connection = utils.KodiSQL("video") cursor = connection.cursor() for item in itemsRemoved: + type=ReadKodiDB().getTypeByEmbyId(item, connection, cursor) + self.logMsg("Type: " + str(type)) self.logMsg("Message : Doing LibraryChanged : Items Removed : Calling deleteItemFromKodiLibrary: " + item, 0) + if type == "episode": + showId=ReadKodiDB().getShowIdByEmbyId(item, connection, cursor) # Get the TV Show ID + self.logMsg("ShowID: " + str(showId),0) WriteKodiVideoDB().deleteItemFromKodiLibrary(item, connection, cursor) - connection.commit() + connection.commit() #Need to commit so that the count will be right - can't use one in case of multiple deletes + if type == "episode": + showTotalCount = ReadKodiDB().getShowTotalCount(showId, connection, cursor) # Check if there are no episodes left + self.logMsg("ShowTotalCount: " + str(showTotalCount),0) + if showTotalCount == 0 or showTotalCount == None: # Delete show if no episodes are left + embyId=ReadKodiDB().getEmbyIdByKodiId(showId, "tvshow", connection, cursor) + self.logMsg("Message : Doing LibraryChanged : Deleting show:" + embyId, 0) + WriteKodiVideoDB().deleteItemFromKodiLibrary(embyId, connection, cursor) + connection.commit() cursor.close() #Process music library