diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index db02a07b..665fa074 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -386,11 +386,8 @@ def getThemeMedia(): return # Get every user view Id - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - emby_db = embydb.Embydb_Functions(embycursor) - viewids = emby_db.getViews() - embycursor.close() + with utils.GetEmbyDB() as emby_db: + viewids = emby_db.getViews() # Get Ids with Theme Videos itemIds = {} diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 9d8814bc..7db4475f 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -72,10 +72,6 @@ class ThreadedGetMetadata(threading.Thread): updateItem['XML'] = plexXML # place item into out queue out_queue.put(updateItem) - del plexXML - del updateItem - # If we don't have a valid XML, don't put that into the queue - # but skip this item for now # Keep track of where we are at with lock: getMetadataCount += 1 @@ -139,7 +135,7 @@ class ThreadedProcessMetadata(threading.Thread): del plexitem del updateItem # signals to queue job is done - self.queue.task_done() + queue.task_done() @utils.ThreadMethodsAdditionalStop('emby_shouldStop') @@ -278,17 +274,16 @@ class LibrarySync(threading.Thread): self.saveLastSync() # Get all PMS items already saved in Kodi - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - emby_db = embydb.Embydb_Functions(embycursor) # Also get checksums of every Plex items already saved in Kodi allKodiElementsId = {} - for itemtype in PlexFunctions.EmbyItemtypes(): - try: - allKodiElementsId.update(dict(emby_db.getChecksum(itemtype))) - except ValueError: - pass - embyconn.close() + with utils.GetEmbyDB() as emby_db: + for itemtype in PlexFunctions.EmbyItemtypes(): + try: + allKodiElementsId.update( + dict(emby_db.getChecksum(itemtype))) + except ValueError: + pass + self.allKodiElementsId = allKodiElementsId # Run through views and get latest changed elements using time diff @@ -310,7 +305,8 @@ class LibrarySync(threading.Thread): PlexFunctions.GetItemClassFromType(plexType), PlexFunctions.GetMethodFromPlexType(plexType), view['name'], - view['id']) + view['id'], + dontCheck=True) # Process self.updatelist if self.updatelist: if self.updatelist[0]['itemType'] in ['Movies', 'TVShows']: @@ -573,7 +569,8 @@ class LibrarySync(threading.Thread): embyconn.close() kodiconn.close() - def GetUpdatelist(self, xml, itemType, method, viewName, viewId): + def GetUpdatelist(self, xml, itemType, method, viewName, viewId, + dontCheck=False): """ Adds items to self.updatelist as well as self.allPlexElementsId dict @@ -584,6 +581,8 @@ class LibrarySync(threading.Thread): see itemtypes.py viewName: Name of the Plex view (e.g. 'My TV shows') viewId: Id/Key of Plex library (e.g. '1') + dontCheck: If True, skips checksum check but assumes + that all items in xml must be processed Output: self.updatelist, self.allPlexElementsId self.updatelist APPENDED(!!) list itemids (Plex Keys as @@ -599,16 +598,16 @@ class LibrarySync(threading.Thread): self.allPlexElementsId APPENDED(!!) dict = {itemid: checksum} """ - if self.compare: + if self.compare or not dontCheck: # Manual sync for item in xml: # Skipping items 'title=All episodes' without a 'ratingKey' if not item.attrib.get('ratingKey', False): continue API = PlexAPI.API(item) - plex_checksum = API.getChecksum() itemId = API.getRatingKey() title, sorttitle = API.getTitle() + plex_checksum = API.getChecksum() self.allPlexElementsId[itemId] = plex_checksum kodi_checksum = self.allKodiElementsId.get(itemId) if kodi_checksum != plex_checksum: @@ -637,7 +636,6 @@ class LibrarySync(threading.Thread): 'viewName': viewName, 'viewId': viewId, 'title': title}) - return def GetAndProcessXMLs(self, itemType): """ @@ -728,27 +726,20 @@ class LibrarySync(threading.Thread): # Initialize self.allPlexElementsId = {} - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - - emby_db = embydb.Embydb_Functions(embycursor) itemType = 'Movies' views = [x for x in self.views if x['itemtype'] == 'movie'] self.logMsg("Processing Plex %s. Libraries: %s" % (itemType, views), 1) + self.allKodiElementsId = {} if self.compare: - # Get movies from Plex server - emby_db = embydb.Embydb_Functions(embycursor) - # Pull the list of movies and boxsets in Kodi - try: - self.allKodiElementsId = dict(emby_db.getChecksum('Movie')) - except ValueError: - self.allKodiElementsId = {} - else: - # Getting all metadata, hence set Kodi elements to {} - self.allKodiElementsId = {} - embyconn.close() + with utils.GetEmbyDB() as emby_db: + # Get movies from Plex server + # Pull the list of movies and boxsets in Kodi + try: + self.allKodiElementsId = dict(emby_db.getChecksum('Movie')) + except ValueError: + self.allKodiElementsId = {} ##### PROCESS MOVIES ##### self.updatelist = [] @@ -853,37 +844,32 @@ class LibrarySync(threading.Thread): # Initialize self.allPlexElementsId = {} itemType = 'TVShows' - # Open DB connections - embyconn = utils.kodiSQL('emby') - embycursor = embyconn.cursor() - emby_db = embydb.Embydb_Functions(embycursor) views = [x for x in self.views if x['itemtype'] == 'show'] self.logMsg("Media folders for %s: %s" % (itemType, views), 1) self.allKodiElementsId = {} if self.compare: - # Get movies from Plex server - # Pull the list of TV shows already in Kodi - try: - all_koditvshows = dict(emby_db.getChecksum('Series')) - self.allKodiElementsId.update(all_koditvshows) - except ValueError: - pass - # Same for seasons - try: - all_kodiseasons = dict(emby_db.getChecksum('Season')) - self.allKodiElementsId.update(all_kodiseasons) - except ValueError: - pass - # Same for the episodes (sub-element of shows/series) - try: - all_kodiepisodes = dict(emby_db.getChecksum('Episode')) - self.allKodiElementsId.update(all_kodiepisodes) - except ValueError: - pass - # Close DB connections - embyconn.close() + with utils.GetEmbyDB() as emby_db: + # Get movies from Plex server + # Pull the list of TV shows already in Kodi + try: + all_koditvshows = dict(emby_db.getChecksum('Series')) + self.allKodiElementsId.update(all_koditvshows) + except ValueError: + pass + # Same for seasons + try: + all_kodiseasons = dict(emby_db.getChecksum('Season')) + self.allKodiElementsId.update(all_kodiseasons) + except ValueError: + pass + # Same for the episodes (sub-element of shows/series) + try: + all_kodiepisodes = dict(emby_db.getChecksum('Episode')) + self.allKodiElementsId.update(all_kodiepisodes) + except ValueError: + pass ##### PROCESS TV Shows ##### self.updatelist = [] diff --git a/resources/lib/utils.py b/resources/lib/utils.py index bd8cc150..aa39b46e 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -19,12 +19,32 @@ import xbmcaddon import xbmcgui import xbmcvfs +import embydb_functions as embydb + ################################################################################################# addonName = xbmcaddon.Addon().getAddonInfo('name') +class GetEmbyDB(): + """ + Usage: with GetEmbyDB() as emby_db: + do stuff with emby_db + + On exiting "with" (no matter what), commits get automatically committed + and the db gets closed + """ + def __enter__(self): + self.embyconn = kodiSQL('emby') + self.emby_db = embydb.Embydb_Functions(self.embyconn.cursor()) + return self.emby_db + + def __exit__(self, type, value, traceback): + self.embyconn.commit() + self.embyconn.close() + + def LogTime(func): """ Decorator for functions and methods to log the time it took to run the code