diff --git a/resources/lib/playlists/__init__.py b/resources/lib/playlists/__init__.py index 601879dc..231b1865 100644 --- a/resources/lib/playlists/__init__.py +++ b/resources/lib/playlists/__init__.py @@ -34,8 +34,6 @@ SUPPORTED_FILETYPES = ( # 'pls', # 'cue', ) -# Avoid endless loops. Store Plex IDs for creating, Kodi paths for deleting! -IGNORE_KODI_PLAYLIST_CHANGE = list() ############################################################################### @@ -106,10 +104,9 @@ 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: - IGNORE_KODI_PLAYLIST_CHANGE.remove(plex_id) + pass return xml = pms.metadata(plex_id) if xml is None: @@ -127,7 +124,6 @@ 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: @@ -136,10 +132,9 @@ 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: - IGNORE_KODI_PLAYLIST_CHANGE.remove(plex_id) + pass def full_sync(): @@ -184,41 +179,33 @@ 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()) try: 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(): LOG.debug('Detected changed Plex playlist %s: %s', api.plex_id(), api.title()) # 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) 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()) try: kodi_pl.create(api.plex_id()) except PlaylistError: LOG.info('Could not recreate playlist %s', api.plex_id()) - 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: if isCanceled(): return False playlist = db.get_playlist(plex_id=plex_id) - IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.kodi_path) LOG.debug('Removing outdated Plex playlist from Kodi: %s', playlist) try: kodi_pl.delete(playlist) except PlaylistError: LOG.debug('Skipping deletion of playlist: %s', playlist) - IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.kodi_path) # 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): @@ -382,15 +369,9 @@ 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 - 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) + if path in kodi_pl.IGNORE_KODI_PLAYLIST_CHANGE: + LOG.debug('Ignoring event %s', event) + kodi_pl.IGNORE_KODI_PLAYLIST_CHANGE.remove(path) return _method_map = { events.EVENT_TYPE_MODIFIED: self.on_modified, diff --git a/resources/lib/playlists/kodi_pl.py b/resources/lib/playlists/kodi_pl.py index 2e6a8ac8..f9413144 100644 --- a/resources/lib/playlists/kodi_pl.py +++ b/resources/lib/playlists/kodi_pl.py @@ -14,10 +14,10 @@ from ..plex_api import API from .. import utils, path_ops, variables as v ############################################################################### LOG = getLogger('PLEX.playlists.kodi_pl') - -############################################################################### - REGEX_FILE_NUMBERING = re.compile(r'''_(\d\d)\.\w+$''') +# Avoid endless loops. Store the Kodi paths +IGNORE_KODI_PLAYLIST_CHANGE = list() +############################################################################### def create(plex_id): @@ -65,7 +65,12 @@ def create(plex_id): if xml_playlist is None: LOG.error('Could not get Plex playlist %s', plex_id) raise PlaylistError('Could not get Plex playlist %s' % plex_id) - _write_playlist_to_file(playlist, xml_playlist) + IGNORE_KODI_PLAYLIST_CHANGE.append(playlist.kodi_path) + try: + _write_playlist_to_file(playlist, xml_playlist) + except Exception: + IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.kodi_path) + raise playlist.kodi_hash = utils.generate_file_md5(path) db.update_playlist(playlist) LOG.debug('Created Kodi playlist based on Plex playlist: %s', playlist) @@ -79,12 +84,14 @@ def delete(playlist): Returns None or raises PlaylistError """ if path_ops.exists(playlist.kodi_path): + IGNORE_KODI_PLAYLIST_CHANGE.append(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) + IGNORE_KODI_PLAYLIST_CHANGE.remove(playlist.kodi_path) raise PlaylistError('Could not delete %s' % playlist.kodi_path) db.update_playlist(playlist, delete=True)