Rewire partial playlist sync, part 1

This commit is contained in:
croneter 2018-07-09 21:11:51 +02:00
parent 873601ec5a
commit ad54059289

View file

@ -86,25 +86,15 @@ def create_kodi_playlist(plex_id):
created in any case (not replaced). Thus make sure that the "same" playlist created in any case (not replaced). Thus make sure that the "same" playlist
is deleted from both disk and the Plex database. is deleted from both disk and the Plex database.
Returns the playlist or raises PL.PlaylistError Returns the playlist or raises PL.PlaylistError
Be aware that user settings will be checked whether this Plex playlist
should actually indeed be synced
""" """
xml_metadata = PL.get_pms_playlist_metadata(plex_id) xml_metadata = PL.get_pms_playlist_metadata(plex_id)
if xml_metadata is None: if xml_metadata is None:
LOG.error('Could not get Plex playlist metadata %s', plex_id) LOG.error('Could not get Plex playlist metadata %s', plex_id)
raise PL.PlaylistError('Could not get Plex playlist %s' % plex_id) raise PL.PlaylistError('Could not get Plex playlist %s' % plex_id)
api = API(xml_metadata[0]) api = API(xml_metadata[0])
if state.SYNC_SPECIFIC_PLEX_PLAYLISTS:
prefix = utils.settings('syncSpecificPlexPlaylistsPrefix').lower()
if api.title() and not api.title().lower().startswith(prefix):
LOG.debug('User chose to not sync Plex playlist %s', api.title())
return
playlist = PL.Playlist_Object() playlist = PL.Playlist_Object()
playlist.id = api.plex_id() playlist.id = api.plex_id()
playlist.type = v.KODI_PLAYLIST_TYPE_FROM_PLEX[api.playlist_type()] playlist.type = v.KODI_PLAYLIST_TYPE_FROM_PLEX[api.playlist_type()]
if not state.ENABLE_MUSIC and playlist.type == v.KODI_PLAYLIST_TYPE_AUDIO:
return
playlist.plex_name = api.title() playlist.plex_name = api.title()
playlist.plex_updatedat = api.updated_at() playlist.plex_updatedat = api.updated_at()
LOG.debug('Creating new Kodi playlist from Plex playlist: %s', playlist) LOG.debug('Creating new Kodi playlist from Plex playlist: %s', playlist)
@ -144,10 +134,7 @@ def delete_kodi_playlist(playlist):
the Plex playlist table. the Plex playlist table.
Returns None or raises PL.PlaylistError Returns None or raises PL.PlaylistError
""" """
if not sync_kodi_playlist(playlist.kodi_path): if path_ops.exists(playlist.kodi_path):
LOG.debug('Do not delete since we should not sync playlist %s',
playlist)
else:
try: try:
path_ops.remove(playlist.kodi_path) path_ops.remove(playlist.kodi_path)
except (OSError, IOError) as err: except (OSError, IOError) as err:
@ -336,7 +323,7 @@ def process_websocket(plex_id, status):
elif not playlist and not status == 9: elif not playlist and not status == 9:
LOG.debug('Creation of new Plex playlist detected: %s', LOG.debug('Creation of new Plex playlist detected: %s',
plex_id) plex_id)
create = True create = sync_plex_playlist(xml=xml[0])
# To the actual work # To the actual work
if create: if create:
create_kodi_playlist(plex_id) create_kodi_playlist(plex_id)
@ -371,30 +358,28 @@ def _full_sync():
old_plex_ids = plex_db.plex_ids_all_playlists() old_plex_ids = plex_db.plex_ids_all_playlists()
for xml_playlist in xml: for xml_playlist in xml:
api = API(xml_playlist) api = API(xml_playlist)
if (not state.ENABLE_MUSIC and try:
api.playlist_type() == v.PLEX_TYPE_AUDIO_PLAYLIST): old_plex_ids.remove(api.plex_id())
continue except ValueError:
pass
playlist = playlist_object_from_db(plex_id=api.plex_id()) playlist = playlist_object_from_db(plex_id=api.plex_id())
try: try:
if not playlist: if not playlist:
if not sync_plex_playlist(xml=xml_playlist):
continue
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())
create_kodi_playlist(api.plex_id()) create_kodi_playlist(api.plex_id())
continue continue
elif playlist.plex_updatedat != api.updated_at(): elif playlist.plex_updatedat != api.updated_at():
if not sync_plex_playlist(xml=xml_playlist):
continue
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())
if path_ops.exists(playlist.kodi_path): delete_kodi_playlist(playlist)
delete_kodi_playlist(playlist)
else:
update_plex_table(playlist, delete=True)
create_kodi_playlist(api.plex_id()) create_kodi_playlist(api.plex_id())
except PL.PlaylistError: except PL.PlaylistError:
LOG.info('Skipping playlist %s: %s', api.plex_id(), api.title()) LOG.info('Skipping playlist %s: %s', api.plex_id(), api.title())
try:
old_plex_ids.remove(api.plex_id())
except ValueError:
pass
# 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 = playlist_object_from_db(plex_id=plex_id) playlist = playlist_object_from_db(plex_id=plex_id)
@ -413,16 +398,14 @@ def _full_sync():
master_paths.append(v.PLAYLIST_PATH_MUSIC) master_paths.append(v.PLAYLIST_PATH_MUSIC)
for master_path in master_paths: for master_path in master_paths:
for root, _, files in path_ops.walk(master_path): for root, _, files in path_ops.walk(master_path):
for file in files: for f in files:
try: try:
extension = file.rsplit('.', 1)[1] extension = f.rsplit('.', 1)[1].lower()
except IndexError: except IndexError:
continue continue
if extension not in SUPPORTED_FILETYPES: if extension not in SUPPORTED_FILETYPES:
continue continue
path = path_ops.path.join(root, file) path = path_ops.path.join(root, f)
if not sync_kodi_playlist(path):
continue
kodi_hash = utils.generate_file_md5(path) kodi_hash = utils.generate_file_md5(path)
playlist = playlist_object_from_db(kodi_hash=kodi_hash) playlist = playlist_object_from_db(kodi_hash=kodi_hash)
playlist_2 = playlist_object_from_db(path=path) playlist_2 = playlist_object_from_db(path=path)
@ -430,6 +413,8 @@ def _full_sync():
# Nothing changed at all - neither path nor content # Nothing changed at all - neither path nor content
old_kodi_hashes.remove(kodi_hash) old_kodi_hashes.remove(kodi_hash)
continue continue
if not sync_kodi_playlist(path):
continue
try: try:
playlist = PL.Playlist_Object() playlist = PL.Playlist_Object()
playlist.kodi_path = path playlist.kodi_path = path
@ -474,6 +459,35 @@ def sync_kodi_playlist(path):
prefix = utils.settings('syncSpecificKodiPlaylistsPrefix').lower() prefix = utils.settings('syncSpecificKodiPlaylistsPrefix').lower()
if playlist.kodi_filename.lower().startswith(prefix): if playlist.kodi_filename.lower().startswith(prefix):
return True return True
LOG.debug('User chose to not sync Kodi playlist %s', path)
return False
def sync_plex_playlist(plex_id=None, xml=None):
"""
Returns True if we should sync this specific Plex playlist due to the
user settings (including a disabled music library), False if not.
Pass in either the plex_id or an xml (where API(xml) will be used)
"""
if not state.SYNC_SPECIFIC_PLEX_PLAYLISTS:
return True
if xml is None:
xml = PL.get_pms_playlist_metadata(plex_id)
if xml is None:
LOG.error('Could not get Plex metadata for playlist %s', plex_id)
return False
api = API(xml[0])
else:
api = API(xml)
if (not state.ENABLE_MUSIC and
api.playlist_type() == v.PLEX_PLAYLIST_TYPE_AUDIO):
LOG.debug('Not synching Plex audio playlist')
return False
prefix = utils.settings('syncSpecificPlexPlaylistsPrefix').lower()
if api.title() and api.title().lower().startswith(prefix):
return True
LOG.debug('User chose to not sync Plex playlist')
return False return False