From 23b9f000e765cb2d2ba7622ffa9c34d1071dae85 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Fri, 5 Jun 2015 01:05:40 -0500 Subject: [PATCH 01/13] Unicode paths for users with special characters --- service.py | 248 +++++++++++++++++++++++++++-------------------------- 1 file changed, 128 insertions(+), 120 deletions(-) diff --git a/service.py b/service.py index 80a9d82b..b86c535c 100644 --- a/service.py +++ b/service.py @@ -1,155 +1,188 @@ +# -- coding: utf-8 -- + +import os +import sys +import time +from datetime import datetime + import xbmcaddon import xbmc import xbmcgui -import os -import threading -import json -from datetime import datetime -import time -cwd = xbmcaddon.Addon(id='plugin.video.emby').getAddonInfo('path') -BASE_RESOURCE_PATH = xbmc.translatePath( os.path.join( cwd, 'resources', 'lib' ) ) -sys.path.append(BASE_RESOURCE_PATH) +addon_ = xbmcaddon.Addon(id='plugin.video.emby') +addon_path = addon_.getAddonInfo('path').decode('utf-8') +base_resource_path = xbmc.translatePath(os.path.join(addon_path, 'resources', 'lib')).decode('utf-8') +sys.path.append(base_resource_path) import KodiMonitor import Utils as utils -from LibrarySync import LibrarySync -from Player import Player -from DownloadUtils import DownloadUtils -from ConnectionManager import ConnectionManager from ClientInformation import ClientInformation -from WebSocketClient import WebSocketThread +from ConnectionManager import ConnectionManager from UserClient import UserClient -from PlaybackUtils import PlaybackUtils -librarySync = LibrarySync() +from Player import Player +from WebSocketClient import WebSocketThread +from LibrarySync import LibrarySync class Service(): - - newWebSocketThread = None - newUserClient = None - - clientInfo = ClientInformation() KodiMonitor = KodiMonitor.Kodi_Monitor() + clientInfo = ClientInformation() + librarySync = LibrarySync() + addonName = clientInfo.getAddonName() WINDOW = xbmcgui.Window(10000) + newWebSocketThread = None + newUserClient = None warn_auth = True + welcome_msg = True server_online = True - - def __init__(self, *args ): - addonName = self.addonName + def __init__(self, *args): + + addonName = self.addonName + WINDOW = self.WINDOW + + # Initial logging self.logMsg("Starting Monitor", 0) self.logMsg("======== START %s ========" % addonName, 0) - self.logMsg("KODI Version: %s" % xbmc.getInfoLabel("System.BuildVersion"), 0) - self.logMsg("%s Version: %s" % (addonName, self.clientInfo.getVersion()), 0) self.logMsg("Platform: %s" % (self.clientInfo.getPlatform()), 0) + self.logMsg("KODI Version: %s" % xbmc.getInfoLabel('System.BuildVersion'), 0) + self.logMsg("%s Version: %s" % (addonName, self.clientInfo.getVersion()), 0) + self.logMsg("Log Level: %s" % UserClient().getLogLevel(), 1) + + # Reset window props for profile switch + WINDOW.clearProperty('Server_online') + WINDOW.clearProperty('Server_status') + WINDOW.clearProperty('startup') + + embyProperty = WINDOW.getProperty('Emby.nodes.total') + propNames = [ - #reset all window props on startup for user profile switches - self.WINDOW.clearProperty("startup") - embyProperty = self.WINDOW.getProperty("Emby.nodes.total") - propNames = ["index","path","title","content","inprogress.content","inprogress.title","inprogress.content","inprogress.path","nextepisodes.title","nextepisodes.content","nextepisodes.path","unwatched.title","unwatched.content","unwatched.path","recent.title","recent.content","recent.path","recentepisodes.title","recentepisodes.content","recentepisodes.path","inprogressepisodes.title","inprogressepisodes.content","inprogressepisodes.path"] + "index","path","title","content", + "inprogress.content","inprogress.title", + "inprogress.content","inprogress.path", + "nextepisodes.title","nextepisodes.content", + "nextepisodes.path","unwatched.title", + "unwatched.content","unwatched.path", + "recent.title","recent.content","recent.path", + "recentepisodes.title","recentepisodes.content", + "recentepisodes.path","inprogressepisodes.title", + "inprogressepisodes.content","inprogressepisodes.path" + ] + if embyProperty: totalNodes = int(embyProperty) for i in range(totalNodes): for prop in propNames: - self.WINDOW.clearProperty("Emby.nodes.%s.%s" %(str(i),prop)) - + WINDOW.clearProperty('Emby.nodes.%s.%s' % (str(i), prop)) def logMsg(self, msg, lvl=1): - + className = self.__class__.__name__ - utils.logMsg("%s %s" % (self.addonName, className), str(msg), int(lvl)) - + utils.logMsg("%s %s" % (self.addonName, className), msg, int(lvl)) + def ServiceEntryPoint(self): WINDOW = self.WINDOW - WINDOW.setProperty("Server_online", "") - self.WINDOW.setProperty("Server_status", "") - WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) + # Server auto-detect ConnectionManager().checkServer() - lastProgressUpdate = datetime.today() - startupComplete = False - + + # Initialize important threads user = UserClient() player = Player() ws = WebSocketThread() - - lastFile = None - + + # Sync and progress report + startupComplete = False + lastProgressUpdate = datetime.today() + while not self.KodiMonitor.abortRequested(): - #WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) - - if self.KodiMonitor.waitForAbort(1): - # Abort was requested while waiting. We should exit - break + # Before proceeding, need to make sure: + # 1. Server is online + # 2. User is set + # 3. User has access to the server + if WINDOW.getProperty('Server_online') == "true": - # Server is online - if (user.currUser != None) and (user.HasAccess == True): - self.warn_auth = True + + # Emby server is online + # Verify if user is set and has access to the server + if (user.currUser != None) and user.HasAccess: + + if self.welcome_msg: + # Reset authentication warnings + self.welcome_msg = False + self.warn_auth = True + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, sound=False) - # Correctly launch the websocket, if user manually launches the add-on + # Start the Websocket Client if (self.newWebSocketThread == None): self.newWebSocketThread = "Started" ws.start() + # If an item is playing if xbmc.Player().isPlaying(): - #WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) try: + # Update and report progress playTime = xbmc.Player().getTime() totalTime = xbmc.Player().getTotalTime() currentFile = xbmc.Player().getPlayingFile() - if(player.played_information.get(currentFile) != None): + # Update positionticks + if player.played_information.get(currentFile) != None: player.played_information[currentFile]["currentPosition"] = playTime - # send update td = datetime.today() - lastProgressUpdate secDiff = td.seconds - if(secDiff > 3): + + # Report progress to Emby server + if (secDiff > 3): try: player.reportPlayback() - except Exception, msg: + except Exception as msg: self.logMsg("Exception reporting progress: %s" % msg) pass lastProgressUpdate = datetime.today() + elif WINDOW.getProperty('commandUpdate') == "true": + # Received a remote control command that + # requires updating immediately try: WINDOW.clearProperty('commandUpdate') player.reportPlayback() except: pass lastProgressUpdate = datetime.today() - except Exception, e: + except Exception as e: self.logMsg("Exception in Playback Monitor Service: %s" % e) pass else: - #full sync - if (startupComplete == False): - self.logMsg("Doing_Db_Sync: syncDatabase (Started)") - libSync = librarySync.FullLibrarySync() - self.logMsg("Doing_Db_Sync: syncDatabase (Finished) " + str(libSync)) - #WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) - if (libSync): + # Library sync + if not startupComplete: + # Run full sync + self.logMsg("Doing_Db_Sync: syncDatabase (Started)", 1) + libSync = self.librarySync.FullLibrarySync() + self.logMsg("Doing_Db_Sync: syncDatabase (Finished) %s" % libSync, 1) + + if libSync: startupComplete = True - else: - if self.KodiMonitor.waitForAbort(1): - # Abort was requested while waiting. We should exit - break + else: - if self.warn_auth: - self.logMsg("Not authenticated yet.", 1) + if (user.currUser == None) and self.warn_auth: + # Alert user is not authenticated and suppress future warning self.warn_auth = False + self.logMsg("Not authenticated yet.", 1) + # User access is restricted. + # Keep verifying until access is granted + # unless server goes offline or Kodi is shut down. while user.HasAccess == False: - - #WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) + # Verify access with an API call user.hasAccess() if WINDOW.getProperty('Server_online') != "true": @@ -160,38 +193,41 @@ class Service(): # Abort was requested while waiting. We should exit break - else: - # Wait until server becomes online or shut down is requested + # Wait until Emby server is online + # or Kodi is shut down. while not self.KodiMonitor.abortRequested(): - #WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time()))) if user.getServer() == "": + # No server info set in add-on settings pass + elif user.getPublicUsers() == False: - # Server is not online, suppress future warning + # Server is offline. + # Alert the user and suppress future warning if self.server_online: - WINDOW.setProperty("Server_online", "false") self.logMsg("Server is offline.", 1) - xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName) + WINDOW.setProperty('Server_online', "false") + xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName, sound=False) self.server_online = False + else: # Server is online if not self.server_online: - # Server was not online when Kodi started. + # Server was offline when Kodi started. # Wait for server to be fully established. if self.KodiMonitor.waitForAbort(5): # Abort was requested while waiting. break + # Alert the user that server is online. + xbmcgui.Dialog().notification("Connection successful", "%s Server is online." % self.addonName, time=2000, sound=False) + self.server_online = True self.logMsg("Server is online and ready.", 1) - addonSettings = xbmcaddon.Addon(id='plugin.video.emby') - if addonSettings.getSetting("supressConnectMsg")=="false": - xbmcgui.Dialog().notification("Connection successful", "%s Server is online." % self.addonName, time=2000) - WINDOW.setProperty("Server_online", "true") + WINDOW.setProperty('Server_online', "true") - # Server is online, proceed. - if (self.newUserClient == None): + # Start the User client + if self.newUserClient == None: self.newUserClient = "Started" user.start() break @@ -200,12 +236,14 @@ class Service(): # Abort was requested while waiting. break - #self.checkService() + if self.KodiMonitor.waitForAbort(1): + # Abort was requested while waiting. We should exit + break # If user reset library database. - if WINDOW.getProperty("SyncInstallRunDone") == "false": + if WINDOW.getProperty('SyncInstallRunDone') == "false": addon = xbmcaddon.Addon('plugin.video.emby') - addon.setSetting("SyncInstallRunDone", "false") + addon.setSetting('SyncInstallRunDone', "false") if (self.newWebSocketThread != None): ws.stopClient() @@ -215,35 +253,5 @@ class Service(): self.logMsg("======== STOP %s ========" % self.addonName, 0) - # To be reviewed when moving the sync process to it's own thread - '''def checkService(self): - - WINDOW = self.WINDOW - timeStamp = WINDOW.getProperty("Emby_Service_Timestamp") - loops = 0 - - while(timeStamp == ""): - timeStamp = WINDOW.getProperty("Emby_Service_Timestamp") - loops = loops + 1 - if(loops == 5): - self.logMsg("Emby Service Not Running, no time stamp, exiting.", 0) - addon = xbmcaddon.Addon(id='plugin.video.emby') - language = addon.getLocalizedString - xbmcgui.Dialog().ok(language(30135), language(30136), language(30137)) - sys.exit() - if self.KodiMonitor.waitForAbort(1): - # Abort was requested while waiting. We should exit - return - - self.logMsg("Emby Service Timestamp: " + timeStamp, 2) - self.logMsg("Emby Current Timestamp: " + str(int(time.time())), 2) - - if((int(timeStamp) + 30) < int(time.time())): - self.logMsg("Emby Service Not Running, time stamp to old, exiting.", 0) - addon = xbmcaddon.Addon(id='plugin.video.emby') - language = addon.getLocalizedString - xbmcgui.Dialog().ok(language(30135), language(30136), language(30137)) - sys.exit()''' - #start the service -Service().ServiceEntryPoint() +Service().ServiceEntryPoint() \ No newline at end of file From ef1a88db794a9085543e0b6b04dfeea539cde7d2 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 15 Jun 2015 20:23:19 -0500 Subject: [PATCH 02/13] Add latest changes --- service.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/service.py b/service.py index b86c535c..c7cf80ab 100644 --- a/service.py +++ b/service.py @@ -31,6 +31,7 @@ class Service(): librarySync = LibrarySync() addonName = clientInfo.getAddonName() + logLevel = UserClient().getLogLevel() WINDOW = xbmcgui.Window(10000) newWebSocketThread = None @@ -43,6 +44,7 @@ class Service(): addonName = self.addonName WINDOW = self.WINDOW + WINDOW.setProperty('getLogLevel', str(self.logLevel)) # Initial logging self.logMsg("Starting Monitor", 0) @@ -50,7 +52,7 @@ class Service(): self.logMsg("Platform: %s" % (self.clientInfo.getPlatform()), 0) self.logMsg("KODI Version: %s" % xbmc.getInfoLabel('System.BuildVersion'), 0) self.logMsg("%s Version: %s" % (addonName, self.clientInfo.getVersion()), 0) - self.logMsg("Log Level: %s" % UserClient().getLogLevel(), 1) + self.logMsg("Log Level: %s" % self.logLevel, 1) # Reset window props for profile switch WINDOW.clearProperty('Server_online') @@ -86,6 +88,7 @@ class Service(): def ServiceEntryPoint(self): WINDOW = self.WINDOW + addon = xbmcaddon.Addon() # Server auto-detect ConnectionManager().checkServer() @@ -112,11 +115,12 @@ class Service(): # Verify if user is set and has access to the server if (user.currUser != None) and user.HasAccess: - if self.welcome_msg: - # Reset authentication warnings - self.welcome_msg = False - self.warn_auth = True - xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, sound=False) + self.warn_auth = True + if addon.getSetting('supressConnectMsg') == "false": + if self.welcome_msg: + # Reset authentication warnings + self.welcome_msg = False + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, time=2000, sound=False) # Start the Websocket Client if (self.newWebSocketThread == None): @@ -242,7 +246,7 @@ class Service(): # If user reset library database. if WINDOW.getProperty('SyncInstallRunDone') == "false": - addon = xbmcaddon.Addon('plugin.video.emby') + addon = xbmcaddon.Addon() addon.setSetting('SyncInstallRunDone', "false") if (self.newWebSocketThread != None): From 0ac2ea1e09fa10ff6a57083e27ad71d125042cb7 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 15 Jun 2015 20:27:11 -0500 Subject: [PATCH 03/13] quick follow up --- service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.py b/service.py index c7cf80ab..3b09ef1f 100644 --- a/service.py +++ b/service.py @@ -9,7 +9,7 @@ import xbmcaddon import xbmc import xbmcgui -addon_ = xbmcaddon.Addon(id='plugin.video.emby') +addon_ = xbmcaddon.Addon() addon_path = addon_.getAddonInfo('path').decode('utf-8') base_resource_path = xbmc.translatePath(os.path.join(addon_path, 'resources', 'lib')).decode('utf-8') sys.path.append(base_resource_path) From 04e0d4f89ecba61a3787e241509acad62df1ff45 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 15 Jun 2015 20:30:45 -0500 Subject: [PATCH 04/13] Revert "quick follow up" This reverts commit 0ac2ea1e09fa10ff6a57083e27ad71d125042cb7. --- service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.py b/service.py index 3b09ef1f..c7cf80ab 100644 --- a/service.py +++ b/service.py @@ -9,7 +9,7 @@ import xbmcaddon import xbmc import xbmcgui -addon_ = xbmcaddon.Addon() +addon_ = xbmcaddon.Addon(id='plugin.video.emby') addon_path = addon_.getAddonInfo('path').decode('utf-8') base_resource_path = xbmc.translatePath(os.path.join(addon_path, 'resources', 'lib')).decode('utf-8') sys.path.append(base_resource_path) From 08c3ea1a33b3ab14bd4282a3ad1ccf13a95f751c Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 15 Jun 2015 20:33:26 -0500 Subject: [PATCH 05/13] quick follow up --- service.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/service.py b/service.py index c7cf80ab..e57bf688 100644 --- a/service.py +++ b/service.py @@ -9,7 +9,7 @@ import xbmcaddon import xbmc import xbmcgui -addon_ = xbmcaddon.Addon(id='plugin.video.emby') +addon_ = xbmcaddon.Addon() addon_path = addon_.getAddonInfo('path').decode('utf-8') base_resource_path = xbmc.translatePath(os.path.join(addon_path, 'resources', 'lib')).decode('utf-8') sys.path.append(base_resource_path) @@ -246,7 +246,6 @@ class Service(): # If user reset library database. if WINDOW.getProperty('SyncInstallRunDone') == "false": - addon = xbmcaddon.Addon() addon.setSetting('SyncInstallRunDone', "false") if (self.newWebSocketThread != None): From 2de317edee340dc5e698d5800fa4073736b22571 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Tue, 16 Jun 2015 02:11:38 -0500 Subject: [PATCH 06/13] Quick adjustment First if will be xbmc playing for quicker report progress --- service.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/service.py b/service.py index 097dde87..2bde048d 100644 --- a/service.py +++ b/service.py @@ -115,23 +115,6 @@ class Service(): # Emby server is online # Verify if user is set and has access to the server if (user.currUser != None) and user.HasAccess: - - self.warn_auth = True - if addon.getSetting('supressConnectMsg') == "false": - if self.welcome_msg: - # Reset authentication warnings - self.welcome_msg = False - xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, time=2000, sound=False) - - # Start the Websocket Client - if (self.newWebSocketThread == None): - self.newWebSocketThread = "Started" - ws.start() - - # Start the LibrarySync Thread - if (self.newLibraryThread == None): - self.newLibraryThread = "Started" - library.start() # If an item is playing if xbmc.Player().isPlaying(): @@ -169,6 +152,23 @@ class Service(): except Exception as e: self.logMsg("Exception in Playback Monitor Service: %s" % e) pass + else: + # Start up events + self.warn_auth = True + if addon.getSetting('supressConnectMsg') == "false": + if self.welcome_msg: + # Reset authentication warnings + self.welcome_msg = False + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, time=2000, sound=False) + + # Start the Websocket Client + if (self.newWebSocketThread == None): + self.newWebSocketThread = "Started" + ws.start() + + if (self.newLibraryThread == None): + self.newLibraryThread = "Started" + library.start() else: From 324137460d02740f9804c9737931ef7da86d09df Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Tue, 16 Jun 2015 03:25:12 -0500 Subject: [PATCH 07/13] Further modification for service Forgot to encode username --- service.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/service.py b/service.py index 2bde048d..ad23b2d0 100644 --- a/service.py +++ b/service.py @@ -9,7 +9,7 @@ import xbmcaddon import xbmc import xbmcgui -addon_ = xbmcaddon.Addon() +addon_ = xbmcaddon.Addon(id='plugin.video.emby') addon_path = addon_.getAddonInfo('path').decode('utf-8') base_resource_path = xbmc.translatePath(os.path.join(addon_path, 'resources', 'lib')).decode('utf-8') sys.path.append(base_resource_path) @@ -136,8 +136,7 @@ class Service(): try: player.reportPlayback() except Exception as msg: - self.logMsg("Exception reporting progress: %s" % msg) - pass + self.logMsg("Exception reporting progress: %s" % msg)- lastProgressUpdate = datetime.today() elif WINDOW.getProperty('commandUpdate') == "true": @@ -159,13 +158,13 @@ class Service(): if self.welcome_msg: # Reset authentication warnings self.welcome_msg = False - xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser, time=2000, sound=False) + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % user.currUser.decode('utf-8').encode('utf-8'), time=2000, sound=False) # Start the Websocket Client if (self.newWebSocketThread == None): self.newWebSocketThread = "Started" ws.start() - + # Start the Library Sync Thread if (self.newLibraryThread == None): self.newLibraryThread = "Started" library.start() From e4647a90b2cb5ed68da3b77df04749c95d3934dd Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sat, 27 Jun 2015 20:02:00 -0500 Subject: [PATCH 08/13] Remove librarysync import Prevent import error loop --- resources/lib/KodiMonitor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py index b06569a1..e6be1443 100644 --- a/resources/lib/KodiMonitor.py +++ b/resources/lib/KodiMonitor.py @@ -11,7 +11,6 @@ import json import Utils as utils from WriteKodiVideoDB import WriteKodiVideoDB from ReadKodiDB import ReadKodiDB -from LibrarySync import LibrarySync from PlayUtils import PlayUtils from DownloadUtils import DownloadUtils from PlaybackUtils import PlaybackUtils From 8fbe680b5e8785f20b284e503e37b6ac197d3d56 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sat, 27 Jun 2015 23:44:39 -0500 Subject: [PATCH 09/13] Typo --- service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.py b/service.py index ad23b2d0..00e58901 100644 --- a/service.py +++ b/service.py @@ -136,7 +136,7 @@ class Service(): try: player.reportPlayback() except Exception as msg: - self.logMsg("Exception reporting progress: %s" % msg)- + self.logMsg("Exception reporting progress: %s" % msg) lastProgressUpdate = datetime.today() elif WINDOW.getProperty('commandUpdate') == "true": From e3412912ac81565fb6630f69607433f6e90f9ee9 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sun, 28 Jun 2015 07:08:06 -0500 Subject: [PATCH 10/13] 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): From 986d60d3703f0806e06619caa1f53ec46e5d0468 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sun, 28 Jun 2015 08:33:58 -0500 Subject: [PATCH 11/13] remove reset of updateItem list --- resources/lib/LibrarySync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 9e400cea..fb449623 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -743,7 +743,7 @@ class LibrarySync(threading.Thread): self.doIncrementalSync = True def user_data_update(self, userDataList): - self.updateItems = [] + # do full playcount update for now for userData in userDataList: itemId = userData.get("ItemId") if(itemId != None): From 5f9dc429500d495d840ad478e39cec373ff088f2 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sun, 28 Jun 2015 08:36:44 -0500 Subject: [PATCH 12/13] Fix fail paste --- resources/lib/LibrarySync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index fb449623..bc5482b2 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -701,7 +701,7 @@ class LibrarySync(threading.Thread): showTotalCount = ReadKodiDB().getShowTotalCount(showId, connection, cursor) self.logMsg("ShowTotalCount: %s" % showTotalCount, 0) # If there are no episodes left - if not showTotalCount: + if showTotalCount == 0 or showTotalCount == None: # Delete show embyId = ReadKodiDB().getEmbyIdByKodiId(showId, "tvshow", connection, cursor) self.logMsg("Message: Doing LibraryChanged: Deleting show: %s" % embyId, 0) From 0b9b5d3e643cbde64bd7df5c6e1d013a60be2360 Mon Sep 17 00:00:00 2001 From: xnappo Date: Sat, 4 Jul 2015 10:04:04 -0500 Subject: [PATCH 13/13] Expand offer delete to movies (optionally) --- resources/language/English/strings.xml | 10 ++++++---- resources/lib/KodiMonitor.py | 2 +- resources/lib/PlaybackUtils.py | 4 +++- resources/lib/Player.py | 11 +++++++---- resources/settings.xml | 6 ++++-- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index 33d65f09..a5c50521 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -97,9 +97,9 @@ Include Stream Info Include People Include Overview - On Resume Jump Back Seconds - - Offer delete when stopped above % - Add Item and Played Counts + Offer delete after playback + - For Episodes + - For Movies Background Art Refresh Rate (seconds) Add Resume Percent Add Episode Number @@ -110,7 +110,7 @@ Downloading Jason Data Done Processing Item : - Offer delete for watched episodes + YOUCANUSETHIS Play Error This item is not playable Local path detected @@ -255,6 +255,8 @@ Music Videos Music Tracks Channels + + diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py index e6be1443..e2d8cdd5 100644 --- a/resources/lib/KodiMonitor.py +++ b/resources/lib/KodiMonitor.py @@ -134,7 +134,7 @@ class Kodi_Monitor(xbmc.Monitor): cursor.close if jsondata: - if jsondata.get("type") == "episode": + if jsondata.get("type") == "episode" or "movie": url='{server}/mediabrowser/Items?Ids=' + id + '&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 result = DownloadUtils().downloadUrl(url) diff --git a/resources/lib/PlaybackUtils.py b/resources/lib/PlaybackUtils.py index 8211e115..026ebeaa 100644 --- a/resources/lib/PlaybackUtils.py +++ b/resources/lib/PlaybackUtils.py @@ -379,8 +379,10 @@ class PlaybackUtils(): WINDOW.setProperty(playurl + "positionurl", positionurl) WINDOW.setProperty(playurl + "deleteurl", "") - if item.get("Type") == "Episode" and addon.getSetting("offerDelete")=="true": + if item.get("Type") == "Episode" and addon.getSetting("offerDeleteTV")=="true": WINDOW.setProperty(playurl + "deleteurl", deleteurl) + if item.get("Type") == "Movie" and addon.getSetting("offerDeleteMovies")=="true": + WINDOW.setProperty(playurl + "deleteurl", deleteurl) WINDOW.setProperty(playurl + "runtimeticks", str(item.get("RunTimeTicks"))) WINDOW.setProperty(playurl+"type", item.get("Type")) diff --git a/resources/lib/Player.py b/resources/lib/Player.py index 00390ea3..3e5fd9cd 100644 --- a/resources/lib/Player.py +++ b/resources/lib/Player.py @@ -103,15 +103,18 @@ class Player( xbmc.Player ): self.stopPlayback(data) - if percentComplete > .80 and data.get("Type") == "Episode" and addonSettings.getSetting("offerDelete")=="true": + offerDelete=False + if data.get("Type") == "Episode" and addonSettings.getSetting("offerDeleteTV")=="true": + offerDelete = True + elif data.get("Type") == "Movie" and addonSettings.getSetting("offerDeleteMovies")=="true": + offerDelete = True + + if percentComplete > .80 and offerDelete == True: return_value = xbmcgui.Dialog().yesno("Offer Delete", "Delete\n" + data.get("currentfile").split("/")[-1] + "\non Emby Server? ") if return_value: # 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) # Stop transcoding if self.WINDOW.getProperty("transcoding%s" % item_id) == "true": diff --git a/resources/settings.xml b/resources/settings.xml index 6eaca198..83ea2959 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -25,7 +25,9 @@ - + + + @@ -35,7 +37,7 @@ - +