Skip to content

Commit 02e3ddf

Browse files
authored
Add maxresults parameter to MyPlexAccount.watchlist() (#1009)
* Add `maxresults` parameter to watchlist * Test watchlist maxresults
1 parent e640f57 commit 02e3ddf

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

plexapi/myplex.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from xml.etree import ElementTree
88

99
import requests
10-
from plexapi import (BASE_HEADERS, CONFIG, TIMEOUT, X_PLEX_ENABLE_FAST_CONNECT,
11-
X_PLEX_IDENTIFIER, log, logfilter, utils)
10+
from plexapi import (BASE_HEADERS, CONFIG, TIMEOUT, X_PLEX_CONTAINER_SIZE,
11+
X_PLEX_ENABLE_FAST_CONNECT, X_PLEX_IDENTIFIER, log, logfilter, utils)
1212
from plexapi.base import PlexObject
1313
from plexapi.client import PlexClient
1414
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
@@ -761,7 +761,7 @@ def tidal(self):
761761
data = self.query(f'{self.MUSIC}/hubs')
762762
return self.findItems(data)
763763

764-
def watchlist(self, filter=None, sort=None, libtype=None, **kwargs):
764+
def watchlist(self, filter=None, sort=None, libtype=None, maxresults=9999999, **kwargs):
765765
""" Returns a list of :class:`~plexapi.video.Movie` and :class:`~plexapi.video.Show` items in the user's watchlist.
766766
Note: The objects returned are from Plex's online metadata. To get the matching item on a Plex server,
767767
search for the media using the guid.
@@ -773,6 +773,7 @@ def watchlist(self, filter=None, sort=None, libtype=None, **kwargs):
773773
``titleSort`` (Title), ``originallyAvailableAt`` (Release Date), or ``rating`` (Critic Rating).
774774
``dir`` can be ``asc`` or ``desc``.
775775
libtype (str, optional): 'movie' or 'show' to only return movies or shows, otherwise return all items.
776+
maxresults (int, optional): Only return the specified number of results.
776777
**kwargs (dict): Additional custom filters to apply to the search results.
777778
778779
@@ -800,9 +801,18 @@ def watchlist(self, filter=None, sort=None, libtype=None, **kwargs):
800801
if libtype:
801802
params['type'] = utils.searchType(libtype)
802803

804+
params['X-Plex-Container-Start'] = 0
805+
params['X-Plex-Container-Size'] = min(X_PLEX_CONTAINER_SIZE, maxresults)
803806
params.update(kwargs)
804-
data = self.query(f'{self.METADATA}/library/sections/watchlist/{filter}', params=params)
805-
return self._toOnlineMetadata(self.findItems(data))
807+
808+
results, subresults = [], '_init'
809+
while subresults and maxresults > len(results):
810+
data = self.query(f'{self.METADATA}/library/sections/watchlist/{filter}', params=params)
811+
subresults = self.findItems(data)
812+
results += subresults[:maxresults - len(results)]
813+
params['X-Plex-Container-Start'] += params['X-Plex-Container-Size']
814+
815+
return self._toOnlineMetadata(results)
806816

807817
def onWatchlist(self, item):
808818
""" Returns True if the item is on the user's watchlist.

tests/test_myplex.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ def test_myplex_watchlist(account, movie, show, artist):
296296
with pytest.raises(BadRequest):
297297
account.addToWatchlist(movie)
298298

299+
# Test retrieving maxresults from watchlist
300+
watchlist = account.watchlist(maxresults=1)
301+
assert len(watchlist) == 1
302+
299303
# Remove multiple items from watchlist
300304
account.removeFromWatchlist([movie, show])
301305
assert not movie.onWatchlist(account) and not show.onWatchlist(account)

0 commit comments

Comments
 (0)