From 32f5f56ebbd68c9b81fefb1970163137fbee5c2e Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 4 May 2024 21:02:31 -0700 Subject: [PATCH] Allow adding items from different server to playlist --- plexapi/playlist.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/plexapi/playlist.py b/plexapi/playlist.py index a50b6200..dda3bdf5 100644 --- a/plexapi/playlist.py +++ b/plexapi/playlist.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import re +from itertools import groupby from pathlib import Path from urllib.parse import quote_plus, unquote @@ -212,19 +213,23 @@ def addItems(self, items): if items and not isinstance(items, (list, tuple)): items = [items] - 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}') - ratingKeys.append(str(item.ratingKey)) + # Group items by server to maintain order when adding items from multiple servers + for server, _items in groupby(items, key=lambda item: item._server): - ratingKeys = ','.join(ratingKeys) - uri = f'{self._server._uriRoot()}/library/metadata/{ratingKeys}' + 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}') + ratingKeys.append(str(item.ratingKey)) + + ratingKeys = ','.join(ratingKeys) + uri = f'{server._uriRoot()}/library/metadata/{ratingKeys}' + + args = {'uri': uri} + key = f"{self.key}/items{utils.joinArgs(args)}" + self._server.query(key, method=self._server._session.put) - args = {'uri': uri} - key = f"{self.key}/items{utils.joinArgs(args)}" - self._server.query(key, method=self._server._session.put) return self @deprecated('use "removeItems" instead')