Skip to content

Commit 66df034

Browse files
committed
add fetchItems to library
use totalSize so we dont do any more http requests then needed.
1 parent 91d76a5 commit 66df034

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

plexapi/library.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,33 @@ 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):
364+
""" Load the specified key to find and build all items with the specified tag
365+
and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
366+
on how this is used.
367+
368+
Use container_start and container_size for pagination.
369+
"""
370+
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)
376+
377+
if ekey is None:
378+
raise BadRequest('ekey was not provided')
379+
data = self._server.query(ekey, params=url_kw)
380+
381+
self._total_size = int(data.attrib.get("totalSize"))
382+
items = self.findItems(data, cls, ekey, **kwargs)
383+
384+
librarySectionID = data.attrib.get('librarySectionID')
385+
if librarySectionID:
386+
for item in items:
387+
item.librarySectionID = librarySectionID
388+
return items
389+
363390
@property
364391
def totalSize(self):
365392
if self._total_size is None:
@@ -549,18 +576,20 @@ def search(self, title=None, sort=None, maxresults=999999, libtype=None, contain
549576
args['sort'] = self._cleanSearchSort(sort)
550577
if libtype is not None:
551578
args['type'] = utils.searchType(libtype)
552-
# iterate over the results
579+
553580
results = []
554581
subresults = []
555582
args['X-Plex-Container-Start'] = 0
556583
args['X-Plex-Container-Size'] = container_size or X_PLEX_CONTAINER_SIZE
557584
while True:
558585
key = '/library/sections/%s/all%s' % (self.key, utils.joinArgs(args))
559586
subresults = self.fetchItems(key)
560-
if not len(subresults):
587+
results.extend(subresults)
588+
589+
# this si not set as condition in the while as
590+
# this require a additional http request.
591+
if self.totalSize <= len(results):
561592
break
562-
else:
563-
results.extend(subresults)
564593

565594
args['X-Plex-Container-Start'] += args['X-Plex-Container-Size']
566595
return results[:maxresults]

0 commit comments

Comments
 (0)