From 86434bab3428f65dfcc2c2a7805abfc70e6af7a7 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Wed, 1 Feb 2017 12:16:35 +0100 Subject: [PATCH] Delete items of a removed Plex library --- resources/lib/librarysync.py | 34 +++++++++++++++++++++++++------ resources/lib/plexdb_functions.py | 16 +++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 8f80f2b9..1416bb98 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -847,17 +847,39 @@ class LibrarySync(Thread): # Save total window('Plex.nodes.total', str(totalnodes)) - # Reopen DB connection to ensure that changes were commited before + # Get rid of old items (view has been deleted on Plex side) + if self.old_views: + self.delete_views() + # update views for all: with plexdb.Get_Plex_DB() as plex_db: - log.info("Removing views: %s" % self.old_views) - for view in self.old_views: - plex_db.removeView(view) - # update views for all: self.views = plex_db.getAllViewInfo() - log.info("Finished processing views. Views saved: %s" % self.views) return True + def delete_views(self): + log.info("Removing views: %s" % self.old_views) + delete_items = [] + with plexdb.Get_Plex_DB() as plex_db: + for view in self.old_views: + plex_db.removeView(view) + delete_items.extend(plex_db.get_items_by_viewid(view)) + # All video removals are the same, so pick Movies() class + with itemtypes.Movies() as movie: + for item in (i for i in delete_items + if i['kodi_type'] == v.KODI_TYPE_MOVIE): + movie.remove(item['plex_id']) + with itemtypes.TVShows() as tv: + for item in (i for i in delete_items + if i['kodi_type'] in (v.KODI_TYPE_EPISODE, + v.KODI_TYPE_SEASON, + v.KODI_TYPE_SHOW)): + tv.remove(item['plex_id']) + # And for the music DB: + with itemtypes.Music() as music: + for item in (i for i in delete_items + if i['kodi_type'] in v.KODI_AUDIOTYPES): + music.remove(item['plex_id']) + def GetUpdatelist(self, xml, itemType, method, viewName, viewId): """ THIS METHOD NEEDS TO BE FAST! => e.g. no API calls diff --git a/resources/lib/plexdb_functions.py b/resources/lib/plexdb_functions.py index 6406e7fd..cd8f34e6 100644 --- a/resources/lib/plexdb_functions.py +++ b/resources/lib/plexdb_functions.py @@ -159,6 +159,22 @@ class Plex_DB_Functions(): ''' self.plexcursor.execute(query, (view_id,)) + def get_items_by_viewid(self, view_id): + """ + Returns a list for view_id with one item like this: + { + 'plex_id': xxx + 'kodi_type': xxx + } + """ + query = '''SELECT plex_id, kodi_type FROM plex WHERE view_id = ?''' + self.plexcursor.execute(query, (view_id, )) + rows = self.plexcursor.fetchall() + res = [] + for row in rows: + res.append({'plex_id': row[0], 'kodi_type': row[1]}) + return res + def getItem_byFileId(self, kodi_fileid, kodi_type): """ Returns plex_id for kodi_fileid and kodi_type