python-plexapi/plexapi/playlist.py

115 lines
4.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-02-03 18:07:53 +00:00
"""
PlexPlaylist
"""
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
from plexapi.utils import PlexPartialObject, Playable
NA = utils.NA
2016-02-03 18:07:53 +00:00
@utils.register_libtype
class Playlist(PlexPartialObject, Playable):
2016-02-03 18:07:53 +00:00
TYPE = 'playlist'
def __init__(self, server, data, initpath):
2017-01-02 21:06:40 +00:00
"""Playlist stuff.
Args:
server (Plexserver): The PMS server your connected to
data (Element): Element built from server.query
initpath (str): Relativ path fx /library/sections/1/all
"""
super(Playlist, self).__init__(data, initpath, server)
2016-02-03 18:07:53 +00:00
def _loadData(self, data):
2017-01-02 21:06:40 +00:00
"""Used to set the attributes
Args:
data (Element): Usually built from server.query
"""
Playable._loadData(self, data)
2016-02-03 18:07:53 +00:00
self.addedAt = toDatetime(data.attrib.get('addedAt', NA))
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)
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
self.leafCount = cast(int, data.attrib.get('leafCount', NA))
self.playlistType = data.attrib.get('playlistType', NA)
2017-01-02 21:06:40 +00:00
self.ratingKey = cast(int, 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
def items(self):
2017-01-02 21:06:40 +00:00
"""Return all items in the playlist."""
path = '%s/items' % self.key
return utils.listItems(self.server, path)
2016-12-21 13:17:28 +00:00
2016-04-11 03:49:23 +00:00
def addItems(self, items):
2017-01-02 21:06:40 +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:
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)
uuid = items[0].section().uuid
ratingKeys = ','.join(ratingKeys)
2016-04-11 03:49:23 +00:00
path = '%s/items%s' % (self.key, utils.joinArgs({
'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):
2017-01-02 21:06:40 +00:00
"""Remove a file from a playlist."""
2016-04-11 03:49:23 +00:00
path = '%s/items/%s' % (self.key, item.playlistItemID)
return self.server.query(path, method=self.server.session.delete)
def moveItem(self, item, after=None):
2017-01-02 21:06:40 +00:00
"""Move a to a new position in playlist."""
2016-04-11 03:49:23 +00:00
path = '%s/items/%s/move' % (self.key, item.playlistItemID)
2017-01-02 21:06:40 +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)
2016-12-21 13:17:28 +00:00
2016-04-11 03:49:23 +00:00
def edit(self, title=None, summary=None):
2017-01-02 21:06:40 +00:00
"""Edit playlist."""
2016-04-11 03:49:23 +00:00
path = '/library/metadata/%s%s' % (self.ratingKey, utils.joinArgs({'title':title, 'summary':summary}))
return self.server.query(path, 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-01-02 21:06:40 +00:00
"""Delete playlist."""
2016-04-11 03:49:23 +00:00
return self.server.query(self.key, method=self.server.session.delete)
2016-12-21 13:17:28 +00:00
2016-04-11 03:49:23 +00:00
@classmethod
def create(cls, server, title, items):
2017-01-02 21:06:40 +00:00
"""Create a playlist."""
2016-04-11 03:49:23 +00:00
if not isinstance(items, (list, tuple)):
items = [items]
ratingKeys = []
2016-12-21 13:17:28 +00:00
2016-04-11 03:49:23 +00:00
for item in items:
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))
ratingKeys = ','.join(ratingKeys)
2016-04-13 03:52:47 +00:00
uuid = items[0].section().uuid
2016-04-11 03:49:23 +00:00
path = '/playlists%s' % utils.joinArgs({
'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
})
2016-12-21 13:17:28 +00:00
2016-04-11 03:49:23 +00:00
data = server.query(path, method=server.session.post)[0]
return cls(server, data, initpath=path)