Faster way to initialize playlists on the Plex side

- Partially fixes #578
This commit is contained in:
croneter 2018-12-24 17:06:01 +01:00
parent 323a4482e5
commit a9f51f9ac4
2 changed files with 30 additions and 9 deletions

View file

@ -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)

View file

@ -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.