2016-03-22 03:52:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from plexapi import utils
|
2017-02-04 19:46:51 +00:00
|
|
|
from plexapi.base import PlexPartialObject, Playable
|
2016-04-11 03:49:23 +00:00
|
|
|
from plexapi.exceptions import BadRequest
|
2017-02-07 06:58:29 +00:00
|
|
|
from plexapi.playqueue import PlayQueue
|
2016-02-03 18:07:53 +00:00
|
|
|
from plexapi.utils import cast, toDatetime
|
|
|
|
|
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2016-04-08 02:48:45 +00:00
|
|
|
class Playlist(PlexPartialObject, Playable):
|
2017-02-20 05:37:00 +00:00
|
|
|
""" Represents a single Playlist object.
|
2017-02-14 04:32:27 +00:00
|
|
|
# TODO: Document attributes
|
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Playlist'
|
2016-02-03 18:07:53 +00:00
|
|
|
TYPE = 'playlist'
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2016-04-08 02:48:45 +00:00
|
|
|
Playable._loadData(self, data)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.addedAt = toDatetime(data.attrib.get('addedAt'))
|
|
|
|
self.composite = data.attrib.get('composite') # url to thumbnail
|
|
|
|
self.duration = cast(int, data.attrib.get('duration'))
|
|
|
|
self.durationInSeconds = cast(int, data.attrib.get('durationInSeconds'))
|
|
|
|
self.guid = data.attrib.get('guid')
|
|
|
|
self.key = data.attrib.get('key')
|
2016-04-11 03:49:23 +00:00
|
|
|
self.key = self.key.replace('/items', '') if self.key else self.key # FIX_BUG_50
|
2017-02-04 17:43:50 +00:00
|
|
|
self.leafCount = cast(int, data.attrib.get('leafCount'))
|
|
|
|
self.playlistType = data.attrib.get('playlistType')
|
|
|
|
self.ratingKey = cast(int, data.attrib.get('ratingKey'))
|
|
|
|
self.smart = cast(bool, data.attrib.get('smart'))
|
|
|
|
self.summary = data.attrib.get('summary')
|
|
|
|
self.title = data.attrib.get('title')
|
|
|
|
self.type = data.attrib.get('type')
|
|
|
|
self.updatedAt = toDatetime(data.attrib.get('updatedAt'))
|
2016-02-03 18:07:53 +00:00
|
|
|
|
2016-03-22 03:52:58 +00:00
|
|
|
def items(self):
|
2017-02-06 04:52:10 +00:00
|
|
|
""" Returns a list of all items in the playlist. """
|
|
|
|
key = '%s/items' % self.key
|
2017-02-07 06:20:49 +00:00
|
|
|
return self.fetchItems(key)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def addItems(self, items):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Add items to a playlist. """
|
2016-04-11 03:49:23 +00:00
|
|
|
if not isinstance(items, (list, tuple)):
|
|
|
|
items = [items]
|
|
|
|
ratingKeys = []
|
|
|
|
for item in items:
|
2016-04-12 02:43:21 +00:00
|
|
|
if item.listType != self.playlistType:
|
2017-02-20 05:37:00 +00:00
|
|
|
raise BadRequest('Can not mix media types when building a playlist: %s and %s' %
|
|
|
|
(self.playlistType, item.listType))
|
2017-01-09 14:21:54 +00:00
|
|
|
ratingKeys.append(str(item.ratingKey))
|
2016-04-12 02:43:21 +00:00
|
|
|
uuid = items[0].section().uuid
|
2017-02-02 14:09:34 +00:00
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '%s/items%s' % (self.key, utils.joinArgs({
|
|
|
|
'uri': 'library://%s/directory//library/metadata/%s' % (uuid, ratingKeys)
|
2016-04-11 03:49:23 +00:00
|
|
|
}))
|
2017-02-09 04:29:17 +00:00
|
|
|
return self._server.query(key, method=self._server._session.put)
|
2016-04-11 03:49:23 +00:00
|
|
|
|
|
|
|
def removeItem(self, item):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Remove a file from a playlist. """
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '%s/items/%s' % (self.key, item.playlistItemID)
|
2017-02-09 04:29:17 +00:00
|
|
|
return self._server.query(key, method=self._server._session.delete)
|
2016-04-11 03:49:23 +00:00
|
|
|
|
|
|
|
def moveItem(self, item, after=None):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Move a to a new position in playlist. """
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '%s/items/%s/move' % (self.key, item.playlistItemID)
|
2017-01-02 21:06:40 +00:00
|
|
|
if after:
|
2017-02-07 06:20:49 +00:00
|
|
|
key += '?after=%s' % after.playlistItemID
|
2017-02-09 04:29:17 +00:00
|
|
|
return self._server.query(key, method=self._server._session.put)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def edit(self, title=None, summary=None):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Edit playlist. """
|
2017-02-20 05:37:00 +00:00
|
|
|
key = '/library/metadata/%s%s' % (self.ratingKey, utils.joinArgs({'title': title, 'summary': summary}))
|
2017-02-09 04:29:17 +00:00
|
|
|
return self._server.query(key, method=self._server._session.put)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def delete(self):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Delete playlist. """
|
2017-02-09 04:29:17 +00:00
|
|
|
return self._server.query(self.key, method=self._server._session.delete)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2017-02-07 06:58:29 +00:00
|
|
|
def playQueue(self, *args, **kwargs):
|
|
|
|
""" Create a playqueue from this playlist. """
|
2017-02-09 04:13:54 +00:00
|
|
|
return PlayQueue.create(self._server, self, *args, **kwargs)
|
2017-02-07 06:58:29 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, server, title, items):
|
2017-02-14 04:32:27 +00:00
|
|
|
""" Create a playlist. """
|
2016-04-11 03:49:23 +00:00
|
|
|
if not isinstance(items, (list, tuple)):
|
|
|
|
items = [items]
|
|
|
|
ratingKeys = []
|
|
|
|
for item in items:
|
2016-04-12 02:43:21 +00:00
|
|
|
if item.listType != items[0].listType:
|
2016-04-11 03:49:23 +00:00
|
|
|
raise BadRequest('Can not mix media types when building a playlist')
|
2016-12-21 13:17:28 +00:00
|
|
|
ratingKeys.append(str(item.ratingKey))
|
2016-04-12 02:43:21 +00:00
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2016-04-13 03:52:47 +00:00
|
|
|
uuid = items[0].section().uuid
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '/playlists%s' % utils.joinArgs({
|
2016-04-12 02:43:21 +00:00
|
|
|
'uri': 'library://%s/directory//library/metadata/%s' % (uuid, ratingKeys),
|
|
|
|
'type': items[0].listType,
|
2016-04-11 03:49:23 +00:00
|
|
|
'title': title,
|
|
|
|
'smart': 0
|
|
|
|
})
|
2017-02-09 04:29:17 +00:00
|
|
|
data = server.query(key, method=server._session.post)[0]
|
2017-02-07 06:20:49 +00:00
|
|
|
return cls(server, data, initpath=key)
|
2017-07-17 14:11:03 +00:00
|
|
|
|
2017-08-11 19:14:32 +00:00
|
|
|
def copyToUser(self, user):
|
|
|
|
""" Copy playlist to another user account. """
|
2017-07-17 14:11:03 +00:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
myplex = self._server.myPlexAccount()
|
|
|
|
user = myplex.user(user)
|
|
|
|
# Get the token for your machine.
|
|
|
|
token = user.get_token(self._server.machineIdentifier)
|
|
|
|
# Login to your server using your friends credentials.
|
|
|
|
user_server = PlexServer(self._server._baseurl, token)
|
|
|
|
return self.create(user_server, self.title, self.items())
|