Skip to content

Commit 6eea7cc

Browse files
authored
Allow pagination (pushingkarmaorg#464)
Allow pagination on fetchItems
1 parent 772c41d commit 6eea7cc

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎plexapi/base.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,21 @@ def fetchItems(self, ekey, cls=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.
149+
150+
Use container_start and container_size for pagination.
149151
"""
152+
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)
158+
150159
if ekey is None:
151160
raise BadRequest('ekey was not provided')
152-
data = self._server.query(ekey)
161+
data = self._server.query(ekey, params=url_kw)
153162
items = self.findItems(data, cls, ekey, **kwargs)
163+
154164
librarySectionID = data.attrib.get('librarySectionID')
155165
if librarySectionID:
156166
for item in items:

‎plexapi/library.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ class LibrarySection(PlexObject):
332332
type (str): Type of content section represents (movie, artist, photo, show).
333333
updatedAt (datetime): Datetime this library section was last updated.
334334
uuid (str): Unique id for this section (32258d7c-3e6c-4ac5-98ad-bad7a3b78c63)
335+
totalSize (int): Total number of item in the library
336+
335337
"""
336338
ALLOWED_FILTERS = ()
337339
ALLOWED_SORT = ()
@@ -355,6 +357,17 @@ def _loadData(self, data):
355357
self.type = data.attrib.get('type')
356358
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt'))
357359
self.uuid = data.attrib.get('uuid')
360+
# Private attrs as we dont want a reload.
361+
self._total_size = None
362+
363+
@property
364+
def totalSize(self):
365+
if self._total_size is None:
366+
part = '/library/sections/%s/all?X-Plex-Container-Start=0&X-Plex-Container-Size=1' % self.key
367+
data = self._server.query(part)
368+
self._total_size = int(data.attrib.get("totalSize"))
369+
370+
return self._total_size
358371

359372
def delete(self):
360373
""" Delete a library section. """

‎tests/test_library.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def test_library_section_get_movie(plex):
5050
assert plex.library.section("Movies").get("Sita Sings the Blues")
5151

5252

53+
def test_library_section_movies_all(movies):
54+
# size should always be none unless pagenation is being used.
55+
assert movies.totalSize == 4
56+
assert len(movies.all(container_start=0, container_size=1)) == 1
57+
58+
5359
def test_library_section_delete(movies, patched_http_call):
5460
movies.delete()
5561

0 commit comments

Comments
 (0)