From e3412912ac81565fb6630f69607433f6e90f9ee9 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sun, 28 Jun 2015 07:08:06 -0500 Subject: [PATCH] Move websocket library to library sync This allows the websocket to properly communicate, since it's not busy doing incremental syncs anymore. --- resources/lib/LibrarySync.py | 117 +++++++++++++++++++++++++++++++ resources/lib/Player.py | 8 +-- resources/lib/WebSocketClient.py | 56 +-------------- 3 files changed, 123 insertions(+), 58 deletions(-) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 90be6ec3..9e400cea 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -36,13 +36,20 @@ WINDOW = xbmcgui.Window( 10000 ) class LibrarySync(threading.Thread): + _shared_state = {} + KodiMonitor = KodiMonitor.Kodi_Monitor() clientInfo = ClientInformation() addonName = clientInfo.getAddonName() + doIncrementalSync = False + updateItems = [] + removeItems = [] + def __init__(self, *args): + self.__dict__ = self._shared_state threading.Thread.__init__(self, *args) def logMsg(self, msg, lvl=1): @@ -648,6 +655,103 @@ class LibrarySync(threading.Thread): # tell any widgets to refresh because the content has changed WINDOW.setProperty("widgetreload", datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + def removefromDB(self, itemList, deleteEmbyItem = False): + # Delete from Kodi before Emby + # To be able to get mediaType + doUtils = DownloadUtils() + + video = [] + music = [] + + itemIds = ','.join(itemList) + url = "{server}/mediabrowser/Users/{UserId}/Items?Ids=%s&format=json" % itemIds + result = doUtils.downloadUrl(url) + + if result is "": + # Websocket feedback + self.logMsg("Item %s is removed." % itemIds) + return + + for item in result[u'Items']: + # Sort by type for database deletion + itemId = item["Id"] + mediaType = item["MediaType"] + + if "Video" in mediaType: + video.append(itemId) + elif "Audio" in mediaType: + music.append(itemId) + + if len(video) > 0: + #Process video library + connection = utils.KodiSQL("video") + cursor = connection.cursor() + + for item in video: + type = ReadKodiDB().getTypeByEmbyId(item, connection, cursor) + self.logMsg("Type: %s" % type) + self.logMsg("Message: Doing LibraryChanged: Items Removed: Calling deleteItemFromKodiLibrary: %s" % item, 0) + if "episode" in type: + # Get the TV Show Id for reference later + showId = ReadKodiDB().getShowIdByEmbyId(item, connection, cursor) + self.logMsg("ShowId: %s" % showId, 0) + WriteKodiVideoDB().deleteItemFromKodiLibrary(item, connection, cursor) + # Verification + if "episode" in type: + showTotalCount = ReadKodiDB().getShowTotalCount(showId, connection, cursor) + self.logMsg("ShowTotalCount: %s" % showTotalCount, 0) + # If there are no episodes left + if not showTotalCount: + # Delete show + embyId = ReadKodiDB().getEmbyIdByKodiId(showId, "tvshow", connection, cursor) + self.logMsg("Message: Doing LibraryChanged: Deleting show: %s" % embyId, 0) + WriteKodiVideoDB().deleteItemFromKodiLibrary(embyId, connection, cursor) + + connection.commit() + cursor.close() + + if len(music) > 0: + #Process music library + addon = xbmcaddon.Addon(id='plugin.video.emby') + if addon.getSetting("enableMusicSync") is "true": + connection = utils.KodiSQL("music") + cursor = connection.cursor() + + for item in music: + self.logMsg("Message : Doing LibraryChanged : Items Removed : Calling deleteItemFromKodiLibrary (musiclibrary): " + item, 0) + WriteKodiMusicDB().deleteItemFromKodiLibrary(item, connection, cursor) + + connection.commit() + cursor.close() + + if deleteEmbyItem: + for item in itemList: + url = "{server}/mediabrowser/Items/%s" % item + self.logMsg('Deleting via URL: %s' % url) + doUtils.downloadUrl(url, type="DELETE") + xbmc.executebuiltin("Container.Refresh") + + def remove_items(self, itemsRemoved): + + self.removeItems.extend(itemsRemoved) + + def update_items(self, itemsToUpdate): + # doing adds and updates + if(len(itemsToUpdate) > 0): + self.logMsg("Message : Doing LibraryChanged : Processing Added and Updated : " + str(itemsToUpdate), 0) + self.updateItems.extend(itemsToUpdate) + self.doIncrementalSync = True + + def user_data_update(self, userDataList): + self.updateItems = [] + for userData in userDataList: + itemId = userData.get("ItemId") + if(itemId != None): + self.updateItems.append(itemId) + if(len(self.updateItems) > 0): + self.logMsg("Message : Doing UserDataChanged : Processing Updated : " + str(self.updateItems), 0) + self.doIncrementalSync = True + def ShouldStop(self): if(xbmc.abortRequested): @@ -683,6 +787,19 @@ class LibrarySync(threading.Thread): libSync = self.FullLibrarySync() utils.logMsg("Doing_Db_Sync Post Resume: syncDatabase (Finished) " + str(libSync),0) + if self.doIncrementalSync: + # Add or update item to Kodi library + listItems = self.updateItems + self.updateItems = [] + self.doIncrementalSync = False + self.IncrementalSync(listItems) + + if len(self.removeItems) > 0: + # Remove item from Kodi library + listItems = self.removeItems + self.removeItems = [] + self.removefromDB(listItems) + if self.KodiMonitor.waitForAbort(1): # Abort was requested while waiting. We should exit break diff --git a/resources/lib/Player.py b/resources/lib/Player.py index e7e2e0b8..00390ea3 100644 --- a/resources/lib/Player.py +++ b/resources/lib/Player.py @@ -106,11 +106,9 @@ class Player( xbmc.Player ): if percentComplete > .80 and data.get("Type") == "Episode" and addonSettings.getSetting("offerDelete")=="true": return_value = xbmcgui.Dialog().yesno("Offer Delete", "Delete\n" + data.get("currentfile").split("/")[-1] + "\non Emby Server? ") if return_value: - url='{server}/mediabrowser/Items/' + item_id - xbmc.log('Deleting via URL: ' + url) - self.doUtils.downloadUrl(url, type="DELETE") - xbmc.sleep (15000) - xbmc.executebuiltin( "Container.Refresh" ) + # Delete Kodi entry before Emby + listItem = [item_id] + LibrarySync().removefromDB(listItem, True) #if(refresh_id != None): #report updates playcount and resume status to Kodi and MB3 #librarySync.updatePlayCount(item_id) diff --git a/resources/lib/WebSocketClient.py b/resources/lib/WebSocketClient.py index 65ced6c7..db996bd3 100644 --- a/resources/lib/WebSocketClient.py +++ b/resources/lib/WebSocketClient.py @@ -156,7 +156,7 @@ class WebSocketThread(threading.Thread): userDataList = data.get("UserDataList") self.logMsg("Message : Doing UserDataChanged : UserDataList : " + str(userDataList), 0) if(userDataList != None): - self.user_data_update(userDataList) + LibrarySync().user_data_update(userDataList) elif(messageType != None and messageType == "LibraryChanged"): foldersAddedTo = data.get("FoldersAddedTo") @@ -171,8 +171,8 @@ class WebSocketThread(threading.Thread): self.logMsg("Message : WebSocket LibraryChanged : Items Updated : " + str(itemsUpdated), 0) self.logMsg("Message : WebSocket LibraryChanged : Items Removed : " + str(itemsRemoved), 0) - self.remove_items(itemsRemoved) - self.update_items(itemsToUpdate) + LibrarySync().remove_items(itemsRemoved) + LibrarySync().update_items(itemsToUpdate) elif messageType == "GeneralCommand": @@ -248,56 +248,6 @@ class WebSocketThread(threading.Thread): result = xbmc.executeJSONRPC(text) else: self.logMsg("Unknown command.", 1) - - def remove_items(self, itemsRemoved): - - #Process video library - 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) - 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 - addon = xbmcaddon.Addon(id='plugin.video.emby') - if addon.getSetting("enableMusicSync") == "true": - connection = utils.KodiSQL("music") - cursor = connection.cursor() - for item in itemsRemoved: - self.logMsg("Message : Doing LibraryChanged : Items Removed : Calling deleteItemFromKodiLibrary (musiclibrary): " + item, 0) - WriteKodiMusicDB().deleteItemFromKodiLibrary(item, connection, cursor) - connection.commit() - cursor.close() - - def update_items(self, itemsToUpdate): - # doing adds and updates - if(len(itemsToUpdate) > 0): - self.logMsg("Message : Doing LibraryChanged : Processing Added and Updated : " + str(itemsToUpdate), 0) - LibrarySync().IncrementalSync(itemsToUpdate) - - def user_data_update(self, userDataList): - itemsToUpdate = list() - for userData in userDataList: - itemId = userData.get("ItemId") - if(itemId != None): - itemsToUpdate.append(itemId) - if(len(itemsToUpdate) > 0): - self.logMsg("Message : Doing UserDataChanged : Processing Updated : " + str(itemsToUpdate), 0) - LibrarySync().IncrementalSync(itemsToUpdate) def on_error(self, ws, error): if "10061" in str(error):