diff --git a/resources/lib/playlists/__init__.py b/resources/lib/playlists/__init__.py index 1f89a8ae..da5df27d 100644 --- a/resources/lib/playlists/__init__.py +++ b/resources/lib/playlists/__init__.py @@ -40,6 +40,8 @@ SUPPORTED_FILETYPES = ( # 'pls', # 'cue', ) +# Avoid endless loops +IGNORE_KODI_PLAYLIST_CHANGE = list() ############################################################################### @@ -70,9 +72,10 @@ def websocket(plex_id, status): if sync_plex_playlist(playlist=playlist): LOG.debug('Plex deletion of playlist detected: %s', playlist) try: + IGNORE_KODI_PLAYLIST_CHANGE.append(plex_id) kodi_pl.delete(playlist) except PlaylistError: - pass + IGNORE_KODI_PLAYLIST_CHANGE.remove(plex_id) return xml = pms.metadata(plex_id) if xml is None: @@ -90,6 +93,7 @@ def websocket(plex_id, status): else: LOG.debug('Change of Plex playlist detected: %s', playlist) + IGNORE_KODI_PLAYLIST_CHANGE.append(plex_id) kodi_pl.delete(playlist) create = True elif not playlist and not status == 9: @@ -98,9 +102,10 @@ def websocket(plex_id, status): create = True # To the actual work if create: + IGNORE_KODI_PLAYLIST_CHANGE.append(plex_id) kodi_pl.create(plex_id) except PlaylistError: - pass + IGNORE_KODI_PLAYLIST_CHANGE.remove(plex_id) def full_sync(): @@ -140,22 +145,28 @@ def _full_sync(): if not playlist: LOG.debug('New Plex playlist %s discovered: %s', api.plex_id(), api.title()) + IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) kodi_pl.create(api.plex_id()) elif playlist.plex_updatedat != api.updated_at(): LOG.debug('Detected changed Plex playlist %s: %s', api.plex_id(), api.title()) + IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) kodi_pl.delete(playlist) + IGNORE_KODI_PLAYLIST_CHANGE.append(api.plex_id()) kodi_pl.create(api.plex_id()) except PlaylistError: LOG.info('Skipping playlist %s: %s', api.plex_id(), api.title()) + IGNORE_KODI_PLAYLIST_CHANGE.remove(api.plex_id()) # Get rid of old Plex playlists that were deleted on the Plex side for plex_id in old_plex_ids: playlist = db.get_playlist(plex_id=plex_id) LOG.debug('Removing outdated Plex playlist: %s', playlist) try: + IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.plex_id) kodi_pl.delete(playlist) except PlaylistError: LOG.debug('Skipping deletion of playlist: %s', playlist) + IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.plex_id) # Look at all supported Kodi playlists. Check whether they are in the DB. old_kodi_paths = db.kodi_playlist_paths() for root, _, files in path_ops.walk(v.PLAYLIST_PATH): @@ -275,6 +286,11 @@ class PlaylistEventhandler(events.FileSystemEventHandler): else event.src_path if not sync_kodi_playlist(path): return + playlist = db.get_playlist(path=path) + if playlist and playlist.plex_id in IGNORE_KODI_PLAYLIST_CHANGE: + LOG.debug('Ignoring event %s for playlist %s', event, playlist) + IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.plex_id) + return _method_map = { events.EVENT_TYPE_MODIFIED: self.on_modified, events.EVENT_TYPE_MOVED: self.on_moved, diff --git a/resources/lib/playlists/kodi_pl.py b/resources/lib/playlists/kodi_pl.py index 9b90bf83..41a0c146 100644 --- a/resources/lib/playlists/kodi_pl.py +++ b/resources/lib/playlists/kodi_pl.py @@ -77,6 +77,7 @@ def delete(playlist): if path_ops.exists(playlist.kodi_path): try: path_ops.remove(playlist.kodi_path) + LOG.debug('Deleted Kodi playlist: %s', playlist) except (OSError, IOError) as err: LOG.error('Could not delete Kodi playlist file %s. Error:\n%s: %s', playlist, err.errno, err.strerror)