Skip to content

Commit e6dbf83

Browse files
committed
inital smart playlist
1 parent 4f5ce34 commit e6dbf83

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

plexapi/playlist.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from plexapi import utils
33
from plexapi.base import PlexPartialObject, Playable
44
from plexapi.exceptions import BadRequest, Unsupported
5+
from plexapi.library import LibrarySection
56
from plexapi.playqueue import PlayQueue
67
from plexapi.utils import cast, toDatetime
78
from plexapi.compat import quote_plus
@@ -127,9 +128,9 @@ def playQueue(self, *args, **kwargs):
127128
return PlayQueue.create(self._server, self, *args, **kwargs)
128129

129130
@classmethod
130-
def create(cls, server, title, items):
131+
def _create(cls, server, title, items):
131132
""" Create a playlist. """
132-
if not isinstance(items, (list, tuple)):
133+
if items and not isinstance(items, (list, tuple)):
133134
items = [items]
134135
ratingKeys = []
135136
for item in items:
@@ -147,6 +148,64 @@ def create(cls, server, title, items):
147148
data = server.query(key, method=server._session.post)[0]
148149
return cls(server, data, initpath=key)
149150

151+
@classmethod
152+
def create(cls, server, title, items=None, section=None, limit=None, smart=False, **kwargs):
153+
"""Create a playlist.
154+
155+
Parameters:
156+
server (:class:`~plexapi.server.PlexServer`): Server your connected to
157+
title (str): Title of the playlist.
158+
items (Iterable): Iterable of objects that should be in the playlist
159+
section (:class:`~plexapi.library.LibrarySection, str):
160+
limit (int): default None
161+
smart (bool): default False
162+
163+
**kwargs dict:
164+
is passed to the filters. For a example see the search method.
165+
166+
returns:
167+
class:`~plexapi.playlist.Playlist
168+
169+
170+
"""
171+
if smart:
172+
return cls._createSmart(server, title, section, limit, **kwargs)
173+
174+
else:
175+
return cls._create(server, title, items)
176+
177+
@classmethod
178+
def _createSmart(cls, server, title, section, limit=None, **kwargs):
179+
""" Create a Smart playlist. """
180+
181+
if not isinstance(section, LibrarySection):
182+
section = server.library.section(section)
183+
184+
sectionType = utils.searchType(section.type)
185+
sectionId = section.key
186+
uuid = section.uuid
187+
uri = 'library://%s/directory//library/sections/%s/all?type=%s' % (uuid,
188+
sectionId,
189+
sectionType)
190+
if limit:
191+
uri = uri + '&limit=%s' % str(limit)
192+
193+
for category, value in kwargs.items():
194+
sectionChoices = section.listChoices(category)
195+
for choice in sectionChoices:
196+
if choice.title == value or choice.title.lower() == value.lower():
197+
uri = uri + '&%s=%s' % (category.lower(), str(choice.key))
198+
199+
uri = uri + '&sourceType=%s' % sectionType
200+
key = '/playlists%s' % utils.joinArgs({
201+
'uri': uri,
202+
'type': section.CONTENT_TYPE,
203+
'title': title,
204+
'smart': 1,
205+
})
206+
data = server.query(key, method=server._session.post)[0]
207+
return cls(server, data, initpath=key)
208+
150209
def copyToUser(self, user):
151210
""" Copy playlist to another user account. """
152211
from plexapi.server import PlexServer

plexapi/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ def client(self, name):
240240

241241
raise NotFound('Unknown client name: %s' % name)
242242

243-
def createPlaylist(self, title, items):
243+
def createPlaylist(self, title, items=None, section=None, limit=None, smart=None, **kwargs):
244244
""" Creates and returns a new :class:`~plexapi.playlist.Playlist`.
245245
246246
Parameters:
247247
title (str): Title of the playlist to be created.
248248
items (list<Media>): List of media items to include in the playlist.
249249
"""
250-
return Playlist.create(self, title, items)
250+
return Playlist.create(self, title, items=items, limit=limit, section=section, smart=smart, **kwargs)
251251

252252
def createPlayQueue(self, item, **kwargs):
253253
""" Creates and returns a new :class:`~plexapi.playqueue.PlayQueue`.

0 commit comments

Comments
 (0)