Fix deleting entire Plex library sections

Typo plex_type - section_type
This commit is contained in:
croneter 2019-01-30 14:34:35 +01:00
parent dcff85e203
commit a279cf5198
2 changed files with 61 additions and 50 deletions

View file

@ -13,12 +13,17 @@ from .. import plex_functions as PF, music, utils, variables as v, app
LOG = getLogger('PLEX.sync.sections') LOG = getLogger('PLEX.sync.sections')
BATCH_SIZE = 200
VNODES = videonodes.VideoNodes() VNODES = videonodes.VideoNodes()
PLAYLISTS = {} PLAYLISTS = {}
NODES = {} NODES = {}
SECTIONS = [] SECTIONS = []
def isCanceled():
return app.APP.stop_pkc or app.APP.suspend_threads or app.SYNC.stop_sync
def sync_from_pms(): def sync_from_pms():
""" """
Sync the Plex library sections Sync the Plex library sections
@ -198,56 +203,61 @@ def _process_section(section_xml, kodidb, plexdb, sorted_sections,
return totalnodes return totalnodes
def _delete_kodi_db_items(section_id, section_type):
if section_type == v.PLEX_TYPE_MOVIE:
kodi_context = kodi_db.KodiVideoDB
types = ((v.PLEX_TYPE_MOVIE, itemtypes.Movie), )
elif section_type == v.PLEX_TYPE_SHOW:
kodi_context = kodi_db.KodiVideoDB
types = ((v.PLEX_TYPE_SHOW, itemtypes.Show),
(v.PLEX_TYPE_SEASON, itemtypes.Season),
(v.PLEX_TYPE_EPISODE, itemtypes.Episode))
elif section_type == v.PLEX_TYPE_ARTIST:
kodi_context = kodi_db.KodiMusicDB
types = ((v.PLEX_TYPE_ARTIST, itemtypes.Artist),
(v.PLEX_TYPE_ALBUM, itemtypes.Album),
(v.PLEX_TYPE_SONG, itemtypes.Song))
for plex_type, context in types:
while True:
with PlexDB() as plexdb:
plex_ids = list(plexdb.plexid_by_sectionid(section_id,
plex_type,
BATCH_SIZE))
with kodi_context(texture_db=True) as kodidb:
typus = context(None, plexdb=plexdb, kodidb=kodidb)
for plex_id in plex_ids:
if isCanceled():
return False
typus.remove(plex_id)
if len(plex_ids) < BATCH_SIZE:
break
return True
def delete_sections(old_sections): def delete_sections(old_sections):
""" """
Deletes all elements for a Plex section that has been deleted. (e.g. all Deletes all elements for a Plex section that has been deleted. (e.g. all
TV shows, Seasons and Episodes of a Show section) TV shows, Seasons and Episodes of a Show section)
""" """
utils.dialog('notification', try:
heading='{plex}',
message=utils.lang(30052),
icon='{plex}',
sound=False)
video_library_update = False
music_library_update = False
with PlexDB() as plexdb: with PlexDB() as plexdb:
old_sections = [plexdb.section(x) for x in old_sections] old_sections = [plexdb.section(x) for x in old_sections]
LOG.info("Removing entire Plex library sections: %s", old_sections) LOG.info("Removing entire Plex library sections: %s", old_sections)
with kodi_db.KodiVideoDB(texture_db=True) as kodidb:
for section in old_sections: for section in old_sections:
# "Deleting <section_name>"
utils.dialog('notification',
heading='{plex}',
message='%s %s' % (utils.lang(30052), section[1]),
icon='{plex}',
sound=False)
if section[2] == v.PLEX_TYPE_PHOTO: if section[2] == v.PLEX_TYPE_PHOTO:
# not synced # not synced - just remove the link in our Plex sections table
plexdb.remove_section(section[0]) pass
continue
elif section[2] == v.PLEX_TYPE_MOVIE:
video_library_update = True
context = itemtypes.Movie(None,
plexdb=plexdb,
kodidb=kodidb)
elif section[2] == v.PLEX_TYPE_SHOW:
video_library_update = True
context = itemtypes.Show(None,
plexdb=plexdb,
kodidb=kodidb)
else: else:
continue if not _delete_kodi_db_items(section[0], section[2]):
for plex_id in plexdb.plexid_by_sectionid(section[0], section[2]): return
context.remove(plex_id)
# Only remove Plex entry if we've removed all items first # Only remove Plex entry if we've removed all items first
with PlexDB() as plexdb:
plexdb.remove_section(section[0]) plexdb.remove_section(section[0])
finally:
with kodi_db.KodiMusicDB(texture_db=True) as kodidb: common.update_kodi_library()
for section in old_sections:
if section[2] == v.PLEX_TYPE_ARTIST:
music_library_update = True
context = itemtypes.Artist(None,
plexdb=plexdb,
kodidb=kodidb)
else:
continue
for plex_id in plexdb.plexid_by_sectionid(section[0], section[2]):
context.remove(plex_id)
# Only remove Plex entry if we've removed all items first
plexdb.remove_section(section[0])
common.update_kodi_library(video=video_library_update,
music=music_library_update)

View file

@ -162,10 +162,11 @@ class PlexDBBase(object):
self.cursor.execute('UPDATE %s SET fanart_synced = 1 WHERE plex_id = ?' % plex_type, self.cursor.execute('UPDATE %s SET fanart_synced = 1 WHERE plex_id = ?' % plex_type,
(plex_id, )) (plex_id, ))
def plexid_by_sectionid(self, section_id, plex_type): def plexid_by_sectionid(self, section_id, plex_type, limit):
return (x[0] for x in query = '''
self.cursor.execute('SELECT plex_id FROM %s WHERE section_id = ?' % plex_type, SELECT plex_id FROM %s WHERE section_id = ? LIMIT %s
(section_id, ))) ''' % (plex_type, limit)
return (x[0] for x in self.cursor.execute(query, (section_id, )))
def kodiid_by_sectionid(self, section_id, plex_type): def kodiid_by_sectionid(self, section_id, plex_type):
return (x[0] for x in return (x[0] for x in