diff --git a/resources/lib/playlists/plex_pl.py b/resources/lib/playlists/plex_pl.py index 8b94563d..d7edc432 100644 --- a/resources/lib/playlists/plex_pl.py +++ b/resources/lib/playlists/plex_pl.py @@ -28,14 +28,7 @@ def create(playlist): if not plex_ids: LOG.warning('No Plex ids found for playlist %s', playlist) raise PlaylistError - for pos, plex_id in enumerate(plex_ids): - try: - if pos == 0 or not playlist.plex_id: - pms.initialize(playlist, plex_id) - else: - pms.add_item(playlist, plex_id=plex_id) - except PlaylistError: - continue + pms.add_items(playlist, plex_ids) db.update_playlist(playlist) LOG.debug('Done creating Plex playlist %s', playlist) diff --git a/resources/lib/playlists/pms.py b/resources/lib/playlists/pms.py index 9d7f9023..ccafd3bf 100644 --- a/resources/lib/playlists/pms.py +++ b/resources/lib/playlists/pms.py @@ -12,7 +12,7 @@ from .common import PlaylistError from ..plex_api import API from ..downloadutils import DownloadUtils as DU -from .. import variables as v +from .. import app, variables as v ############################################################################### LOG = getLogger('PLEX.playlists.pms') @@ -97,6 +97,34 @@ def add_item(playlist, plex_id): playlist.plex_updatedat = api.updated_at() +def add_items(playlist, plex_ids): + """ + Adds all plex_ids (a list of ints) to a new Plex playlist. + Will set playlist.plex_updatedat + Raises PlaylistError if that did not work out. + """ + params = { + 'type': v.PLEX_PLAYLIST_TYPE_FROM_KODI[playlist.kodi_type], + 'title': playlist.plex_name, + 'smart': 0, + 'uri': ('server://%s/com.plexapp.plugins.library/library/metadata/%s' + % (app.CONN.machine_identifier, ','.join(plex_ids))) + } + xml = DU().downloadUrl(url='{server}/playlists/', + action_type='POST', + parameters=params) + try: + xml[0].attrib + except (TypeError, IndexError, AttributeError): + LOG.error('Could not add items to a new playlist %s on Plex side', + playlist) + raise PlaylistError('Could not add items to a new Plex playlist %s' % + playlist) + api = API(xml[0]) + playlist.plex_id = api.plex_id() + playlist.plex_updatedat = api.updated_at() + + def metadata(plex_id): """ Returns an xml with the entire metadata like updatedAt.