Fix putting more items on Plex playlist

This commit is contained in:
Croneter 2018-05-02 15:44:54 +02:00
parent 61b0645314
commit b33ed4ccbe
2 changed files with 35 additions and 10 deletions

View file

@ -459,8 +459,8 @@ def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
def init_plex_playlist(playlist, plex_id): def init_plex_playlist(playlist, plex_id):
""" """
Initializes a new playlist on the PMS side. Returns True if it worked, Initializes a new playlist on the PMS side. Will set playlist.id and
False otherwise. Will set playlist.id and playlist.plex_updatedat playlist.plex_updatedat. Will raise PlaylistError if something went wrong.
""" """
LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s', LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s',
plex_id, playlist) plex_id, playlist)
@ -479,17 +479,16 @@ def init_plex_playlist(playlist, plex_id):
except (TypeError, IndexError, AttributeError): except (TypeError, IndexError, AttributeError):
LOG.error('Could not initialize playlist on Plex side with plex id %s', LOG.error('Could not initialize playlist on Plex side with plex id %s',
plex_id) plex_id)
return False raise PlaylistError('Could not initialize Plex playlist %s', plex_id)
api = API(xml[0]) api = API(xml[0])
playlist.id = api.plex_id() playlist.id = api.plex_id()
playlist.plex_updatedat = api.updated_at() playlist.plex_updatedat = api.updated_at()
return True
def init_plex_playqueue(playlist, plex_id=None, kodi_item=None): def init_plex_playqueue(playlist, plex_id=None, kodi_item=None):
""" """
Initializes the Plex side without changing the Kodi playlists 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 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 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): 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 Adds a new item to the playlist at position pos [int] only on the Plex

View file

@ -53,11 +53,13 @@ def create_plex_playlist(playlist=None, path=None):
LOG.info('No Plex ids found for playlist %s', path) LOG.info('No Plex ids found for playlist %s', path)
raise PL.PlaylistError raise PL.PlaylistError
for pos, plex_id in enumerate(plex_ids): for pos, plex_id in enumerate(plex_ids):
if pos == 0: try:
if not PL.init_plex_playlist(playlist, plex_id): if pos == 0 or not playlist.id:
return PL.init_plex_playlist(playlist, plex_id)
else: else:
PL.add_item_to_plex_playqueue(playlist, pos, plex_id=plex_id) PL.add_item_to_plex_playqueue(playlist, pos, plex_id=plex_id)
except PL.PlaylistError:
continue
update_plex_table(playlist, update_kodi_hash=True) update_plex_table(playlist, update_kodi_hash=True)
LOG.info('Done creating Plex %s playlist %s', LOG.info('Done creating Plex %s playlist %s',
playlist.type, playlist.plex_name) playlist.type, playlist.plex_name)