diff --git a/plexapi/playqueue.py b/plexapi/playqueue.py index 2ee7d3b25..ca6fda639 100644 --- a/plexapi/playqueue.py +++ b/plexapi/playqueue.py @@ -66,6 +66,8 @@ def _loadData(self, data): self.selectedItem = self[self.playQueueSelectedItemOffset] def __getitem__(self, key): + if not self.items: + return None return self.items[key] def __len__(self): @@ -93,6 +95,45 @@ def getQueueItem(self, item): else: raise BadRequest("{item} not valid for this PlayQueue".format(item=item)) + @classmethod + def get( + cls, + server, + playQueueID, + own=False, + center=None, + window=50, + includeBefore=True, + includeAfter=True, + ): + """Retrieve an existing :class:`~plexapi.playqueue.PlayQueue` by identifier. + + Parameters: + server (:class:`~plexapi.server.PlexServer`): Server you are connected to. + playQueueID (int): Identifier of an existing PlayQueue. + own (bool, optional): If server should transfer ownership. + center (int, optional): The playQueueItemID of the center of the window. Does not change selectedItem. + window (int, optional): Number of items to return from each side of the center item. + includeBefore (bool, optional): + Include items before the center, defaults True. Does not include center if False. + includeAfter (bool, optional): + Include items after the center, defaults True. Does not include center if False. + """ + args = { + "own": utils.cast(int, own), + "window": window, + "includeBefore": utils.cast(int, includeBefore), + "includeAfter": utils.cast(int, includeAfter), + } + if center: + args["center"] = center + + path = "/playQueues/{playQueueID}{args}".format(playQueueID=playQueueID, args=utils.joinArgs(args)) + data = server.query(path, method=server._session.get) + c = cls(server, data, initpath=path) + c._server = server + return c + @classmethod def create( cls, diff --git a/tests/test_playqueue.py b/tests/test_playqueue.py index 946d24d74..6cc7deffa 100644 --- a/tests/test_playqueue.py +++ b/tests/test_playqueue.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from plexapi.exceptions import BadRequest +from plexapi.playqueue import PlayQueue import pytest @@ -138,3 +139,11 @@ def test_create_playqueue_from_playlist(plex, album): assert len(pq) == 2 * len(playlist) finally: playlist.delete() + + +def test_lookup_playqueue(plex, movie): + pq = PlayQueue.create(plex, movie) + pq_id = pq.playQueueID + pq2 = PlayQueue.get(plex, pq_id) + assert pq.playQueueID == pq2.playQueueID + assert pq.items == pq2.items