Skip to content

Commit b5027e8

Browse files
committed
Merge branch 'smart'
2 parents 3f6a69e + c5083c3 commit b5027e8

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

plexapi/playlist.py

Lines changed: 58 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,61 @@ 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): is passed to the filters. For a example see the search method.
164+
165+
Returns:
166+
class:`~plexapi.playlist.Playlist
167+
"""
168+
if smart:
169+
return cls._createSmart(server, title, section, limit, **kwargs)
170+
171+
else:
172+
return cls._create(server, title, items)
173+
174+
@classmethod
175+
def _createSmart(cls, server, title, section, limit=None, **kwargs):
176+
""" Create a Smart playlist. """
177+
178+
if not isinstance(section, LibrarySection):
179+
section = server.library.section(section)
180+
181+
sectionType = utils.searchType(section.type)
182+
sectionId = section.key
183+
uuid = section.uuid
184+
uri = 'library://%s/directory//library/sections/%s/all?type=%s' % (uuid,
185+
sectionId,
186+
sectionType)
187+
if limit:
188+
uri = uri + '&limit=%s' % str(limit)
189+
190+
for category, value in kwargs.items():
191+
sectionChoices = section.listChoices(category)
192+
for choice in sectionChoices:
193+
if choice.title == value or choice.title.lower() == value.lower():
194+
uri = uri + '&%s=%s' % (category.lower(), str(choice.key))
195+
196+
uri = uri + '&sourceType=%s' % sectionType
197+
key = '/playlists%s' % utils.joinArgs({
198+
'uri': uri,
199+
'type': section.CONTENT_TYPE,
200+
'title': title,
201+
'smart': 1,
202+
})
203+
data = server.query(key, method=server._session.post)[0]
204+
return cls(server, data, initpath=key)
205+
150206
def copyToUser(self, user):
151207
""" Copy playlist to another user account. """
152208
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`.

tests/test_playlist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ def test_copyToUser(plex, show, fresh_plex, shared_username):
109109
assert playlist.title in [p.title for p in user_plex.playlists()]
110110
finally:
111111
playlist.delete()
112+
113+
114+
def test_smart_playlist(plex, movies):
115+
pl = plex.createPlaylist(title='smart_playlist', smart=True, limit=1, section=movies, year=2008)
116+
assert len(pl.items()) == 1
117+
assert pl.smart

0 commit comments

Comments
 (0)