2016-03-22 03:52:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-05-27 07:28:53 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote_plus, unquote
|
2020-05-12 21:15:16 +00:00
|
|
|
|
2021-09-13 00:56:21 +00:00
|
|
|
from plexapi import media, utils
|
2020-05-12 21:15:16 +00:00
|
|
|
from plexapi.base import Playable, PlexPartialObject
|
2020-12-24 06:32:48 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound, Unsupported
|
2018-11-16 22:47:49 +00:00
|
|
|
from plexapi.library import LibrarySection
|
2022-02-27 05:40:51 +00:00
|
|
|
from plexapi.mixins import SmartFilterMixin, ArtMixin, PosterMixin
|
2017-02-07 06:58:29 +00:00
|
|
|
from plexapi.playqueue import PlayQueue
|
2021-06-06 21:50:39 +00:00
|
|
|
from plexapi.utils import deprecated
|
2016-02-03 18:07:53 +00:00
|
|
|
|
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2022-02-27 05:40:51 +00:00
|
|
|
class Playlist(
|
|
|
|
PlexPartialObject, Playable,
|
|
|
|
SmartFilterMixin,
|
|
|
|
ArtMixin, PosterMixin
|
|
|
|
):
|
2020-12-24 06:25:10 +00:00
|
|
|
""" Represents a single Playlist.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
TAG (str): 'Playlist'
|
|
|
|
TYPE (str): 'playlist'
|
|
|
|
addedAt (datetime): Datetime the playlist was added to the server.
|
|
|
|
allowSync (bool): True if you allow syncing playlists.
|
|
|
|
composite (str): URL to composite image (/playlist/<ratingKey>/composite/<compositeid>)
|
2021-05-27 05:54:48 +00:00
|
|
|
content (str): The filter URI string for smart playlists.
|
2020-12-24 06:25:10 +00:00
|
|
|
duration (int): Duration of the playlist in milliseconds.
|
|
|
|
durationInSeconds (int): Duration of the playlist in seconds.
|
2021-09-13 00:56:21 +00:00
|
|
|
fields (List<:class:`~plexapi.media.Field`>): List of field objects.
|
2020-12-24 06:25:10 +00:00
|
|
|
guid (str): Plex GUID for the playlist (com.plexapp.agents.none://XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX).
|
2021-05-27 05:54:48 +00:00
|
|
|
icon (str): Icon URI string for smart playlists.
|
2020-12-24 06:25:10 +00:00
|
|
|
key (str): API URL (/playlist/<ratingkey>).
|
|
|
|
leafCount (int): Number of items in the playlist view.
|
2022-01-24 04:39:35 +00:00
|
|
|
librarySectionID (int): Library section identifier (radio only)
|
|
|
|
librarySectionKey (str): Library section key (radio only)
|
|
|
|
librarySectionTitle (str): Library section title (radio only)
|
2020-12-24 06:25:10 +00:00
|
|
|
playlistType (str): 'audio', 'video', or 'photo'
|
2022-01-24 04:39:35 +00:00
|
|
|
radio (bool): If this playlist represents a radio station
|
2020-12-24 06:25:10 +00:00
|
|
|
ratingKey (int): Unique key identifying the playlist.
|
|
|
|
smart (bool): True if the playlist is a smart playlist.
|
|
|
|
summary (str): Summary of the playlist.
|
|
|
|
title (str): Name of the playlist.
|
|
|
|
type (str): 'playlist'
|
2022-02-27 03:26:08 +00:00
|
|
|
updatedAt (datetime): Datetime the playlist was updated.
|
2017-02-14 04:32:27 +00:00
|
|
|
"""
|
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)
|
2021-05-27 02:45:12 +00:00
|
|
|
self.addedAt = utils.toDatetime(data.attrib.get('addedAt'))
|
|
|
|
self.allowSync = utils.cast(bool, data.attrib.get('allowSync'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.composite = data.attrib.get('composite') # url to thumbnail
|
2021-05-27 05:54:48 +00:00
|
|
|
self.content = data.attrib.get('content')
|
2021-05-27 02:45:12 +00:00
|
|
|
self.duration = utils.cast(int, data.attrib.get('duration'))
|
|
|
|
self.durationInSeconds = utils.cast(int, data.attrib.get('durationInSeconds'))
|
2021-09-13 00:56:21 +00:00
|
|
|
self.fields = self.findItems(data, media.Field)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.guid = data.attrib.get('guid')
|
2021-09-13 00:56:21 +00:00
|
|
|
self.icon = data.attrib.get('icon')
|
2020-12-24 06:25:10 +00:00
|
|
|
self.key = data.attrib.get('key', '').replace('/items', '') # FIX_BUG_50
|
2021-05-27 02:45:12 +00:00
|
|
|
self.leafCount = utils.cast(int, data.attrib.get('leafCount'))
|
2022-01-24 04:39:35 +00:00
|
|
|
self.librarySectionID = utils.cast(int, data.attrib.get('librarySectionID'))
|
|
|
|
self.librarySectionKey = data.attrib.get('librarySectionKey')
|
|
|
|
self.librarySectionTitle = data.attrib.get('librarySectionTitle')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.playlistType = data.attrib.get('playlistType')
|
2022-01-24 04:39:35 +00:00
|
|
|
self.radio = utils.cast(bool, data.attrib.get('radio', 0))
|
2021-05-27 02:45:12 +00:00
|
|
|
self.ratingKey = utils.cast(int, data.attrib.get('ratingKey'))
|
|
|
|
self.smart = utils.cast(bool, data.attrib.get('smart'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.summary = data.attrib.get('summary')
|
|
|
|
self.title = data.attrib.get('title')
|
|
|
|
self.type = data.attrib.get('type')
|
2021-05-27 02:45:12 +00:00
|
|
|
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt'))
|
2017-10-09 13:58:44 +00:00
|
|
|
self._items = None # cache for self.items
|
2021-05-27 07:28:53 +00:00
|
|
|
self._section = None # cache for self.section
|
2021-06-18 21:26:48 +00:00
|
|
|
self._filters = None # cache for self.filters
|
2017-10-05 20:24:49 +00:00
|
|
|
|
2017-10-25 22:01:42 +00:00
|
|
|
def __len__(self): # pragma: no cover
|
2017-10-05 20:24:49 +00:00
|
|
|
return len(self.items())
|
|
|
|
|
2020-08-11 17:11:53 +00:00
|
|
|
def __iter__(self): # pragma: no cover
|
|
|
|
for item in self.items():
|
|
|
|
yield item
|
|
|
|
|
2021-05-28 03:09:49 +00:00
|
|
|
def __contains__(self, other): # pragma: no cover
|
|
|
|
return any(i.key == other.key for i in self.items())
|
|
|
|
|
|
|
|
def __getitem__(self, key): # pragma: no cover
|
|
|
|
return self.items()[key]
|
|
|
|
|
2021-02-15 03:58:03 +00:00
|
|
|
@property
|
|
|
|
def thumb(self):
|
|
|
|
""" Alias to self.composite. """
|
|
|
|
return self.composite
|
|
|
|
|
2018-09-08 15:25:16 +00:00
|
|
|
@property
|
|
|
|
def metadataType(self):
|
2021-05-28 05:59:14 +00:00
|
|
|
""" Returns the type of metadata in the playlist (movie, track, or photo). """
|
2018-09-08 15:25:16 +00:00
|
|
|
if self.isVideo:
|
|
|
|
return 'movie'
|
|
|
|
elif self.isAudio:
|
|
|
|
return 'track'
|
|
|
|
elif self.isPhoto:
|
|
|
|
return 'photo'
|
|
|
|
else:
|
|
|
|
raise Unsupported('Unexpected playlist type')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def isVideo(self):
|
2021-05-28 05:59:14 +00:00
|
|
|
""" Returns True if this is a video playlist. """
|
2018-09-08 15:25:16 +00:00
|
|
|
return self.playlistType == 'video'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def isAudio(self):
|
2021-05-28 05:59:14 +00:00
|
|
|
""" Returns True if this is an audio playlist. """
|
2018-09-08 15:25:16 +00:00
|
|
|
return self.playlistType == 'audio'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def isPhoto(self):
|
2021-05-28 05:59:14 +00:00
|
|
|
""" Returns True if this is a photo playlist. """
|
2018-09-08 15:25:16 +00:00
|
|
|
return self.playlistType == 'photo'
|
|
|
|
|
2021-06-18 21:26:48 +00:00
|
|
|
def _getPlaylistItemID(self, item):
|
|
|
|
""" Match an item to a playlist item and return the item playlistItemID. """
|
|
|
|
for _item in self.items():
|
|
|
|
if _item.ratingKey == item.ratingKey:
|
|
|
|
return _item.playlistItemID
|
|
|
|
raise NotFound('Item with title "%s" not found in the playlist' % item.title)
|
|
|
|
|
|
|
|
def filters(self):
|
|
|
|
""" Returns the search filter dict for smart playlist.
|
|
|
|
The filter dict be passed back into :func:`~plexapi.library.LibrarySection.search`
|
|
|
|
to get the list of items.
|
|
|
|
"""
|
|
|
|
if self.smart and self._filters is None:
|
|
|
|
self._filters = self._parseFilters(self.content)
|
|
|
|
return self._filters
|
|
|
|
|
2021-05-27 07:28:53 +00:00
|
|
|
def section(self):
|
|
|
|
""" Returns the :class:`~plexapi.library.LibrarySection` this smart playlist belongs to.
|
2017-10-05 20:24:49 +00:00
|
|
|
|
2021-05-27 07:28:53 +00:00
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When trying to get the section for a regular playlist.
|
|
|
|
:class:`plexapi.exceptions.Unsupported`: When unable to determine the library section.
|
|
|
|
"""
|
|
|
|
if not self.smart:
|
|
|
|
raise BadRequest('Regular playlists are not associated with a library.')
|
|
|
|
|
|
|
|
if self._section is None:
|
|
|
|
# Try to parse the library section from the content URI string
|
|
|
|
match = re.search(r'/library/sections/(\d+)/all', unquote(self.content or ''))
|
|
|
|
if match:
|
|
|
|
sectionKey = int(match.group(1))
|
|
|
|
self._section = self._server.library.sectionByID(sectionKey)
|
|
|
|
return self._section
|
|
|
|
|
|
|
|
# Try to get the library section from the first item in the playlist
|
|
|
|
if self.items():
|
|
|
|
self._section = self.items()[0].section()
|
|
|
|
return self._section
|
|
|
|
|
|
|
|
raise Unsupported('Unable to determine the library section')
|
|
|
|
|
|
|
|
return self._section
|
2016-02-03 18:07:53 +00:00
|
|
|
|
2020-12-24 06:32:48 +00:00
|
|
|
def item(self, title):
|
|
|
|
""" Returns the item in the playlist that matches the specified title.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
title (str): Title of the item to return.
|
2021-05-28 05:59:14 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.NotFound`: When the item is not found in the playlist.
|
2020-12-24 06:32:48 +00:00
|
|
|
"""
|
|
|
|
for item in self.items():
|
2020-12-24 17:21:29 +00:00
|
|
|
if item.title.lower() == title.lower():
|
2020-12-24 06:32:48 +00:00
|
|
|
return item
|
2020-12-24 17:21:29 +00:00
|
|
|
raise NotFound('Item with title "%s" not found in the playlist' % title)
|
2020-12-24 06:32:48 +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. """
|
2022-01-24 04:39:35 +00:00
|
|
|
if self.radio:
|
|
|
|
return []
|
2017-10-05 20:24:49 +00:00
|
|
|
if self._items is None:
|
2021-05-27 07:05:17 +00:00
|
|
|
key = '%s/items' % self.key
|
2017-10-05 20:24:49 +00:00
|
|
|
items = self.fetchItems(key)
|
|
|
|
self._items = items
|
|
|
|
return self._items
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2020-12-24 06:32:48 +00:00
|
|
|
def get(self, title):
|
|
|
|
""" Alias to :func:`~plexapi.playlist.Playlist.item`. """
|
|
|
|
return self.item(title)
|
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def addItems(self, items):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Add items to the playlist.
|
|
|
|
|
|
|
|
Parameters:
|
2021-05-30 00:58:55 +00:00
|
|
|
items (List): List of :class:`~plexapi.audio.Audio`, :class:`~plexapi.video.Video`,
|
|
|
|
or :class:`~plexapi.photo.Photo` objects to be added to the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When trying to add items to a smart playlist.
|
|
|
|
"""
|
|
|
|
if self.smart:
|
|
|
|
raise BadRequest('Cannot add items to a smart playlist.')
|
|
|
|
|
|
|
|
if items and not isinstance(items, (list, tuple)):
|
2016-04-11 03:49:23 +00:00
|
|
|
items = [items]
|
2021-05-27 05:56:57 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
ratingKeys = []
|
|
|
|
for item in items:
|
2017-10-25 22:01:42 +00:00
|
|
|
if item.listType != self.playlistType: # pragma: no cover
|
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))
|
2021-05-27 05:56:57 +00:00
|
|
|
|
2017-02-02 14:09:34 +00:00
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2021-05-30 01:22:27 +00:00
|
|
|
uri = '%s/library/metadata/%s' % (self._server._uriRoot(), ratingKeys)
|
2021-05-27 05:56:57 +00:00
|
|
|
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '%s/items%s' % (self.key, utils.joinArgs({
|
2021-05-27 05:56:57 +00:00
|
|
|
'uri': uri
|
2016-04-11 03:49:23 +00:00
|
|
|
}))
|
2021-05-27 05:56:57 +00:00
|
|
|
self._server.query(key, method=self._server._session.put)
|
2016-04-11 03:49:23 +00:00
|
|
|
|
2021-05-27 05:56:57 +00:00
|
|
|
@deprecated('use "removeItems" instead', stacklevel=3)
|
2016-04-11 03:49:23 +00:00
|
|
|
def removeItem(self, item):
|
2021-05-27 05:56:57 +00:00
|
|
|
self.removeItems(item)
|
|
|
|
|
|
|
|
def removeItems(self, items):
|
|
|
|
""" Remove items from the playlist.
|
|
|
|
|
|
|
|
Parameters:
|
2021-05-30 00:58:55 +00:00
|
|
|
items (List): List of :class:`~plexapi.audio.Audio`, :class:`~plexapi.video.Video`,
|
|
|
|
or :class:`~plexapi.photo.Photo` objects to be removed from the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When trying to remove items from a smart playlist.
|
2021-06-02 16:23:39 +00:00
|
|
|
:class:`plexapi.exceptions.NotFound`: When the item does not exist in the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
"""
|
|
|
|
if self.smart:
|
|
|
|
raise BadRequest('Cannot remove items from a smart playlist.')
|
|
|
|
|
|
|
|
if items and not isinstance(items, (list, tuple)):
|
|
|
|
items = [items]
|
|
|
|
|
|
|
|
for item in items:
|
2021-06-02 16:23:39 +00:00
|
|
|
playlistItemID = self._getPlaylistItemID(item)
|
|
|
|
key = '%s/items/%s' % (self.key, playlistItemID)
|
2021-05-27 05:56:57 +00:00
|
|
|
self._server.query(key, method=self._server._session.delete)
|
2016-04-11 03:49:23 +00:00
|
|
|
|
|
|
|
def moveItem(self, item, after=None):
|
2021-06-30 03:08:19 +00:00
|
|
|
""" Move an item to a new position in the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
|
|
|
|
Parameters:
|
2021-05-30 00:58:55 +00:00
|
|
|
items (obj): :class:`~plexapi.audio.Audio`, :class:`~plexapi.video.Video`,
|
|
|
|
or :class:`~plexapi.photo.Photo` objects to be moved in the playlist.
|
|
|
|
after (obj): :class:`~plexapi.audio.Audio`, :class:`~plexapi.video.Video`,
|
|
|
|
or :class:`~plexapi.photo.Photo` objects to move the item after in the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When trying to move items in a smart playlist.
|
2021-06-02 16:23:39 +00:00
|
|
|
:class:`plexapi.exceptions.NotFound`: When the item or item after does not exist in the playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
"""
|
|
|
|
if self.smart:
|
|
|
|
raise BadRequest('Cannot move items in a smart playlist.')
|
|
|
|
|
2021-06-02 16:23:39 +00:00
|
|
|
playlistItemID = self._getPlaylistItemID(item)
|
|
|
|
key = '%s/items/%s/move' % (self.key, playlistItemID)
|
|
|
|
|
2017-01-02 21:06:40 +00:00
|
|
|
if after:
|
2021-06-02 16:23:39 +00:00
|
|
|
afterPlaylistItemID = self._getPlaylistItemID(after)
|
|
|
|
key += '?after=%s' % afterPlaylistItemID
|
|
|
|
|
2021-05-27 05:56:57 +00:00
|
|
|
self._server.query(key, method=self._server._session.put)
|
|
|
|
|
2021-05-27 07:05:17 +00:00
|
|
|
def updateFilters(self, limit=None, sort=None, filters=None, **kwargs):
|
|
|
|
""" Update the filters for a smart playlist.
|
2021-05-27 05:56:57 +00:00
|
|
|
|
2021-05-27 07:05:17 +00:00
|
|
|
Parameters:
|
|
|
|
limit (int): Limit the number of items in the playlist.
|
|
|
|
sort (str or list, optional): A string of comma separated sort fields
|
|
|
|
or a list of sort fields in the format ``column:dir``.
|
2021-05-30 00:58:55 +00:00
|
|
|
See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-27 07:05:17 +00:00
|
|
|
filters (dict): A dictionary of advanced filters.
|
2021-05-30 00:58:55 +00:00
|
|
|
See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-27 07:05:17 +00:00
|
|
|
**kwargs (dict): Additional custom filters to apply to the search results.
|
2021-05-30 00:58:55 +00:00
|
|
|
See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-27 07:05:17 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When trying update filters for a regular playlist.
|
|
|
|
"""
|
|
|
|
if not self.smart:
|
|
|
|
raise BadRequest('Cannot update filters for a regular playlist.')
|
|
|
|
|
|
|
|
section = self.section()
|
|
|
|
searchKey = section._buildSearchKey(
|
2021-05-28 04:16:49 +00:00
|
|
|
sort=sort, libtype=section.METADATA_TYPE, limit=limit, filters=filters, **kwargs)
|
2021-05-30 01:22:27 +00:00
|
|
|
uri = '%s%s' % (self._server._uriRoot(), searchKey)
|
2021-05-27 07:05:17 +00:00
|
|
|
|
|
|
|
key = '%s/items%s' % (self.key, utils.joinArgs({
|
|
|
|
'uri': uri
|
|
|
|
}))
|
|
|
|
self._server.query(key, method=self._server._session.put)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2021-09-13 00:56:21 +00:00
|
|
|
def _edit(self, **kwargs):
|
|
|
|
""" Actually edit the playlist. """
|
|
|
|
key = '%s%s' % (self.key, utils.joinArgs(kwargs))
|
|
|
|
self._server.query(key, method=self._server._session.put)
|
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def edit(self, title=None, summary=None):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Edit the playlist.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
title (str, optional): The title of the playlist.
|
|
|
|
summary (str, optional): The summary of the playlist.
|
|
|
|
"""
|
|
|
|
args = {}
|
2021-05-30 00:58:55 +00:00
|
|
|
if title:
|
2021-05-27 05:56:57 +00:00
|
|
|
args['title'] = title
|
2021-05-30 00:58:55 +00:00
|
|
|
if summary:
|
2021-05-27 05:56:57 +00:00
|
|
|
args['summary'] = summary
|
2021-09-13 00:56:21 +00:00
|
|
|
self._edit(**args)
|
2016-12-21 13:17:28 +00:00
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def delete(self):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Delete the playlist. """
|
|
|
|
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):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Returns a new :class:`~plexapi.playqueue.PlayQueue` from the 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
|
2018-11-16 22:47:49 +00:00
|
|
|
def _create(cls, server, title, items):
|
2021-05-28 02:53:38 +00:00
|
|
|
""" Create a regular playlist. """
|
2020-07-23 23:31:27 +00:00
|
|
|
if not items:
|
2021-05-27 05:56:57 +00:00
|
|
|
raise BadRequest('Must include items to add when creating new playlist.')
|
|
|
|
|
2018-11-16 22:47:49 +00:00
|
|
|
if items and not isinstance(items, (list, tuple)):
|
2016-04-11 03:49:23 +00:00
|
|
|
items = [items]
|
2021-05-28 02:53:38 +00:00
|
|
|
|
|
|
|
listType = items[0].listType
|
2016-04-11 03:49:23 +00:00
|
|
|
ratingKeys = []
|
|
|
|
for item in items:
|
2021-05-28 02:53:38 +00:00
|
|
|
if item.listType != listType: # pragma: no cover
|
2021-05-27 05:56:57 +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))
|
2021-05-28 02:53:38 +00:00
|
|
|
|
2016-04-12 02:43:21 +00:00
|
|
|
ratingKeys = ','.join(ratingKeys)
|
2021-05-30 01:22:27 +00:00
|
|
|
uri = '%s/library/metadata/%s' % (server._uriRoot(), ratingKeys)
|
2021-05-28 02:53:38 +00:00
|
|
|
|
2017-02-07 06:20:49 +00:00
|
|
|
key = '/playlists%s' % utils.joinArgs({
|
2021-05-28 02:53:38 +00:00
|
|
|
'uri': uri,
|
|
|
|
'type': 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
|
|
|
|
2018-11-16 22:47:49 +00:00
|
|
|
@classmethod
|
2021-07-27 01:46:30 +00:00
|
|
|
def _createSmart(cls, server, title, section, limit=None, libtype=None, sort=None, filters=None, **kwargs):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Create a smart playlist. """
|
2018-11-16 22:47:49 +00:00
|
|
|
if not isinstance(section, LibrarySection):
|
|
|
|
section = server.library.section(section)
|
|
|
|
|
2021-07-27 01:46:30 +00:00
|
|
|
libtype = libtype or section.METADATA_TYPE
|
|
|
|
|
2021-05-28 02:53:38 +00:00
|
|
|
searchKey = section._buildSearchKey(
|
2021-07-27 01:46:30 +00:00
|
|
|
sort=sort, libtype=libtype, limit=limit, filters=filters, **kwargs)
|
2021-05-30 01:22:27 +00:00
|
|
|
uri = '%s%s' % (server._uriRoot(), searchKey)
|
2021-05-28 02:53:38 +00:00
|
|
|
|
2018-11-16 22:47:49 +00:00
|
|
|
key = '/playlists%s' % utils.joinArgs({
|
|
|
|
'uri': uri,
|
|
|
|
'type': section.CONTENT_TYPE,
|
|
|
|
'title': title,
|
|
|
|
'smart': 1,
|
|
|
|
})
|
|
|
|
data = server.query(key, method=server._session.post)[0]
|
|
|
|
return cls(server, data, initpath=key)
|
|
|
|
|
2021-05-28 02:53:38 +00:00
|
|
|
@classmethod
|
2021-05-28 05:50:30 +00:00
|
|
|
def create(cls, server, title, section=None, items=None, smart=False, limit=None,
|
2021-07-27 01:46:30 +00:00
|
|
|
libtype=None, sort=None, filters=None, **kwargs):
|
2021-05-27 05:56:57 +00:00
|
|
|
""" Create a playlist.
|
2021-05-28 02:53:38 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
server (:class:`~plexapi.server.PlexServer`): Server to create the playlist on.
|
|
|
|
title (str): Title of the playlist.
|
2021-05-28 05:50:30 +00:00
|
|
|
section (:class:`~plexapi.library.LibrarySection`, str): Smart playlists only,
|
|
|
|
the library section to create the playlist in.
|
2021-05-30 00:58:55 +00:00
|
|
|
items (List): Regular playlists only, list of :class:`~plexapi.audio.Audio`,
|
|
|
|
:class:`~plexapi.video.Video`, or :class:`~plexapi.photo.Photo` objects to be added to the playlist.
|
2021-05-28 02:53:38 +00:00
|
|
|
smart (bool): True to create a smart playlist. Default False.
|
|
|
|
limit (int): Smart playlists only, limit the number of items in the playlist.
|
2021-07-27 01:46:30 +00:00
|
|
|
libtype (str): Smart playlists only, the specific type of content to filter
|
|
|
|
(movie, show, season, episode, artist, album, track, photoalbum, photo).
|
2021-05-28 02:53:38 +00:00
|
|
|
sort (str or list, optional): Smart playlists only, a string of comma separated sort fields
|
|
|
|
or a list of sort fields in the format ``column:dir``.
|
2021-05-30 00:58:55 +00:00
|
|
|
See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-28 02:53:38 +00:00
|
|
|
filters (dict): Smart playlists only, a dictionary of advanced filters.
|
2021-05-30 00:58:55 +00:00
|
|
|
See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-28 02:53:38 +00:00
|
|
|
**kwargs (dict): Smart playlists only, additional custom filters to apply to the
|
2021-05-30 00:58:55 +00:00
|
|
|
search results. See :func:`~plexapi.library.LibrarySection.search` for more info.
|
2021-05-27 07:05:17 +00:00
|
|
|
|
2021-05-28 02:53:38 +00:00
|
|
|
Raises:
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When no items are included to create the playlist.
|
|
|
|
:class:`plexapi.exceptions.BadRequest`: When mixing media types in the playlist.
|
|
|
|
|
|
|
|
Returns:
|
2021-05-27 05:56:57 +00:00
|
|
|
:class:`~plexapi.playlist.Playlist`: A new instance of the created Playlist.
|
2021-05-28 02:53:38 +00:00
|
|
|
"""
|
|
|
|
if smart:
|
2021-07-27 01:46:30 +00:00
|
|
|
return cls._createSmart(server, title, section, limit, libtype, sort, filters, **kwargs)
|
2021-05-28 02:53:38 +00:00
|
|
|
else:
|
|
|
|
return cls._create(server, title, items)
|
|
|
|
|
2017-08-11 19:14:32 +00:00
|
|
|
def copyToUser(self, user):
|
2021-05-12 00:10:39 +00:00
|
|
|
""" Copy playlist to another user account.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
user (str): Username, email or user id of the user to copy the playlist to.
|
|
|
|
"""
|
|
|
|
userServer = self._server.switchUser(user)
|
2021-06-07 18:56:54 +00:00
|
|
|
return self.create(server=userServer, title=self.title, items=self.items())
|
2018-09-08 15:25:16 +00:00
|
|
|
|
|
|
|
def sync(self, videoQuality=None, photoResolution=None, audioBitrate=None, client=None, clientId=None, limit=None,
|
|
|
|
unwatched=False, title=None):
|
2021-05-28 04:16:49 +00:00
|
|
|
""" Add the playlist as a sync item for the specified device.
|
2020-11-23 03:06:30 +00:00
|
|
|
See :func:`~plexapi.myplex.MyPlexAccount.sync` for possible exceptions.
|
2018-09-08 15:25:16 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
videoQuality (int): idx of quality of the video, one of VIDEO_QUALITY_* values defined in
|
2020-11-23 03:06:30 +00:00
|
|
|
:mod:`~plexapi.sync` module. Used only when playlist contains video.
|
2018-09-08 15:25:16 +00:00
|
|
|
photoResolution (str): maximum allowed resolution for synchronized photos, see PHOTO_QUALITY_* values in
|
2020-11-23 03:06:30 +00:00
|
|
|
the module :mod:`~plexapi.sync`. Used only when playlist contains photos.
|
2018-09-14 18:03:23 +00:00
|
|
|
audioBitrate (int): maximum bitrate for synchronized music, better use one of MUSIC_BITRATE_* values
|
2020-11-23 03:06:30 +00:00
|
|
|
from the module :mod:`~plexapi.sync`. Used only when playlist contains audio.
|
|
|
|
client (:class:`~plexapi.myplex.MyPlexDevice`): sync destination, see
|
|
|
|
:func:`~plexapi.myplex.MyPlexAccount.sync`.
|
|
|
|
clientId (str): sync destination, see :func:`~plexapi.myplex.MyPlexAccount.sync`.
|
2018-09-08 15:25:16 +00:00
|
|
|
limit (int): maximum count of items to sync, unlimited if `None`.
|
|
|
|
unwatched (bool): if `True` watched videos wouldn't be synced.
|
2020-11-23 03:06:30 +00:00
|
|
|
title (str): descriptive title for the new :class:`~plexapi.sync.SyncItem`, if empty the value would be
|
2018-09-08 15:25:16 +00:00
|
|
|
generated from metadata of current photo.
|
|
|
|
|
|
|
|
Raises:
|
2021-01-03 00:44:18 +00:00
|
|
|
:exc:`~plexapi.exceptions.BadRequest`: When playlist is not allowed to sync.
|
|
|
|
:exc:`~plexapi.exceptions.Unsupported`: When playlist content is unsupported.
|
2018-09-08 15:25:16 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-05-30 00:58:55 +00:00
|
|
|
:class:`~plexapi.sync.SyncItem`: A new instance of the created sync item.
|
2018-09-08 15:25:16 +00:00
|
|
|
"""
|
|
|
|
if not self.allowSync:
|
|
|
|
raise BadRequest('The playlist is not allowed to sync')
|
|
|
|
|
|
|
|
from plexapi.sync import SyncItem, Policy, MediaSettings
|
|
|
|
|
|
|
|
myplex = self._server.myPlexAccount()
|
|
|
|
sync_item = SyncItem(self._server, None)
|
|
|
|
sync_item.title = title if title else self.title
|
|
|
|
sync_item.rootTitle = self.title
|
|
|
|
sync_item.contentType = self.playlistType
|
|
|
|
sync_item.metadataType = self.metadataType
|
|
|
|
sync_item.machineIdentifier = self._server.machineIdentifier
|
|
|
|
|
|
|
|
sync_item.location = 'playlist:///%s' % quote_plus(self.guid)
|
|
|
|
sync_item.policy = Policy.create(limit, unwatched)
|
|
|
|
|
|
|
|
if self.isVideo:
|
|
|
|
sync_item.mediaSettings = MediaSettings.createVideo(videoQuality)
|
|
|
|
elif self.isAudio:
|
|
|
|
sync_item.mediaSettings = MediaSettings.createMusic(audioBitrate)
|
|
|
|
elif self.isPhoto:
|
|
|
|
sync_item.mediaSettings = MediaSettings.createPhoto(photoResolution)
|
|
|
|
else:
|
|
|
|
raise Unsupported('Unsupported playlist content')
|
|
|
|
|
|
|
|
return myplex.sync(sync_item, client=client, clientId=clientId)
|
2021-08-03 03:37:29 +00:00
|
|
|
|
2021-09-26 22:23:09 +00:00
|
|
|
def _getWebURL(self, base=None):
|
|
|
|
""" Get the Plex Web URL with the correct parameters. """
|
|
|
|
return self._server._buildWebURL(base=base, endpoint='playlist', key=self.key)
|