Skip to content

Commit 8815cda

Browse files
authored
Add totalDuration and totalStorage properties to LibrarySection (#851)
* Add LibrarySection properties totalDuration and totalStorage * Add tests for LibrarySection totalDuration and totalStorage
1 parent 777ea75 commit 8815cda

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

plexapi/library.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ def _loadData(self, data):
356356
self._filterTypes = None
357357
self._fieldTypes = None
358358
self._totalViewSize = None
359+
self._totalSize = None
360+
self._totalDuration = None
361+
self._totalStorage = None
359362

360363
def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, **kwargs):
361364
""" Load the specified key to find and build all items with the specified tag
@@ -394,7 +397,36 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None,
394397
@property
395398
def totalSize(self):
396399
""" Returns the total number of items in the library for the default library type. """
397-
return self.totalViewSize(includeCollections=False)
400+
if self._totalSize is None:
401+
self._totalSize = self.totalViewSize(includeCollections=False)
402+
return self._totalSize
403+
404+
@property
405+
def totalDuration(self):
406+
""" Returns the total duration (in milliseconds) of items in the library. """
407+
if self._totalDuration is None:
408+
self._getTotalDurationStorage()
409+
return self._totalDuration
410+
411+
@property
412+
def totalStorage(self):
413+
""" Returns the total storage (in bytes) of items in the library. """
414+
if self._totalStorage is None:
415+
self._getTotalDurationStorage()
416+
return self._totalStorage
417+
418+
def _getTotalDurationStorage(self):
419+
""" Queries the Plex server for the total library duration and storage and caches the values. """
420+
data = self._server.query('/media/providers?includeStorage=1')
421+
xpath = (
422+
'./MediaProvider[@identifier="com.plexapp.plugins.library"]'
423+
'/Feature[@type="content"]'
424+
'/Directory[@id="%s"]'
425+
) % self.key
426+
directory = next(iter(data.findall(xpath)), None)
427+
if directory:
428+
self._totalDuration = utils.cast(int, directory.attrib.get('durationTotal'))
429+
self._totalStorage = utils.cast(int, directory.attrib.get('storageTotal'))
398430

399431
def totalViewSize(self, libtype=None, includeCollections=True):
400432
""" Returns the total number of items in the library for a specified libtype.

tests/test_library.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def test_library_MovieSection_getGuid(movies, movie):
6161

6262

6363
def test_library_section_movies_all(movies):
64-
# size should always be none unless pagenation is being used.
6564
assert movies.totalSize == 4
6665
assert len(movies.all(container_start=0, container_size=1, maxresults=1)) == 1
6766

@@ -79,6 +78,14 @@ def test_library_section_movies_all_guids(movies):
7978
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.remove('guids')
8079

8180

81+
def test_library_section_totalDuration(tvshows):
82+
assert utils.is_int(tvshows.totalDuration)
83+
84+
85+
def test_library_section_totalStorage(tvshows):
86+
assert utils.is_int(tvshows.totalStorage)
87+
88+
8289
def test_library_section_totalViewSize(tvshows):
8390
assert tvshows.totalViewSize() == 2
8491
assert tvshows.totalViewSize(libtype="show") == 2

0 commit comments

Comments
 (0)