From 3631e723bc1a00b3afd77139686482dc918e7e04 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Mon, 1 Aug 2022 14:47:16 -0700 Subject: [PATCH] Add last played metadata to library stats --- plexpy/datafactory.py | 50 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index c87bcecd..eb4c18ae 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -1038,12 +1038,21 @@ class DataFactory(object): library_stats = [] try: - query = 'SELECT section_id, section_name, section_type, thumb AS library_thumb, ' \ - 'custom_thumb_url AS custom_thumb, art AS library_art, custom_art_url AS custom_art, ' \ - 'count, parent_count, child_count ' \ - 'FROM library_sections ' \ - 'WHERE section_id IN (%s) AND deleted_section = 0 ' \ - 'ORDER BY section_type, count DESC, parent_count DESC, child_count DESC ' % ','.join(library_cards) + query = 'SELECT ls.id, ls.section_id, ls.section_name, ls.section_type, ls.thumb AS library_thumb, ' \ + 'ls.custom_thumb_url AS custom_thumb, ls.art AS library_art, ls.custom_art_url AS custom_art, ' \ + 'ls.count, ls.parent_count, ls.child_count, ' \ + 'sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, ' \ + 'shm.media_index, shm.parent_media_index, ' \ + 'sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, ' \ + 'sh.user, sh.user_id, sh.player, sh.section_id, ' \ + 'shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, ' \ + 'MAX(sh.started) AS last_watch ' \ + 'FROM library_sections AS ls ' \ + 'JOIN session_history AS sh ON ls.section_id = sh.section_id ' \ + 'JOIN session_history_metadata AS shm ON sh.id = shm.id ' \ + 'WHERE ls.section_id IN (%s) AND ls.deleted_section = 0 ' \ + 'GROUP BY ls.id ' \ + 'ORDER BY ls.section_type, ls.count DESC, ls.parent_count DESC, ls.child_count DESC ' % ','.join(library_cards) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_library_stats: %s." % e) @@ -1062,17 +1071,40 @@ class DataFactory(object): else: library_art = item['library_art'] + if not item['grandparent_thumb'] or item['grandparent_thumb'] == '': + thumb = item['thumb'] + else: + thumb = item['grandparent_thumb'] + library = {'section_id': item['section_id'], 'section_name': item['section_name'], 'section_type': item['section_type'], - 'thumb': library_thumb, - 'art': library_art, + 'library_thumb': library_thumb, + 'library_art': library_art, 'count': item['count'], 'child_count': item['parent_count'], - 'grandchild_count': item['child_count'] + 'grandchild_count': item['child_count'], + 'thumb': thumb, + 'grandparent_thumb': item['grandparent_thumb'], + 'art': item['art'], + 'title': item['full_title'], + 'grandparent_title': item['grandparent_title'], + 'grandchild_title': item['title'], + 'year': item['year'], + 'media_index': item['media_index'], + 'parent_media_index': item['parent_media_index'], + 'rating_key': item['rating_key'], + 'grandparent_rating_key': item['grandparent_rating_key'], + 'media_type': item['media_type'], + 'content_rating': item['content_rating'], + 'labels': item['labels'].split(';') if item['labels'] else (), + 'live': item['live'], + 'guid': item['guid'], + 'row_id': item['id'] } library_stats.append(library) + library_stats = session.mask_session_info(library_stats) library_stats = {k: list(v) for k, v in groupby(library_stats, key=lambda x: x['section_type'])} return library_stats