From ac3bffcc05e2abfbccfc3315f646375a2fe1a8ed Mon Sep 17 00:00:00 2001 From: MoojMidge <56883549+MoojMidge@users.noreply.github.com> Date: Fri, 13 Dec 2024 22:55:24 +1100 Subject: [PATCH] Improve caching and updating of playlist details #1024 - Update playlist details every day - Cache details of own playlists for later use --- .../youtube_plugin/youtube/client/youtube.py | 2 +- .../youtube/helper/resource_manager.py | 26 ++++++++++++++++++- .../lib/youtube_plugin/youtube/provider.py | 4 +-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/resources/lib/youtube_plugin/youtube/client/youtube.py b/resources/lib/youtube_plugin/youtube/client/youtube.py index efac5edfe..6006fa35f 100644 --- a/resources/lib/youtube_plugin/youtube/client/youtube.py +++ b/resources/lib/youtube_plugin/youtube/client/youtube.py @@ -883,7 +883,7 @@ def get_channel_sections(self, channel_id, **kwargs): **kwargs) def get_playlists_of_channel(self, channel_id, page_token='', **kwargs): - params = {'part': 'snippet', + params = {'part': 'snippet,status,contentDetails', 'maxResults': str(self.max_results())} if channel_id == 'mine': params['mine'] = True diff --git a/resources/lib/youtube_plugin/youtube/helper/resource_manager.py b/resources/lib/youtube_plugin/youtube/helper/resource_manager.py index 2eb81eee2..44dac2ed7 100644 --- a/resources/lib/youtube_plugin/youtube/helper/resource_manager.py +++ b/resources/lib/youtube_plugin/youtube/helper/resource_manager.py @@ -251,7 +251,7 @@ def get_playlists(self, ids, suppress_errors=False, defer_cache=False): result = {} else: data_cache = context.get_data_cache() - result = data_cache.get_items(ids, data_cache.ONE_MONTH) + result = data_cache.get_items(ids, data_cache.ONE_DAY) to_update = [id_ for id_ in ids if id_ not in result or not result[id_] @@ -415,6 +415,30 @@ def get_related_playlists(self, channel_id, defer_cache=False): return None return item.get('contentDetails', {}).get('relatedPlaylists') + def get_my_playlists(self, channel_id, page_token, defer_cache=False): + context = self._context + client = self._provider.get_client(context) + + result = client.get_playlists_of_channel(channel_id, page_token) + if not result: + return None + + new_data = { + yt_item['id']: yt_item + for yt_item in result.get('items', []) + if yt_item + } + if new_data: + context.debug_log and context.log_debug( + 'ResourceManager.get_my_playlists' + ' - Retrieved new data for playlists' + '\n\tPlaylist IDs: {ids}' + .format(ids=list(new_data)) + ) + self.cache_data(new_data, defer=defer_cache) + + return result + def get_videos(self, ids, live_details=False, diff --git a/resources/lib/youtube_plugin/youtube/provider.py b/resources/lib/youtube_plugin/youtube/provider.py index 0d496a2ff..0e165aef5 100644 --- a/resources/lib/youtube_plugin/youtube/provider.py +++ b/resources/lib/youtube_plugin/youtube/provider.py @@ -470,9 +470,7 @@ def on_channel_playlists(provider, context, re_match): else: result = False - json_data = provider.get_client(context).get_playlists_of_channel( - channel_id, page_token - ) + json_data = resource_manager.get_my_playlists(channel_id, page_token) if not json_data: return result