Add totalDuration and totalStorage properties to LibrarySection (#851)

* Add LibrarySection properties totalDuration and totalStorage

* Add tests for LibrarySection totalDuration and totalStorage
This commit is contained in:
JonnyWong16 2021-11-20 16:29:08 -08:00 committed by GitHub
parent 777ea754be
commit 8815cda444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View file

@ -356,6 +356,9 @@ class LibrarySection(PlexObject):
self._filterTypes = None
self._fieldTypes = None
self._totalViewSize = None
self._totalSize = None
self._totalDuration = None
self._totalStorage = None
def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, **kwargs):
""" Load the specified key to find and build all items with the specified tag
@ -394,7 +397,36 @@ class LibrarySection(PlexObject):
@property
def totalSize(self):
""" Returns the total number of items in the library for the default library type. """
return self.totalViewSize(includeCollections=False)
if self._totalSize is None:
self._totalSize = self.totalViewSize(includeCollections=False)
return self._totalSize
@property
def totalDuration(self):
""" Returns the total duration (in milliseconds) of items in the library. """
if self._totalDuration is None:
self._getTotalDurationStorage()
return self._totalDuration
@property
def totalStorage(self):
""" Returns the total storage (in bytes) of items in the library. """
if self._totalStorage is None:
self._getTotalDurationStorage()
return self._totalStorage
def _getTotalDurationStorage(self):
""" Queries the Plex server for the total library duration and storage and caches the values. """
data = self._server.query('/media/providers?includeStorage=1')
xpath = (
'./MediaProvider[@identifier="com.plexapp.plugins.library"]'
'/Feature[@type="content"]'
'/Directory[@id="%s"]'
) % self.key
directory = next(iter(data.findall(xpath)), None)
if directory:
self._totalDuration = utils.cast(int, directory.attrib.get('durationTotal'))
self._totalStorage = utils.cast(int, directory.attrib.get('storageTotal'))
def totalViewSize(self, libtype=None, includeCollections=True):
""" Returns the total number of items in the library for a specified libtype.

View file

@ -61,7 +61,6 @@ def test_library_MovieSection_getGuid(movies, movie):
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, maxresults=1)) == 1
@ -79,6 +78,14 @@ def test_library_section_movies_all_guids(movies):
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.remove('guids')
def test_library_section_totalDuration(tvshows):
assert utils.is_int(tvshows.totalDuration)
def test_library_section_totalStorage(tvshows):
assert utils.is_int(tvshows.totalStorage)
def test_library_section_totalViewSize(tvshows):
assert tvshows.totalViewSize() == 2
assert tvshows.totalViewSize(libtype="show") == 2