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,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`: an instance of created 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 str (choice .title ).lower () == str (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
0 commit comments