Skip to content

Support smart playlist #221

Closed
Closed

Activity

  1. blacktwin commented on Nov 13, 2018

    @blacktwin
    Collaborator

    I've started working on this. Here is a request POST that will create a Smart Playlist:

    url = 'http://localhost:32400/playlists?'
    headers = {'Accept': 'application/json'}
    
    machine = plex.machineIdentifier
    section = 2
    
    params = {
              'X-Plex-Token': PLEX_TOKEN,
              'type': 'video',
              'title': 'Movies',
              'smart': '1',
              'uri': 'library://{machine}/directory//library/sections/{section}/all?'
                     'type=video'
                     '&sourceType=1'
                     '&genre=35' 
                     '&limit=5'
                     '&unwatched=1'.format(machine=machine, section=section)}
    
    r = requests.post(url, headers=headers, params=params)

    I've tried to reproduce this with PlexAPI but can't seem to get it right.
    I've modified the following files:
    server.py

        def createSmartPlaylist(self, title, libraryName, limit=None, **kwargs):
            """ Creates and returns a new :class:`~plexapi.playlist.Playlist`.
    
                Parameters:
                    title (str): Title of the playlist to be created.
                    libraryName (str): Library name
                    limit (int): Limit of items to include in smart playlist.
            """
            return Playlist.createSmart(self, title, libraryName, limit, **kwargs)

    playlist.py

        @classmethod
        def createSmart(cls, server, title, libraryName, limit=None, **kwargs):
            """ Create a Smart playlist. """
            keyArgs = {}
            section = server.library.section(libraryName)
            sectionType = utils.searchType(section.type)
            sectionId = section.key
            uuid = section.uuid
            uri = 'library://%s/directory//library/sections/%s/all?type=%s' % \
                  (uuid, sectionId, sectionType)
            if limit:
                uri = uri + '&limit=%s' % str(limit)
            for category, value in kwargs.items():
                sectionChoices = section.listChoices(category)
                for choice in sectionChoices:
                    if choice.title.lower() == value.lower():
                        uri = uri + '&%s=%s' % (category.lower(), str(choice.key))
            key = '/playlists%s' % utils.joinArgs({
                'uri': uri,
                'type': sectionType,
                'title': title,
                'smart': 1
            })
            data = server.query(key, method=server._session.post)[0]
    
            return cls(server, data, initpath=key)

    The command to run: plex.createSmartPlaylist(title='TESTTEST', limit=10, libraryName='Movies', **{'genre': 'Action'})

    I've been trying to match the url from the POST and the key var from plexapi.

  2. Hellowlol commented on Nov 17, 2018

    @Hellowlol
    CollaboratorAuthor

    @pkkid Any idea what travis is complaining about in my branch? https://github.com/pkkid/python-plexapi/tree/smart

  3. pkkid commented on Nov 17, 2018

    @pkkid
    Collaborator

    I noticed sometimes things fail and if I kick it off again it will pass. I just started you're one failing build again, we can see if this is the case. -- We should figure it out and be a bit ore graceful, but I never put time into it yet.

  4. blacktwin commented on Jul 29, 2019

    @blacktwin
    Collaborator

    Some of the FILTERS or sectionChoices are not working properly when attempting to create a Smart Playlist.

    plex.createPlaylist("test", section="Movies", smart=True, unwatched=True, **{'genre':"Horror"})
    This will not use the unwatched FILTER/Choice, instead you just get the genre filter. Also because title is used for the naming of the Playlist **{'title':'Die Hard'} will return: TypeError: create() got multiple values for argument 'title'. Maybe changing the parameter to name or playlist_title?

    As it happens, looks like I accidentally omitted title from the ALLOWED_FILTERS for the MovieSection in PR #364 .

  5. blacktwin commented on Jul 30, 2019

    @blacktwin
    Collaborator

    PR incoming for playlist.py

            for category, value in kwargs.items():
                if section.listChoices(category):
                    sectionChoices = section.listChoices(category)
                    for choice in sectionChoices:
                        if str(choice.title).lower() == str(value).lower():
                            uri = uri + '&%s=%s' % (category.lower(), str(choice.key))
                ####New check for BOOLEN FILTER
                if category in section.BOOLEAN_FILTERS:
                    int_bool = section._cleanSearchFilter(category, value)
                    uri = uri + '&%s=%s' % (category.lower(), str(int_bool))

    previous code would not pass unwatched filter if used.
    plex.createPlaylist("test", section="Movies", smart=True, unwatched=True, **{'genre':"Horror"}) or
    plex.createPlaylist("test", section="Movies", smart=True, **{'unwatched':True, 'genre':"Horror"})

    Any thoughts on what to rename the title parameter? playlist_title or playlistTtile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions