From aed84852c0b3b82084e31ea04fca8ee9833e15bb Mon Sep 17 00:00:00 2001 From: herby2212 <12448284+herby2212@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:18:56 +0100 Subject: [PATCH] get_libray_media_stats API call --- plexpy/libraries.py | 35 +++++++++++++++++++++++++++++++++++ plexpy/plex.py | 9 ++++++++- plexpy/webserve.py | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/plexpy/libraries.py b/plexpy/libraries.py index 19ab6ff4..82768123 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -143,6 +143,36 @@ def has_library_type(section_type): result = monitor_db.select_single(query=query, args=args) return bool(result) +def get_library_media_stats(section_id=None): + plex = Plex(token=session.get_session_user_token()) + + default_return = { + 'total_size': 0, + 'total_storage': 0, + 'total_duration': 0 + } + + if section_id and not str(section_id).isdigit(): + logger.warn("Tautulli Libraries :: Library media stats requested but invalid section_id provided.") + return default_return + + if not session.allow_session_library(section_id): + logger.warn("Tautulli Libraries :: Library media stats requested but library is not allowed for this session.") + return default_return + + library = plex.get_library(section_id) + + if library is None: + logger.warn("Tautulli Libraries :: Library media stats requested but no library was found section_id %s.", section_id) + return default_return + + library_media_stats = { + 'total_size': library.totalSize, + 'total_storage': library.totalStorage, + 'total_duration': library.totalDuration + } + + return library_media_stats def get_collections(section_id=None): plex = Plex(token=session.get_session_user_token()) @@ -409,18 +439,23 @@ class Libraries(object): else: library_art = item['library_art'] + library_media_stats = get_library_media_stats(item['section_id']) + row = {'row_id': item['row_id'], 'server_id': item['server_id'], 'section_id': item['section_id'], 'section_name': item['section_name'], 'section_type': item['section_type'], 'count': item['count'], + 'total_size': library_media_stats['total_size'], 'parent_count': item['parent_count'], 'child_count': item['child_count'], 'library_thumb': library_thumb, 'library_art': library_art, 'plays': item['plays'], + 'total_storage': library_media_stats['total_storage'], 'duration': item['duration'], + 'total_duration': library_media_stats['total_duration'], 'last_accessed': item['last_accessed'], 'history_row_id': item['history_row_id'], 'last_played': item['last_played'], diff --git a/plexpy/plex.py b/plexpy/plex.py index 29379053..d25bd478 100644 --- a/plexpy/plex.py +++ b/plexpy/plex.py @@ -59,7 +59,14 @@ class Plex(object): self.PlexServer = PlexObject(url, token) def get_library(self, section_id): - return self.PlexServer.library.sectionByID(int(section_id)) + from plexapi.exceptions import NotFound + + try: + library = self.PlexServer.library.sectionByID(int(section_id)) + except NotFound: + library = None + + return library def get_library_items(self, section_id): return self.get_library(section_id).all() diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 12daa164..7be86c04 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -540,6 +540,9 @@ class WebInterface(object): "section_type": "Show", "server_id": "ds48g4r354a8v9byrrtr697g3g79w", "thumb": "/library/metadata/153036/thumb/1462175062", + "total_duration": 3048551210, + "total_size": 62, + "total_storage": 1866078986762, "year": 2016 }, {...}, @@ -561,7 +564,9 @@ class WebInterface(object): ("last_accessed", True, False), ("last_played", True, True), ("plays", True, False), - ("duration", True, False)] + ("duration", True, False), + ("total_storage", True, False), + ("total_duration", True, False)] kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "section_name") grouping = helpers.bool_true(grouping, return_none=True) @@ -695,6 +700,32 @@ class WebInterface(object): except: return "Failed to update library." + @cherrypy.expose + @requireAuth(member_of("admin")) + @addtoapi() + def get_library_media_stats(self, section_id=None): + """ Get the media stats of a library section on Tautulli. + + ``` + Required parameters: + section_id (str): The id of the Plex library section + + Optional parameters: + None + + Returns: + { + "total_duration": 3048551210, + "total_size": 62, + "total_storage": 1866078986762 + } + ``` + """ + + logger.info("Getting library media stats for section %s.", section_id) + + return libraries.get_library_media_stats(section_id) + @cherrypy.expose @requireAuth() def library_watch_time_stats(self, section_id=None, **kwargs):