Merge pull request #648 from croneter/user_fix

Fix TV sections not being deleted e.g. after user switch
This commit is contained in:
croneter 2019-01-30 18:27:12 +01:00 committed by GitHub
commit dfeba07ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 62 deletions

View file

@ -81,7 +81,7 @@ class MusicMixin(object):
self.remove_song(db_item['kodi_id'], db_item['kodi_pathid']) self.remove_song(db_item['kodi_id'], db_item['kodi_pathid'])
# Album verification # Album verification
if not self.plexdb.album_has_songs(db_item['album_id']): if not self.plexdb.album_has_songs(db_item['album_id']):
# No episode left for this season - so delete the season # No songleft for this album - so delete the album
self.remove_album(db_item['parent_id']) self.remove_album(db_item['parent_id'])
self.plexdb.remove(db_item['album_id'], v.PLEX_TYPE_ALBUM) self.plexdb.remove(db_item['album_id'], v.PLEX_TYPE_ALBUM)
# Artist verification # Artist verification
@ -91,8 +91,9 @@ class MusicMixin(object):
self.plexdb.remove(db_item['artist_id'], v.PLEX_TYPE_ARTIST) self.plexdb.remove(db_item['artist_id'], v.PLEX_TYPE_ARTIST)
# ALBUM ##### # ALBUM #####
elif db_item['plex_type'] == v.PLEX_TYPE_ALBUM: elif db_item['plex_type'] == v.PLEX_TYPE_ALBUM:
# Remove episodes, season, verify tvshow # Remove songs, album, verify artist
for song in self.plexdb.song_by_album(db_item['plex_id']): songs = list(self.plexdb.song_by_album(db_item['plex_id']))
for song in songs:
self.remove_song(song['kodi_id'], song['kodi_pathid']) self.remove_song(song['kodi_id'], song['kodi_pathid'])
self.plexdb.remove(song['plex_id'], v.PLEX_TYPE_SONG) self.plexdb.remove(song['plex_id'], v.PLEX_TYPE_SONG)
# Remove the album # Remove the album
@ -100,16 +101,18 @@ class MusicMixin(object):
# Show verification # Show verification
if (not self.plexdb.artist_has_albums(db_item['kodi_id']) and if (not self.plexdb.artist_has_albums(db_item['kodi_id']) and
not self.plexdb.artist_has_songs(db_item['kodi_id'])): not self.plexdb.artist_has_songs(db_item['kodi_id'])):
# There's no other season or episode left, delete the show # There's no other album or song left, delete the artist
self.remove_artist(db_item['parent_id']) self.remove_artist(db_item['parent_id'])
self.plexdb.remove(db_item['artist_id'], v.KODI_TYPE_ARTIST) self.plexdb.remove(db_item['artist_id'], v.KODI_TYPE_ARTIST)
# ARTIST ##### # ARTIST #####
elif db_item['plex_type'] == v.PLEX_TYPE_ARTIST: elif db_item['plex_type'] == v.PLEX_TYPE_ARTIST:
# Remove songs, albums and the artist himself # Remove songs, albums and the artist himself
for song in self.plexdb.song_by_artist(db_item['plex_id']): songs = list(self.plexdb.song_by_artist(db_item['plex_id']))
for song in songs:
self.remove_song(song['kodi_id'], song['kodi_pathid']) self.remove_song(song['kodi_id'], song['kodi_pathid'])
self.plexdb.remove(song['plex_id'], v.PLEX_TYPE_SONG) self.plexdb.remove(song['plex_id'], v.PLEX_TYPE_SONG)
for album in self.plexdb.album_by_artist(db_item['plex_id']): albums = list(self.plexdb.album_by_artist(db_item['plex_id']))
for album in albums:
self.remove_album(album['kodi_id']) self.remove_album(album['kodi_id'])
self.plexdb.remove(album['plex_id'], v.PLEX_TYPE_ALBUM) self.plexdb.remove(album['plex_id'], v.PLEX_TYPE_ALBUM)
self.remove_artist(db_item['kodi_id']) self.remove_artist(db_item['kodi_id'])

View file

@ -70,7 +70,8 @@ class TvShowMixin(object):
# SEASON ##### # SEASON #####
elif db_item['plex_type'] == v.PLEX_TYPE_SEASON: elif db_item['plex_type'] == v.PLEX_TYPE_SEASON:
# Remove episodes, season, verify tvshow # Remove episodes, season, verify tvshow
for episode in self.plexdb.episode_by_season(db_item['plex_id']): episodes = list(self.plexdb.episode_by_season(db_item['plex_id']))
for episode in episodes:
self.remove_episode(episode['kodi_id'], episode['kodi_fileid']) self.remove_episode(episode['kodi_id'], episode['kodi_fileid'])
self.plexdb.remove(episode['plex_id'], v.PLEX_TYPE_EPISODE) self.plexdb.remove(episode['plex_id'], v.PLEX_TYPE_EPISODE)
# Remove season # Remove season
@ -84,13 +85,15 @@ class TvShowMixin(object):
# TVSHOW ##### # TVSHOW #####
elif db_item['plex_type'] == v.PLEX_TYPE_SHOW: elif db_item['plex_type'] == v.PLEX_TYPE_SHOW:
# Remove episodes, seasons and the tvshow itself # Remove episodes, seasons and the tvshow itself
for episode in self.plexdb.episode_by_show(db_item['plex_id']): seasons = list(self.plexdb.season_by_show(db_item['plex_id']))
for season in seasons:
self.remove_season(season['kodi_id'])
self.plexdb.remove(season['plex_id'], v.PLEX_TYPE_SEASON)
episodes = list(self.plexdb.episode_by_show(db_item['plex_id']))
for episode in episodes:
self.remove_episode(episode['kodi_id'], self.remove_episode(episode['kodi_id'],
episode['kodi_fileid']) episode['kodi_fileid'])
self.plexdb.remove(episode['plex_id'], v.PLEX_TYPE_EPISODE) self.plexdb.remove(episode['plex_id'], v.PLEX_TYPE_EPISODE)
for season in self.plexdb.season_by_show(db_item['plex_id']):
self.remove_season(season['kodi_id'])
self.plexdb.remove(season['plex_id'], v.PLEX_TYPE_SEASON)
self.remove_show(db_item['kodi_id']) self.remove_show(db_item['kodi_id'])
LOG.debug('Deleted %s %s from all databases', LOG.debug('Deleted %s %s from all databases',

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}', with PlexDB() as plexdb:
message=utils.lang(30052), old_sections = [plexdb.section(x) for x in old_sections]
icon='{plex}',
sound=False)
video_library_update = False
music_library_update = False
with PlexDB() as plexdb:
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>"
if section[2] == v.KODI_TYPE_PHOTO: utils.dialog('notification',
# not synced heading='{plex}',
plexdb.remove_section(section[0]) message='%s %s' % (utils.lang(30052), section[1]),
continue icon='{plex}',
elif section[2] == v.KODI_TYPE_MOVIE: sound=False)
video_library_update = True if section[2] == v.PLEX_TYPE_PHOTO:
context = itemtypes.Movie(None, # not synced - just remove the link in our Plex sections table
plexdb=plexdb, pass
kodidb=kodidb) else:
elif section[2] == v.KODI_TYPE_SHOW: if not _delete_kodi_db_items(section[0], section[2]):
video_library_update = True return
context = itemtypes.Show(None, # Only remove Plex entry if we've removed all items first
plexdb=plexdb, with PlexDB() as 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]) 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.KODI_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

View file

@ -91,7 +91,7 @@ COMPANION_PORT = int(_ADDON.getSetting('companionPort'))
PKC_MACHINE_IDENTIFIER = None PKC_MACHINE_IDENTIFIER = None
# Minimal PKC version needed for the Kodi database - otherwise need to recreate # Minimal PKC version needed for the Kodi database - otherwise need to recreate
MIN_DB_VERSION = '2.5.12' MIN_DB_VERSION = '2.6.1'
# Supported databases # Supported databases
SUPPORTED_VIDEO_DB = { SUPPORTED_VIDEO_DB = {