From 84f7aba5d1c3a74326e7af35955e92eb1450a66a Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sun, 26 Feb 2017 18:04:54 +0100 Subject: [PATCH] Try to skip new PMS items we've already processed --- resources/lib/librarysync.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 6c95b56d..8de3a96a 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -321,6 +321,11 @@ class LibrarySync(Thread): def __init__(self, callback=None): self.mgr = callback + # Dict of items we just processed in order to prevent a reprocessing + # caused by websocket + self.just_processed = {} + # How long do we wait until we start re-processing? (in seconds) + self.ignore_just_processed = 10*60 self.itemsToProcess = [] self.sessionKeys = [] self.fanartqueue = Queue.Queue() @@ -532,6 +537,9 @@ class LibrarySync(Thread): # True: we're syncing only the delta, e.g. different checksum self.compare = not repair + # Empty our list of item's we've just processed in the past + self.just_processed = {} + self.new_items_only = True # This will also update playstates and userratings! log.info('Running fullsync for NEW PMS items with repair=%s' % repair) @@ -884,6 +892,7 @@ class LibrarySync(Thread): self.allPlexElementsId APPENDED(!!) dict = {itemid: checksum} """ + now = getUnixTimestamp() if self.new_items_only is True: # Only process Plex items that Kodi does not already have in lib for item in xml: @@ -903,6 +912,7 @@ class LibrarySync(Thread): 'title': item.attrib.get('title', 'Missing Title'), 'mediaType': item.attrib.get('type') }) + self.just_processed[itemId] = now return if self.compare: @@ -928,6 +938,7 @@ class LibrarySync(Thread): 'title': item.attrib.get('title', 'Missing Title'), 'mediaType': item.attrib.get('type') }) + self.just_processed[itemId] = now else: # Initial or repair sync: get all Plex movies for item in xml: @@ -946,6 +957,7 @@ class LibrarySync(Thread): 'title': item.attrib.get('title', 'Missing Title'), 'mediaType': item.attrib.get('type') }) + self.just_processed[itemId] = now def GetAndProcessXMLs(self, itemType): """ @@ -1450,6 +1462,8 @@ class LibrarySync(Thread): continue else: successful = self.process_newitems(item) + if successful: + self.just_processed[str(item['ratingKey'])] = now if successful and settings('FanartTV') == 'true': plex_type = v.PLEX_TYPE_FROM_WEBSOCKET[item['type']] if plex_type in (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW): @@ -1534,6 +1548,7 @@ class LibrarySync(Thread): PMS is messing with the library items, e.g. new or changed. Put in our "processing queue" for later """ + now = getUnixTimestamp() for item in data: if 'tv.plex' in item.get('identifier', ''): # Ommit Plex DVR messages - the Plex IDs are not corresponding @@ -1548,6 +1563,14 @@ class LibrarySync(Thread): if plex_id == '0': log.error('Received malformed PMS message: %s' % item) continue + try: + if (now - self.just_processed[plex_id] < + self.ignore_just_processed and state != 9): + log.debug('We just processed %s: ignoring' % plex_id) + continue + except KeyError: + # Item has NOT just been processed + pass # Have we already added this element? for existingItem in self.itemsToProcess: if existingItem['ratingKey'] == plex_id: