From 1e3f590d99778719820a2e0c777ce67f5618d496 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 8 Feb 2016 15:24:35 -0600 Subject: [PATCH 1/6] Adjust doutils --- resources/lib/read_embyserver.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/resources/lib/read_embyserver.py b/resources/lib/read_embyserver.py index 774a9465..6bd8dafc 100644 --- a/resources/lib/read_embyserver.py +++ b/resources/lib/read_embyserver.py @@ -2,6 +2,8 @@ ################################################################################################# +import xbmc + import utils import clientinfo import downloadutils @@ -18,7 +20,7 @@ class Read_EmbyServer(): self.clientInfo = clientinfo.ClientInfo() self.addonName = self.clientInfo.getAddonName() - self.doUtils = downloadutils.DownloadUtils() + self.doUtils = downloadutils.DownloadUtils().downloadUrl self.userId = utils.window('emby_currUser') self.server = utils.window('emby_server%s' % self.userId) @@ -39,7 +41,7 @@ class Read_EmbyServer(): item = {} url = "{server}/emby/Users/{UserId}/Items/%s?format=json" % itemid - result = self.doUtils.downloadUrl(url) + result = self.doUtils(url) if result: item = result @@ -58,7 +60,7 @@ class Read_EmbyServer(): 'Ids': ",".join(itemlist), 'Fields': "Etag" } - result = self.doUtils.downloadUrl(url, parameters=params) + result = self.doUtils(url, parameters=params) if result: items.extend(result['Items']) @@ -85,7 +87,7 @@ class Read_EmbyServer(): "MediaSources" ) } - result = self.doUtils.downloadUrl(url, parameters=params) + result = self.doUtils(url, parameters=params) if result: items.extend(result['Items']) @@ -95,7 +97,7 @@ class Read_EmbyServer(): # Returns ancestors using embyId viewId = None url = "{server}/emby/Items/%s/Ancestors?UserId={UserId}&format=json" % itemid - result = self.doUtils.downloadUrl(url) + result = self.doUtils(url) for view in result: @@ -147,7 +149,7 @@ class Read_EmbyServer(): "CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations," "Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers") } - return doUtils.downloadUrl(url, parameters=params) + return doUtils(url, parameters=params) def getTvChannels(self): doUtils = self.doUtils @@ -161,7 +163,7 @@ class Read_EmbyServer(): "CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations," "Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers") } - return doUtils.downloadUrl(url, parameters=params) + return doUtils(url, parameters=params) def getTvRecordings(self, groupid): doUtils = self.doUtils @@ -177,7 +179,7 @@ class Read_EmbyServer(): "CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations," "Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers") } - return doUtils.downloadUrl(url, parameters=params) + return doUtils(url, parameters=params) def getSection(self, parentid, itemtype=None, sortby="SortName", basic=False, dialog=None): @@ -200,7 +202,7 @@ class Read_EmbyServer(): 'Recursive': True, 'Limit': 1 } - result = doUtils.downloadUrl(url, parameters=params) + result = doUtils(url, parameters=params) try: total = result['TotalRecordCount'] items['TotalRecordCount'] = total @@ -314,7 +316,7 @@ class Read_EmbyServer(): else: # Views ungrouped url = "{server}/emby/Users/{UserId}/Items?Sortby=SortName&format=json" - result = doUtils.downloadUrl(url) + result = doUtils(url) try: items = result['Items'] @@ -339,7 +341,7 @@ class Read_EmbyServer(): # Assumed missing is mixed then. if itemtype is None: url = "{server}/emby/Library/MediaFolders?format=json" - result = doUtils.downloadUrl(url) + result = doUtils(url) for folder in result['Items']: if itemId == folder['Id']: @@ -407,7 +409,7 @@ class Read_EmbyServer(): 'IsVirtualUnaired': False, 'Fields': "Etag" } - result = self.doUtils.downloadUrl(url, parameters=params) + result = self.doUtils(url, parameters=params) if result: items = result @@ -447,7 +449,7 @@ class Read_EmbyServer(): 'Recursive': True, 'Limit': 1 } - result = doUtils.downloadUrl(url, parameters=params) + result = doUtils(url, parameters=params) try: total = result['TotalRecordCount'] items['TotalRecordCount'] = total @@ -477,7 +479,7 @@ class Read_EmbyServer(): "AirTime,DateCreated,MediaStreams,People,ProviderIds,Overview" ) } - result = doUtils.downloadUrl(url, parameters=params) + result = doUtils(url, parameters=params) items['Items'].extend(result['Items']) index += jump @@ -519,7 +521,7 @@ class Read_EmbyServer(): } url = "{server}/emby/Videos/%s/AdditionalParts?UserId={UserId}&format=json" % itemId - result = self.doUtils.downloadUrl(url) + result = self.doUtils(url) if result: items = result From 994a48df7e47ecfaf1aa9723056d09ef3d329395 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Mon, 8 Feb 2016 18:38:41 -0600 Subject: [PATCH 2/6] Review item throttle --- resources/lib/read_embyserver.py | 44 +++++++++++++++----------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/resources/lib/read_embyserver.py b/resources/lib/read_embyserver.py index 6bd8dafc..3b105d54 100644 --- a/resources/lib/read_embyserver.py +++ b/resources/lib/read_embyserver.py @@ -244,13 +244,15 @@ class Read_EmbyServer(): "MediaSources" ) result = doUtils(url, parameters=params) - - if result == "": - # Something happened to the connection. + try: + items['Items'].extend(result['Items']) + except TypeError: + # Something happened to the connection if not throttled: throttled = True self.logMsg("Throttle activated.", 1) - elif jump == highestjump: + + if jump == highestjump: # We already adjusted to highestjump, but it failed. Reset value self.logMsg("Reset highest value.", 1) highestjump = 0 @@ -259,25 +261,24 @@ class Read_EmbyServer(): if highestjump: throttled = False jump = highestjump + self.logMsg("Throttle deactivated.", 1) else: - jump = int(jump/2) + jump = int(jump/4) + self.logMsg("Set jump limit to recover: %s" % jump, 1) - self.logMsg("Set jump limit to recover: %s" % jump) retry = 0 while utils.window('emby_online') != "true": # Wait server to come back online if retry == 3: - self.logMsg("Server never came back online.") + self.logMsg("Unable to reconnect to server. Abort process.", 1) return - + retry += 1 if xbmc.Monitor().waitForAbort(1): # Abort was requested while waiting. return - else: - xbmc.Monitor().waitForAbort(3) else: - items['Items'].extend(result['Items']) + # Request succeeded index += jump if dialog: @@ -289,20 +290,15 @@ class Read_EmbyServer(): highestjump = jump if throttled: - # We needed to adjust the number, keep increasing until. - if jump < highestjump: - # Found a number that already works, use it. - throttled = False - jump = highestjump - self.logMsg("Throttle deactivated with jump limit set to: %s" % jump, 1) - else: - # keep increasing until the connection times out again - increment = int(jump*0.33) - if not increment: # Incase the increment is 0 - increment += 10 + # We needed to adjust the number of item requested. + # keep increasing until the connection times out again + # to find the highest value + increment = int(jump*0.33) + if not increment: # Incase the increment is 0 + increment = 10 - jump += increment - self.logMsg("Increase jump limit to: %s" % jump, 1) + jump += increment + self.logMsg("Increase jump limit to: %s" % jump, 1) return items def getViews(self, type, root=False): From 15e0f9209240458d4a845f83142adc0f28e72fdb Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Tue, 9 Feb 2016 22:50:31 -0600 Subject: [PATCH 3/6] Update video library fix This is to stop music-only update from updating the video library for no reason. --- resources/lib/itemtypes.py | 5 ++++- resources/lib/librarysync.py | 24 +++++++++++++++++------- resources/lib/read_embyserver.py | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 71d81d5f..ae121021 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -73,6 +73,7 @@ class Items(object): 'Audio': Music } + update_videolibrary = False total = 0 for item in items: total += len(items[item]) @@ -108,6 +109,7 @@ class Items(object): # Music is not enabled, do not proceed with itemtype continue else: + update_videolibrary = True items_process = itemtypes[itemtype](embycursor, kodicursor) if itemtype == "Movie": @@ -202,10 +204,11 @@ class Items(object): if musicconn is not None: # close connection for special types + self.logMsg("Updating music database.", 1) musicconn.commit() musiccursor.close() - return True + return (True, update_videolibrary) def contentPop(self, name, time=5000): xbmcgui.Dialog().notification( diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 2a7697cd..b55ffba5 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -728,12 +728,14 @@ class LibrarySync(threading.Thread): emby = self.emby emby_db = embydb.Embydb_Functions(embycursor) pDialog = None + update_embydb = False if self.refresh_views: # Received userconfig update self.refresh_views = False self.maintainViews(embycursor, kodicursor) self.forceLibraryUpdate = True + update_embydb = True if self.addedItems or self.updateItems or self.userdataItems or self.removeItems: # Only present dialog if we are going to process items @@ -771,23 +773,31 @@ class LibrarySync(threading.Thread): sorted_items = emby.sortby_mediatype(items['Unsorted']) doupdate = items_process.itemsbyId(sorted_items, "added", pDialog) if doupdate: - update = True + embyupdate, kodiupdate_video = doupdate + if embyupdate: + update_embydb = True + if kodiupdate_video: + self.forceLibraryUpdate = True del items['Unsorted'] doupdate = items_process.itemsbyId(items, type, pDialog) if doupdate: - update = True - - if update: - self.forceLibraryUpdate = True + embyupdate, kodiupdate_video = doupdate + if embyupdate: + update_embydb = True + if kodiupdate_video: + self.forceLibraryUpdate = True + if update_embydb: + update_embydb = False + self.logMsg("Updating emby database.", 1) + embyconn.commit() + self.saveLastSync() if self.forceLibraryUpdate: # Force update the Kodi library self.forceLibraryUpdate = False self.dbCommit(kodiconn) - embyconn.commit() - self.saveLastSync() self.logMsg("Updating video library.", 1) utils.window('emby_kodiScan', value="true") diff --git a/resources/lib/read_embyserver.py b/resources/lib/read_embyserver.py index 3b105d54..e26c90e3 100644 --- a/resources/lib/read_embyserver.py +++ b/resources/lib/read_embyserver.py @@ -253,7 +253,7 @@ class Read_EmbyServer(): self.logMsg("Throttle activated.", 1) if jump == highestjump: - # We already adjusted to highestjump, but it failed. Reset value + # We already tried with the highestjump, but it failed. Reset value. self.logMsg("Reset highest value.", 1) highestjump = 0 From afcc92686ea27d3a2c681be51f798b37b226f5da Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Tue, 9 Feb 2016 22:55:28 -0600 Subject: [PATCH 4/6] Remove duplicate logging --- resources/lib/librarysync.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index b55ffba5..ecbb63cf 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -103,20 +103,18 @@ class LibrarySync(threading.Thread): lastSync = utils.settings('LastIncrementalSync') if not lastSync: lastSync = "2010-01-01T00:00:00Z" - self.logMsg("Last sync run: %s" % lastSync, 1) lastSyncTime = utils.convertdate(lastSync) - self.logMsg("LastIncrementalSync : %s" % lastSyncTime, 1) + self.logMsg("Last sync run: %s" % lastSyncTime, 1) # get server RetentionDateTime url = "{server}/Emby.Kodi.SyncQueue/GetServerDateTime?format=json" result = self.doUtils.downloadUrl(url) retention_time = "2010-01-01T00:00:00Z" if result and result.get('RetentionDateTime'): - self.logMsg("RetentionDateTime Found", 1) retention_time = result['RetentionDateTime'] retention_time = utils.convertdate(retention_time) - self.logMsg("RetentionDateTime : %s" % retention_time, 1) + self.logMsg("RetentionDateTime: %s" % retention_time, 1) # if last sync before retention time do a full sync if retention_time > lastSyncTime: From 4171cbff6980fdc665164d27fbd4fdafc99dd8a4 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Tue, 9 Feb 2016 23:07:35 -0600 Subject: [PATCH 5/6] Version bump 2.1.0 --- addon.xml | 2 +- changelog.txt | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/addon.xml b/addon.xml index c30d57e9..cdece919 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/changelog.txt b/changelog.txt index 14c2e746..e1149bd3 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +version 2.1.0 +- Add a throttle (automatically adjust the number of items requested at once) to prevent crashing during the initial sync +- Do not update the video library when there's a music-only update + version 2.0.3 - Add new retention time option that the latest server Sync plugin uses to help determine if full sync or inc sync should be used. - Add control over new content pop up display time. You will find the settings under Extras > Enable new content notification From 4ce9417771b1dacafd7c27d881bd29a450ba3a97 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Wed, 10 Feb 2016 01:07:52 -0600 Subject: [PATCH 6/6] Adjust server offline Prevent manual/repair sync from running when the server is not connected which causes errors. --- default.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/default.py b/default.py index 9932d758..91daef65 100644 --- a/default.py +++ b/default.py @@ -95,6 +95,14 @@ class Main: if mode == "settings": xbmc.executebuiltin('Addon.OpenSettings(plugin.video.emby)') elif mode in ("manualsync", "repair"): + if utils.window('emby_online') != "true": + # Server is not online, do not run the sync + xbmcgui.Dialog().ok(heading="Emby for Kodi", + line1=("Unable to run the sync, the add-on is not " + "connected to the Emby server.")) + utils.logMsg("EMBY", "Not connected to the emby server.", 1) + return + if utils.window('emby_dbScan') != "true": import librarysync lib = librarysync.LibrarySync()