Allow pagination (#464)

Allow pagination on fetchItems
This commit is contained in:
Steffen Fredriksen 2020-04-26 21:18:52 +02:00 committed by GitHub
parent 772c41dfdf
commit 6eea7cce0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View file

@ -146,11 +146,21 @@ class PlexObject(object):
""" Load the specified key to find and build all items with the specified tag
and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
on how this is used.
Use container_start and container_size for pagination.
"""
url_kw = {}
for key, value in dict(kwargs).items():
if key == "container_start":
url_kw["X-Plex-Container-Start"] = kwargs.pop(key)
if key == "container_size":
url_kw["X-Plex-Container-Size"] = kwargs.pop(key)
if ekey is None:
raise BadRequest('ekey was not provided')
data = self._server.query(ekey)
data = self._server.query(ekey, params=url_kw)
items = self.findItems(data, cls, ekey, **kwargs)
librarySectionID = data.attrib.get('librarySectionID')
if librarySectionID:
for item in items:

View file

@ -332,6 +332,8 @@ class LibrarySection(PlexObject):
type (str): Type of content section represents (movie, artist, photo, show).
updatedAt (datetime): Datetime this library section was last updated.
uuid (str): Unique id for this section (32258d7c-3e6c-4ac5-98ad-bad7a3b78c63)
totalSize (int): Total number of item in the library
"""
ALLOWED_FILTERS = ()
ALLOWED_SORT = ()
@ -355,6 +357,17 @@ class LibrarySection(PlexObject):
self.type = data.attrib.get('type')
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt'))
self.uuid = data.attrib.get('uuid')
# Private attrs as we dont want a reload.
self._total_size = None
@property
def totalSize(self):
if self._total_size is None:
part = '/library/sections/%s/all?X-Plex-Container-Start=0&X-Plex-Container-Size=1' % self.key
data = self._server.query(part)
self._total_size = int(data.attrib.get("totalSize"))
return self._total_size
def delete(self):
""" Delete a library section. """

View file

@ -50,6 +50,12 @@ def test_library_section_get_movie(plex):
assert plex.library.section("Movies").get("Sita Sings the Blues")
def test_library_section_movies_all(movies):
# size should always be none unless pagenation is being used.
assert movies.totalSize == 4
assert len(movies.all(container_start=0, container_size=1)) == 1
def test_library_section_delete(movies, patched_http_call):
movies.delete()