Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding items from different servers to playlist #1411

Merged

Conversation

JonnyWong16
Copy link
Collaborator

@JonnyWong16 JonnyWong16 commented May 5, 2024

Description

Allow adding items from different servers to playlist.

Playlist.addItems(items) automatically splits the items into separate API calls since Plex only allows adding items from one server at a time. This will keep the items in the order they added in the list.

Closes #1409

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated the docstring for new or existing methods
  • I have added tests when applicable

@JonnyWong16 JonnyWong16 merged commit bbdb238 into pkkid:master May 11, 2024
5 checks passed
@JonnyWong16 JonnyWong16 deleted the feature/playlist_add_multiserver branch May 11, 2024 16:56
@glensc
Copy link
Contributor

glensc commented Sep 1, 2024

This assumes all items to be added have _server initialized to the actual server. This means if you load a playlist with remote items from local server and try to save, it will save with wrong server id, resulting data loss as invalid ids will be ignored.

...should somehow use sourceURI if present

@glensc
Copy link
Contributor

glensc commented Sep 1, 2024

I currently put together something like this. this is able to take existing playlist (via server.playlist() call) and save it.

def addItems(self, items):
    """ Add items to the playlist.

        Parameters:
            items (List): List of :class:`~plexapi.audio.Audio`, :class:`~plexapi.video.Video`,
                or :class:`~plexapi.photo.Photo` objects to be added to the playlist.

        Raises:
            :class:`plexapi.exceptions.BadRequest`: When trying to add items to a smart playlist.
    """
    if self.smart:
        raise BadRequest('Cannot add items to a smart playlist.')

    if items and not isinstance(items, (list, tuple)):
        items = [items]

    # Group items by server to maintain order when adding items from multiple servers
    # for server, _items in groupby(items, key=lambda item: item._server):
    for sourceURI, _items in groupby(items, key=lambda item: item.sourceURI):
        ratingKeys = []
        for item in _items:
            if item.listType != self.playlistType:  # pragma: no cover
                raise BadRequest(f'Can not mix media types when building a playlist: '
                                 f'{self.playlistType} and {item.listType}')
            sourceURI = item._server._uriRoot() if sourceURI is None else sourceURI
            ratingKeys.append(str(item.ratingKey))

        ratingKeys = ','.join(ratingKeys)
        # uri = f'{server._uriRoot()}/library/metadata/{ratingKeys}'
        uri = f'{sourceURI}/library/metadata/{ratingKeys}'

        args = {'uri': uri}
        key = f"{self.key}/items{utils.joinArgs(args)}"
        self._server.query(key, method=self._server._session.put)

    return self

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

Successfully merging this pull request may close these issues.

Support saving remote playlist items
2 participants