22from plexapi import utils
33from plexapi .base import PlexPartialObject , Playable
44from plexapi .exceptions import BadRequest , Unsupported
5+ from plexapi .library import LibrarySection
56from plexapi .playqueue import PlayQueue
67from plexapi .utils import cast , toDatetime
78from 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
0 commit comments