2016-03-22 03:52:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-03 18:07:53 +00:00
|
|
|
"""
|
|
|
|
PlexPlaylist
|
|
|
|
"""
|
2016-03-22 03:52:58 +00:00
|
|
|
from plexapi import utils
|
2016-04-11 03:49:23 +00:00
|
|
|
from plexapi.exceptions import BadRequest
|
2016-02-03 18:07:53 +00:00
|
|
|
from plexapi.utils import cast, toDatetime
|
2016-04-08 02:48:45 +00:00
|
|
|
from plexapi.utils import PlexPartialObject, Playable
|
2016-03-22 03:52:58 +00:00
|
|
|
NA = utils.NA
|
2016-02-03 18:07:53 +00:00
|
|
|
|
|
|
|
|
2016-03-22 03:52:58 +00:00
|
|
|
@utils.register_libtype
|
2016-04-08 02:48:45 +00:00
|
|
|
class Playlist(PlexPartialObject, Playable):
|
2016-02-03 18:07:53 +00:00
|
|
|
TYPE = 'playlist'
|
|
|
|
|
2016-04-04 03:55:29 +00:00
|
|
|
def __init__(self, server, data, initpath):
|
|
|
|
super(Playlist, self).__init__(data, initpath, server)
|
|
|
|
|
2016-02-03 18:07:53 +00:00
|
|
|
def _loadData(self, data):
|
2016-04-08 02:48:45 +00:00
|
|
|
Playable._loadData(self, data)
|
2016-02-03 18:07:53 +00:00
|
|
|
self.addedAt = toDatetime(data.attrib.get('addedAt', NA))
|
2016-04-02 06:19:32 +00:00
|
|
|
self.composite = data.attrib.get('composite', NA) # url to thumbnail
|
2016-02-03 18:07:53 +00:00
|
|
|
self.duration = cast(int, data.attrib.get('duration', NA))
|
|
|
|
self.durationInSeconds = cast(int, data.attrib.get('durationInSeconds', NA))
|
|
|
|
self.guid = data.attrib.get('guid', NA)
|
2016-04-07 05:39:04 +00:00
|
|
|
self.key = data.attrib.get('key', NA)
|
2016-04-11 03:49:23 +00:00
|
|
|
self.key = self.key.replace('/items', '') if self.key else self.key # FIX_BUG_50
|
2016-03-22 03:52:58 +00:00
|
|
|
self.leafCount = cast(int, data.attrib.get('leafCount', NA))
|
|
|
|
self.playlistType = data.attrib.get('playlistType', NA)
|
|
|
|
self.ratingKey = data.attrib.get('ratingKey', NA)
|
|
|
|
self.smart = cast(bool, data.attrib.get('smart', NA))
|
|
|
|
self.summary = data.attrib.get('summary', NA)
|
|
|
|
self.title = data.attrib.get('title', NA)
|
|
|
|
self.type = data.attrib.get('type', NA)
|
|
|
|
self.updatedAt = toDatetime(data.attrib.get('updatedAt', NA))
|
2016-02-03 18:07:53 +00:00
|
|
|
|
2016-03-22 03:52:58 +00:00
|
|
|
def items(self):
|
|
|
|
path = '%s/items' % self.key
|
|
|
|
return utils.listItems(self.server, path)
|
2016-04-11 03:49:23 +00:00
|
|
|
|
|
|
|
def addItems(self, items):
|
|
|
|
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:
|
|
|
|
raise BadRequest('Can not mix media types when building a playlist: %s and %s' % (self.playlistType, item.listType))
|
2016-04-11 03:49:23 +00:00
|
|
|
ratingKeys.append(item.ratingKey)
|
2016-04-12 02:43:21 +00:00
|
|
|
uuid = items[0].section().uuid
|
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2016-04-11 03:49:23 +00:00
|
|
|
path = '%s/items%s' % (self.key, utils.joinArgs({
|
2016-04-12 02:43:21 +00:00
|
|
|
'uri': 'library://%s/directory//library/metadata/%s' % (uuid, ratingKeys),
|
2016-04-11 03:49:23 +00:00
|
|
|
}))
|
|
|
|
return self.server.query(path, method=self.server.session.put)
|
|
|
|
|
|
|
|
def removeItem(self, item):
|
|
|
|
path = '%s/items/%s' % (self.key, item.playlistItemID)
|
|
|
|
return self.server.query(path, method=self.server.session.delete)
|
|
|
|
|
|
|
|
def moveItem(self, item, after=None):
|
|
|
|
path = '%s/items/%s/move' % (self.key, item.playlistItemID)
|
2016-04-12 02:43:21 +00:00
|
|
|
if after: path += '?after=%s' % after.playlistItemID
|
2016-04-11 03:49:23 +00:00
|
|
|
return self.server.query(path, method=self.server.session.put)
|
|
|
|
|
|
|
|
def edit(self, title=None, summary=None):
|
|
|
|
path = '/library/metadata/%s%s' % (self.ratingKey, utils.joinArgs({'title':title, 'summary':summary}))
|
|
|
|
return self.server.query(path, method=self.server.session.put)
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
return self.server.query(self.key, method=self.server.session.delete)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, server, title, items):
|
|
|
|
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')
|
|
|
|
ratingKeys.append(item.ratingKey)
|
2016-04-12 02:43:21 +00:00
|
|
|
uuid = items[0].section().uuid
|
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2016-04-11 03:49:23 +00:00
|
|
|
path = '/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
|
|
|
|
})
|
|
|
|
data = server.query(path, method=server.session.post)[0]
|
|
|
|
return cls(server, data, initpath=path)
|