Skip to content

Commit 5a2f14e

Browse files
committed
Update based on comments.
1 parent 1ba8fe7 commit 5a2f14e

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

plexapi/base.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,21 @@ def fetchItem(self, ekey, cls=None, **kwargs):
142142
clsname = cls.__name__ if cls else 'None'
143143
raise NotFound('Unable to find elem: cls=%s, attrs=%s' % (clsname, kwargs))
144144

145-
def fetchItems(self, ekey, cls=None, **kwargs):
145+
def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, **kwargs):
146146
""" Load the specified key to find and build all items with the specified tag
147147
and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
148148
on how this is used.
149149
150-
Use container_start and container_size for pagination.
150+
Parameters:
151+
container_start (None, int): offset to get a subset of the data
152+
container_size (None, int): How many items in data
153+
151154
"""
152155
url_kw = {}
153-
for key, value in dict(kwargs).items():
154-
if key == "container_start":
155-
url_kw["X-Plex-Container-Start"] = kwargs.pop(key)
156-
if key == "container_size":
157-
url_kw["X-Plex-Container-Size"] = kwargs.pop(key)
156+
if container_start is not None:
157+
url_kw["X-Plex-Container-Start"] = container_start
158+
if container_size is not None:
159+
url_kw["X-Plex-Container-Size"] = container_size
158160

159161
if ekey is None:
160162
raise BadRequest('ekey was not provided')

plexapi/library.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -360,19 +360,21 @@ def _loadData(self, data):
360360
# Private attrs as we dont want a reload.
361361
self._total_size = None
362362

363-
def fetchItems(self, ekey, cls=None, **kwargs):
363+
def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, **kwargs):
364364
""" Load the specified key to find and build all items with the specified tag
365365
and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
366366
on how this is used.
367367
368-
Use container_start and container_size for pagination.
368+
Parameters:
369+
container_start (None, int): offset to get a subset of the data
370+
container_size (None, int): How many items in data
371+
369372
"""
370373
url_kw = {}
371-
for key, value in dict(kwargs).items():
372-
if key == "container_start":
373-
url_kw["X-Plex-Container-Start"] = kwargs.pop(key)
374-
if key == "container_size":
375-
url_kw["X-Plex-Container-Size"] = kwargs.pop(key)
374+
if container_start is not None:
375+
url_kw["X-Plex-Container-Start"] = container_start
376+
if container_size is not None:
377+
url_kw["X-Plex-Container-Size"] = container_size
376378

377379
if ekey is None:
378380
raise BadRequest('ekey was not provided')
@@ -534,10 +536,9 @@ def listChoices(self, category, libtype=None, **kwargs):
534536
key = '/library/sections/%s/%s%s' % (self.key, category, utils.joinArgs(args))
535537
return self.fetchItems(key, cls=FilterChoice)
536538

537-
def search(self, title=None, sort=None, maxresults=999999,
538-
libtype=None, container_size=X_PLEX_CONTAINER_SIZE, **kwargs):
539-
""" Search the library. If there are many results, they will be fetched from the server
540-
in batches of X_PLEX_CONTAINER_SIZE amounts. If you're only looking for the first <num>
539+
def search(self, title=None, sort=None, maxresults=None,
540+
libtype=None, container_start=0, container_size=X_PLEX_CONTAINER_SIZE, **kwargs):
541+
""" Search the library. The http requests will be batched in container_size. If you're only looking for the first <num>
541542
results, it would be wise to set the maxresults option to that amount so this functions
542543
doesn't iterate over all results on the server.
543544
@@ -548,6 +549,7 @@ def search(self, title=None, sort=None, maxresults=999999,
548549
maxresults (int): Only return the specified number of results (optional).
549550
libtype (str): Filter results to a spcifiec libtype (movie, show, episode, artist,
550551
album, track; optional).
552+
container_start (int): default 0
551553
container_size (int): default X_PLEX_CONTAINER_SIZE in your config file.
552554
**kwargs (dict): Any of the available filters for the current library section. Partial string
553555
matches allowed. Multiple matches OR together. Negative filtering also possible, just add an
@@ -583,22 +585,30 @@ def search(self, title=None, sort=None, maxresults=999999,
583585

584586
results = []
585587
subresults = []
586-
args['X-Plex-Container-Start'] = 0
587-
args['X-Plex-Container-Size'] = container_size
588+
589+
if maxresults is not None:
590+
container_size = min(container_size, maxresults)
588591
while True:
589592
key = '/library/sections/%s/all%s' % (self.key, utils.joinArgs(args))
590-
subresults = self.fetchItems(key)
593+
subresults = self.fetchItems(key, container_start=container_start,
594+
container_size=container_size)
591595
results.extend(subresults)
592596

593597
# self.totalSize is not used as a condition in the while loop as
594598
# this require a additional http request.
595599
# self.totalSize is updated from .fetchItems
596-
if self.totalSize <= len(results):
600+
if maxresults is not None:
601+
res = min(maxresults, self.totalSize)
602+
container_size = min(container_size, maxresults - len(results))
603+
else:
604+
res = self.totalSize
605+
606+
if res <= len(results):
597607
break
598608

599-
args['X-Plex-Container-Start'] += args['X-Plex-Container-Size']
609+
container_start += container_size
600610

601-
return results[:maxresults]
611+
return results
602612

603613
def _cleanSearchFilter(self, category, value, libtype=None):
604614
# check a few things before we begin

0 commit comments

Comments
 (0)