Hopefully fix endless playlist sync loops

This commit is contained in:
croneter 2018-09-23 15:06:41 +02:00
parent 21fb1ad015
commit 5fabaf6a8e

View file

@ -34,8 +34,11 @@ SUPPORTED_FILETYPES = (
# 'pls', # 'pls',
# 'cue', # 'cue',
) )
# Avoid endless loops # Avoid endless loops. Store Plex IDs for creating, Kodi paths for deleting!
IGNORE_KODI_PLAYLIST_CHANGE = list() IGNORE_KODI_PLAYLIST_CHANGE = list()
# Used for updating Plex playlists due to Kodi changes - Plex playlist
# will have to be deleted first. Add Plex ids!
IGNORE_PLEX_PLAYLIST_CHANGE = list()
############################################################################### ###############################################################################
@ -91,6 +94,11 @@ def websocket(plex_id, status):
create = False create = False
with state.LOCK_PLAYLISTS: with state.LOCK_PLAYLISTS:
playlist = db.get_playlist(plex_id=plex_id) playlist = db.get_playlist(plex_id=plex_id)
if plex_id in IGNORE_PLEX_PLAYLIST_CHANGE:
LOG.debug('Ignoring detected Plex playlist change for %s',
playlist)
IGNORE_PLEX_PLAYLIST_CHANGE.remove(plex_id)
return
if playlist and status == 9: if playlist and status == 9:
# Won't be able to download metadata of the deleted playlist # Won't be able to download metadata of the deleted playlist
if sync_plex_playlist(playlist=playlist): if sync_plex_playlist(playlist=playlist):
@ -169,32 +177,42 @@ def _full_sync():
if not sync_plex_playlist(xml=xml_playlist): if not sync_plex_playlist(xml=xml_playlist):
continue continue
playlist = db.get_playlist(plex_id=api.plex_id()) playlist = db.get_playlist(plex_id=api.plex_id())
try:
if not playlist: if not playlist:
LOG.debug('New Plex playlist %s discovered: %s', LOG.debug('New Plex playlist %s discovered: %s',
api.plex_id(), api.title()) api.plex_id(), api.title())
IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id())
try:
kodi_pl.create(api.plex_id()) kodi_pl.create(api.plex_id())
except PlaylistError:
LOG.info('Skipping creation of playlist %s', api.plex_id())
IGNORE_KODI_PLAYLIST_CHANGE.remove(api.plex_id())
elif playlist.plex_updatedat != api.updated_at(): elif playlist.plex_updatedat != api.updated_at():
LOG.debug('Detected changed Plex playlist %s: %s', LOG.debug('Detected changed Plex playlist %s: %s',
api.plex_id(), api.title()) api.plex_id(), api.title())
IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) # Since we are DELETING a playlist, we need to catch with path!
IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.kodi_path)
try:
kodi_pl.delete(playlist) kodi_pl.delete(playlist)
except PlaylistError:
LOG.info('Skipping recreation of playlist %s', api.plex_id())
IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.kodi_path)
else:
IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id())
try:
kodi_pl.create(api.plex_id()) kodi_pl.create(api.plex_id())
except PlaylistError: except PlaylistError:
LOG.info('Skipping playlist %s: %s', api.plex_id(), api.title()) LOG.info('Could not recreate playlist %s', api.plex_id())
IGNORE_KODI_PLAYLIST_CHANGE.remove(api.plex_id()) IGNORE_KODI_PLAYLIST_CHANGE.remove(api.plex_id())
# Get rid of old Plex playlists that were deleted on the Plex side # Get rid of old Plex playlists that were deleted on the Plex side
for plex_id in old_plex_ids: for plex_id in old_plex_ids:
playlist = db.get_playlist(plex_id=plex_id) playlist = db.get_playlist(plex_id=plex_id)
LOG.debug('Removing outdated Plex playlist: %s', playlist) IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.kodi_path)
LOG.debug('Removing outdated Plex playlist from Kodi: %s', playlist)
try: try:
IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.plex_id)
kodi_pl.delete(playlist) kodi_pl.delete(playlist)
except PlaylistError: except PlaylistError:
LOG.debug('Skipping deletion of playlist: %s', playlist) LOG.debug('Skipping deletion of playlist: %s', playlist)
IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.plex_id) IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.kodi_path)
# Look at all supported Kodi playlists. Check whether they are in the DB. # Look at all supported Kodi playlists. Check whether they are in the DB.
old_kodi_paths = db.kodi_playlist_paths() old_kodi_paths = db.kodi_playlist_paths()
for root, _, files in path_ops.walk(v.PLAYLIST_PATH): for root, _, files in path_ops.walk(v.PLAYLIST_PATH):
@ -210,17 +228,21 @@ def _full_sync():
playlist = db.get_playlist(path=path) playlist = db.get_playlist(path=path)
if playlist and playlist.kodi_hash == kodi_hash: if playlist and playlist.kodi_hash == kodi_hash:
continue continue
try:
if not playlist: if not playlist:
LOG.debug('New Kodi playlist detected: %s', path) LOG.debug('New Kodi playlist detected: %s', path)
playlist = Playlist() playlist = Playlist()
playlist.kodi_path = path playlist.kodi_path = path
playlist.kodi_hash = kodi_hash playlist.kodi_hash = kodi_hash
try:
plex_pl.create(playlist) plex_pl.create(playlist)
except PlaylistError:
LOG.info('Skipping Kodi playlist %s', path)
else: else:
LOG.debug('Changed Kodi playlist detected: %s', path) LOG.debug('Changed Kodi playlist detected: %s', path)
IGNORE_PLEX_PLAYLIST_CHANGE.append(playlist.plex_id)
plex_pl.delete(playlist) plex_pl.delete(playlist)
playlist.kodi_hash = kodi_hash playlist.kodi_hash = kodi_hash
try:
plex_pl.create(playlist) plex_pl.create(playlist)
except PlaylistError: except PlaylistError:
LOG.info('Skipping Kodi playlist %s', path) LOG.info('Skipping Kodi playlist %s', path)
@ -229,7 +251,7 @@ def _full_sync():
try: try:
plex_pl.delete(playlist) plex_pl.delete(playlist)
except PlaylistError: except PlaylistError:
LOG.debug('Skipping deletion of playlist: %s', playlist) LOG.debug('Skipping deletion of Plex playlist: %s', playlist)
LOG.info('Playlist full sync done') LOG.info('Playlist full sync done')
return True return True
@ -354,6 +376,11 @@ class PlaylistEventhandler(events.FileSystemEventHandler):
LOG.debug('Ignoring event %s for playlist %s', event, playlist) LOG.debug('Ignoring event %s for playlist %s', event, playlist)
IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.plex_id) IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.plex_id)
return return
if not playlist and path in IGNORE_KODI_PLAYLIST_CHANGE:
LOG.debug('Ignoring deletion event %s for playlist %s',
event, playlist)
IGNORE_KODI_PLAYLIST_CHANGE.remove(path)
return
_method_map = { _method_map = {
events.EVENT_TYPE_MODIFIED: self.on_modified, events.EVENT_TYPE_MODIFIED: self.on_modified,
events.EVENT_TYPE_MOVED: self.on_moved, events.EVENT_TYPE_MOVED: self.on_moved,