From a83cf931507c83825b590cc85c9f3fddfd62da25 Mon Sep 17 00:00:00 2001 From: croneter Date: Thu, 1 Aug 2019 14:21:45 +0200 Subject: [PATCH] Separate function to wipe all synched Plex playlists --- resources/lib/plex_db/common.py | 12 +++++++---- resources/lib/utils.py | 37 ++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/resources/lib/plex_db/common.py b/resources/lib/plex_db/common.py index 70361feb..c2764cad 100644 --- a/resources/lib/plex_db/common.py +++ b/resources/lib/plex_db/common.py @@ -311,12 +311,16 @@ def initialize(): plexdb.cursor.execute(cmd) -def wipe(): +def wipe(table=None): """ - Completely resets the Plex database + Completely resets the Plex database. + If a table [unicode] name is provided, only that table will be dropped """ with PlexDBBase() as plexdb: - plexdb.cursor.execute("SELECT name FROM sqlite_master WHERE type = 'table'") - tables = [i[0] for i in plexdb.cursor.fetchall()] + if table: + tables = [table] + else: + plexdb.cursor.execute("SELECT name FROM sqlite_master WHERE type = 'table'") + tables = [i[0] for i in plexdb.cursor.fetchall()] for table in tables: plexdb.cursor.execute('DROP table IF EXISTS %s' % table) diff --git a/resources/lib/utils.py b/resources/lib/utils.py index be8aadcc..b1f8ca83 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -539,6 +539,29 @@ def create_kodi_db_indicees(): conn.close() +def wipe_synched_playlists(): + """ + Deletes all synched playlist files on the Kodi side; resets the Plex table + listing all synched Plex playlists + """ + from . import plex_db + try: + with plex_db.PlexDB() as plexdb: + plexdb.cursor.execute('SELECT kodi_path FROM playlists') + playlist_paths = [x[0] for x in plexdb.cursor] + except OperationalError: + # Plex DB completely empty yet + playlist_paths = [] + for path in playlist_paths: + try: + path_ops.remove(path) + LOG.info('Removed playlist %s', path) + except (OSError, IOError): + LOG.warn('Could not remove playlist %s', path) + # Now wipe our database + plex_db.wipe(table='playlists') + + def wipe_database(reboot=True): """ Deletes all Plex playlists as well as video nodes, then clears Kodi as well @@ -550,8 +573,8 @@ def wipe_database(reboot=True): from . import kodi_db, plex_db # Clean up the playlists and video nodes delete_files() - # First get the paths to all synced playlists - playlist_paths = [] + # Wipe all synched playlists + wipe_synched_playlists() try: with plex_db.PlexDB() as plexdb: if plexdb.songs_have_been_synced(): @@ -560,22 +583,12 @@ def wipe_database(reboot=True): else: LOG.info('No music has been synced in the past - not wiping') music = False - plexdb.cursor.execute('SELECT kodi_path FROM playlists') - for entry in plexdb.cursor: - playlist_paths.append(entry[0]) except OperationalError: # Plex DB completely empty yet. Wipe existing Kodi music only if we # expect to sync Plex music music = settings('enableMusic') == 'true' kodi_db.wipe_dbs(music) plex_db.wipe() - # Delete all synced playlists - for path in playlist_paths: - try: - path_ops.remove(path) - LOG.debug('Removed playlist %s', path) - except (OSError, IOError): - LOG.warn('Could not remove playlist %s', path) LOG.info("Resetting all cached artwork.") # Remove all cached artwork