From cfb5eff8388793b9af4c6165e3cca77466722f0d Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:26:58 -0700 Subject: [PATCH] Set correct server instance for playlist items --- plexapi/base.py | 2 +- plexapi/myplex.py | 4 ++-- plexapi/playlist.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/plexapi/base.py b/plexapi/base.py index a7fa82eee..4a05eb952 100644 --- a/plexapi/base.py +++ b/plexapi/base.py @@ -17,7 +17,7 @@ MediaContainerT = TypeVar("MediaContainerT", bound="MediaContainer") USER_DONT_RELOAD_FOR_KEYS = set() -_DONT_RELOAD_FOR_KEYS = {'key'} +_DONT_RELOAD_FOR_KEYS = {'key', 'sourceURI'} OPERATORS = { 'exact': lambda v, q: v == q, 'iexact': lambda v, q: v.lower() == q.lower(), diff --git a/plexapi/myplex.py b/plexapi/myplex.py index 24e32e6ba..448a2649a 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -283,10 +283,10 @@ def resource(self, name): """ Returns the :class:`~plexapi.myplex.MyPlexResource` that matches the name specified. Parameters: - name (str): Name to match against. + name (str): Name or machine identifier to match against. """ for resource in self.resources(): - if resource.name.lower() == name.lower(): + if resource.name.lower() == name.lower() or resource.clientIdentifier == name: return resource raise NotFound(f'Unable to find resource {name}') diff --git a/plexapi/playlist.py b/plexapi/playlist.py index dda3bdf50..e2c4da635 100644 --- a/plexapi/playlist.py +++ b/plexapi/playlist.py @@ -190,6 +190,20 @@ def items(self): if self._items is None: key = f'{self.key}/items' items = self.fetchItems(key) + + # Cache server connections to avoid reconnecting for each item + _servers = {} + for item in items: + if item.sourceURI: + serverID = item.sourceURI.split('/')[2] + if serverID not in _servers: + try: + _servers[serverID] = self._server.myPlexAccount().resource(serverID).connect() + except NotFound: + # Override the server connection with None if the server is not found + _servers[serverID] = None + item._server = _servers[serverID] + self._items = items return self._items