From 012bceca9015cd1f01ba770c06d7377be4ae0da4 Mon Sep 17 00:00:00 2001 From: xnappo Date: Sat, 28 Mar 2015 11:14:52 -0500 Subject: [PATCH] Fix episode deletes --- resources/lib/KodiMonitor.py | 33 +++++++++++++++++++++------------ resources/lib/LibrarySync.py | 2 +- resources/lib/WriteKodiDB.py | 4 ++++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py index b29194b5..5cd143fc 100644 --- a/resources/lib/KodiMonitor.py +++ b/resources/lib/KodiMonitor.py @@ -40,22 +40,31 @@ class Kodi_Monitor(xbmc.Monitor): WriteKodiDB().updatePlayCountFromKodi(item, type, playcount) if method == "VideoLibrary.OnRemove": xbmc.log('Intercepted remove from sender: ' + sender + ' method: ' + method + ' data: ' + data) + WINDOW = xbmcgui.Window( 10000 ) + if WINDOW.getProperty("suspendDeletes") == "True": + #This is a handshake to not try to delete if the item was removed from the Kodi DB due to sync + xbmc.log('Item deleted by sync') + WINDOW.setProperty("suspendDeletes", "False") + return jsondata = json.loads(data) if jsondata != None: if jsondata.get("type") == "episode": episodeid = jsondata.get("id") - WINDOW = xbmcgui.Window( 10000 ) - #ignore if the item has just been deleted by the background sync - if not WINDOW.getProperty(episodeid,"deleted"): - MBlist = WINDOW.getProperty("episodeid" + str(episodeid)).split(";;") - url='http://' + server + '/mediabrowser/Items?Ids=' + MBlist[1] + '&format=json' - data = DownloadUtils().downloadUrl(url=url, suppress=True, popup=0) - if data != "": - return_value = xbmcgui.Dialog().yesno("Confirm Delete", "Delete: "+ MBlist[0] + "\n on Emby Server?\nEmbyID: " + MBlist[1]) - if return_value: - url='http://' + server + '/mediabrowser/Items/' + MBlist[1] - xbmc.log('Deleting via URL: ' + url) - DownloadUtils().downloadUrl(url, type="DELETE") + MBlist = WINDOW.getProperty("episodeid" + str(episodeid)).split(";;") + #MBlist[0] is the ID, and [1] the title + url='http://' + server + '/mediabrowser/Items?Ids=' + MBlist[1] + '&format=json' + #This is a check to see if the item exists on the server, if it doesn't it may have already been deleted by another client + data = DownloadUtils().downloadUrl(url=url, suppress=True, popup=0) + result = json.loads(data) + item = result.get("Items")[0] + if WINDOW.getProperty("embyid" + item.get("Id")) == "deleted": + xbmc.log('Item was already deleted: ' + str(MBlist)) + elif data != "": + return_value = xbmcgui.Dialog().yesno("Confirm Delete", "Delete: "+ MBlist[0] + "\n on Emby Server?") + if return_value: + url='http://' + server + '/mediabrowser/Items/' + MBlist[1] + xbmc.log('Deleting via URL: ' + url) + DownloadUtils().downloadUrl(url, type="DELETE") diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 138718d0..c2f23b78 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -580,7 +580,7 @@ class LibrarySync(): allMB3EpisodeIds = set(allMB3EpisodeIds) for episode in allKodiEpisodeIds: if episode.get('episodeid') not in allMB3EpisodeIds: - WINDOW.setProperty(episode.get('episodeid'),"deleted") + WINDOW.setProperty("embyid" + str(episode.get('episodeid')),"deleted") WriteKodiDB().deleteEpisodeFromKodiLibrary(episode.get('episodeid'),episode.get('tvshowid')) cleanneeded = True totalItemsDeleted += 1 diff --git a/resources/lib/WriteKodiDB.py b/resources/lib/WriteKodiDB.py index 9ede4d54..d7ca3ae4 100644 --- a/resources/lib/WriteKodiDB.py +++ b/resources/lib/WriteKodiDB.py @@ -776,9 +776,13 @@ class WriteKodiDB(): if episode != None: strmfile = episode["file"] nfofile = strmfile.replace(".strm",".nfo") + WINDOW = xbmcgui.Window( 10000 ) + WINDOW.setProperty("suspendDeletes", "True") xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.RemoveEpisode", "params": { "episodeid": %i}, "id": 1 }' %(episode["episodeid"])) xbmcvfs.delete(strmfile) xbmcvfs.delete(nfofile) + while WINDOW.getProperty("suspendDeletes") == "True": + xbmc.sleep(100) def addTVShowToKodiLibrary( self, item ): itemPath = os.path.join(tvLibrary,item["Id"])