From b33ed4ccbe648d01d36bdc2ad2368edb2db30888 Mon Sep 17 00:00:00 2001 From: Croneter Date: Wed, 2 May 2018 15:44:54 +0200 Subject: [PATCH] Fix putting more items on Plex playlist --- resources/lib/playlist_func.py | 33 ++++++++++++++++++++++++++++----- resources/lib/playlists.py | 12 +++++++----- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/resources/lib/playlist_func.py b/resources/lib/playlist_func.py index 3b93df7f..708de6f2 100644 --- a/resources/lib/playlist_func.py +++ b/resources/lib/playlist_func.py @@ -459,8 +459,8 @@ def update_playlist_from_PMS(playlist, playlist_id=None, xml=None): def init_plex_playlist(playlist, plex_id): """ - Initializes a new playlist on the PMS side. Returns True if it worked, - False otherwise. Will set playlist.id and playlist.plex_updatedat + Initializes a new playlist on the PMS side. Will set playlist.id and + playlist.plex_updatedat. Will raise PlaylistError if something went wrong. """ LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s', plex_id, playlist) @@ -479,17 +479,16 @@ def init_plex_playlist(playlist, plex_id): except (TypeError, IndexError, AttributeError): LOG.error('Could not initialize playlist on Plex side with plex id %s', plex_id) - return False + raise PlaylistError('Could not initialize Plex playlist %s', plex_id) api = API(xml[0]) playlist.id = api.plex_id() playlist.plex_updatedat = api.updated_at() - return True def init_plex_playqueue(playlist, plex_id=None, kodi_item=None): """ Initializes the Plex side without changing the Kodi playlists - WILL ALSO UPDATE OUR PLAYLISTS. + WILL ALSO UPDATE OUR PLAYLISTS. Returns the first PKC playlist item or raises PlaylistError """ @@ -580,6 +579,30 @@ def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None, return item +def add_item_to_plex_playlist(playlist, plex_id): + """ + Adds the item with plex_id to the existing Plex playlist (at the end). + Will set playlist.plex_updatedat + Raises PlaylistError if that did not work out. + """ + params = { + 'uri': ('library://None/item/%s' % (urllib.quote('/library/metadata/%s' + % plex_id, safe=''))) + } + xml = DU().downloadUrl(url='{server}/playlists/%s/items' % playlist.id, + action_type='PUT', + parameters=params) + try: + xml[0].attrib + except (TypeError, IndexError, AttributeError): + LOG.error('Could not initialize playlist on Plex side with plex id %s', + plex_id) + raise PlaylistError('Could not item %s to Plex playlist %s', + plex_id, playlist) + api = API(xml[0]) + playlist.plex_updatedat = api.updated_at() + + def add_item_to_plex_playqueue(playlist, pos, plex_id=None, kodi_item=None): """ Adds a new item to the playlist at position pos [int] only on the Plex diff --git a/resources/lib/playlists.py b/resources/lib/playlists.py index 0232dc57..a82e43c6 100644 --- a/resources/lib/playlists.py +++ b/resources/lib/playlists.py @@ -53,11 +53,13 @@ def create_plex_playlist(playlist=None, path=None): LOG.info('No Plex ids found for playlist %s', path) raise PL.PlaylistError for pos, plex_id in enumerate(plex_ids): - if pos == 0: - if not PL.init_plex_playlist(playlist, plex_id): - return - else: - PL.add_item_to_plex_playqueue(playlist, pos, plex_id=plex_id) + try: + if pos == 0 or not playlist.id: + PL.init_plex_playlist(playlist, plex_id) + else: + PL.add_item_to_plex_playqueue(playlist, pos, plex_id=plex_id) + except PL.PlaylistError: + continue update_plex_table(playlist, update_kodi_hash=True) LOG.info('Done creating Plex %s playlist %s', playlist.type, playlist.plex_name)