From b59dd3303f021e43f70e087ae59ad4e3d0bdc54c Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Mon, 2 Nov 2020 17:18:46 -0600 Subject: [PATCH 1/3] Add method to retrieve a PlayQueue --- plexapi/playqueue.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/plexapi/playqueue.py b/plexapi/playqueue.py index 2ee7d3b25..7b3ef51b9 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,43 @@ 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, From e22bc81f35b01466662dcf2453a0047f8888fcff Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Mon, 2 Nov 2020 19:24:25 -0600 Subject: [PATCH 2/3] Docstring flake adjustment --- plexapi/playqueue.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plexapi/playqueue.py b/plexapi/playqueue.py index 7b3ef51b9..ca6fda639 100644 --- a/plexapi/playqueue.py +++ b/plexapi/playqueue.py @@ -114,8 +114,10 @@ def get( 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. + 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), From 3bdb09a1c74149ef7115f73bd47658cdeeb9782f Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Mon, 30 Nov 2020 16:04:51 -0600 Subject: [PATCH 3/3] Add test --- tests/test_playqueue.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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