Merge pull request #631 from JonnyWong16/feature/librarysection_all

Update LibrarySection.all() for more filtering/sorting and faster results
This commit is contained in:
Steffen Fredriksen 2020-12-30 21:42:56 +01:00 committed by GitHub
commit c6ac8abd9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 16 deletions

View file

@ -2,7 +2,7 @@
from urllib.parse import quote, quote_plus, unquote, urlencode
from plexapi import X_PLEX_CONTAINER_SIZE, log, utils
from plexapi.base import PlexObject, PlexPartialObject
from plexapi.base import OPERATORS, PlexObject, PlexPartialObject
from plexapi.exceptions import BadRequest, NotFound
from plexapi.media import MediaTag
from plexapi.settings import Setting
@ -437,18 +437,12 @@ class LibrarySection(PlexObject):
key = '/library/sections/%s/all?title=%s' % (self.key, quote(title, safe=''))
return self.fetchItem(key, title__iexact=title)
def all(self, sort=None, **kwargs):
""" Returns a list of media from this library section.
Parameters:
sort (string): The sort string
def all(self, libtype=None, **kwargs):
""" Returns a list of all items from this library section.
See description of :func:`plexapi.library.LibrarySection.search()` for details about filtering / sorting.
"""
sortStr = ''
if sort is not None:
sortStr = '?sort=' + sort
key = '/library/sections/%s/all%s' % (self.key, sortStr)
return self.fetchItems(key, **kwargs)
libtype = libtype or self.TYPE
return self.search(libtype=libtype, **kwargs)
def folders(self):
""" Returns a list of available :class:`~plexapi.library.Folder` for this library section.
@ -672,8 +666,10 @@ class LibrarySection(PlexObject):
"""
# cleanup the core arguments
args = {}
for category, value in kwargs.items():
args[category] = self._cleanSearchFilter(category, value, libtype)
for category, value in list(kwargs.items()):
if category.split('__')[-1] not in OPERATORS:
args[category] = self._cleanSearchFilter(category, value, libtype)
del kwargs[category]
if title is not None:
args['title'] = title
if sort is not None:
@ -690,7 +686,7 @@ class LibrarySection(PlexObject):
while True:
key = '/library/sections/%s/all%s' % (self.key, utils.joinArgs(args))
subresults = self.fetchItems(key, container_start=container_start,
container_size=container_size)
container_size=container_size, **kwargs)
if not len(subresults):
if offset > self.totalSize:
log.info("container_start is higher then the number of items in the library")
@ -1054,6 +1050,13 @@ class PhotoSection(LibrarySection):
CONTENT_TYPE = 'photo'
METADATA_TYPE = 'photo'
def all(self, libtype=None, **kwargs):
""" Returns a list of all items from this library section.
See description of :func:`plexapi.library.LibrarySection.search()` for details about filtering / sorting.
"""
libtype = libtype or 'photoalbum'
return self.search(libtype=libtype, **kwargs)
def collections(self, **kwargs):
raise NotImplementedError('Collections are not available for a Photo library.')

View file

@ -54,7 +54,7 @@ def test_library_section_get_movie(plex):
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
assert len(movies.all(container_start=0, container_size=1, maxresults=1)) == 1
def test_library_section_delete(movies, patched_http_call):