diff --git a/resources/lib/youtube_plugin/kodion/items/menu_items.py b/resources/lib/youtube_plugin/kodion/items/menu_items.py index 24a252919..04a34d729 100644 --- a/resources/lib/youtube_plugin/kodion/items/menu_items.py +++ b/resources/lib/youtube_plugin/kodion/items/menu_items.py @@ -73,7 +73,10 @@ def play_with(context): def refresh(context): return ( context.localize('refresh'), - 'Container.Refresh' + 'ReplaceWindow(Videos, {0})'.format(context.create_uri( + context.get_path(), + dict(context.get_params(), refresh=True), + )) ) diff --git a/resources/lib/youtube_plugin/kodion/service.py b/resources/lib/youtube_plugin/kodion/service.py index d549a75e1..4761b4ea2 100644 --- a/resources/lib/youtube_plugin/kodion/service.py +++ b/resources/lib/youtube_plugin/kodion/service.py @@ -22,13 +22,6 @@ def run(): context = Context() context.log_debug('YouTube service initialization...') context.get_ui().clear_property('abort_requested') - # wipe function cache on updates/restarts to fix cipher related issues on - # update, valid for one day otherwise - try: - context.get_function_cache().clear() - except Exception: - # prevent service failing due to cache related issues - pass monitor = ServiceMonitor() player = PlayerMonitor(provider=Provider(), context=context) diff --git a/resources/lib/youtube_plugin/kodion/sql_store/function_cache.py b/resources/lib/youtube_plugin/kodion/sql_store/function_cache.py index 1ac1f4fc2..015ed30a2 100644 --- a/resources/lib/youtube_plugin/kodion/sql_store/function_cache.py +++ b/resources/lib/youtube_plugin/kodion/sql_store/function_cache.py @@ -57,39 +57,37 @@ def _create_id_from_func(partial_func): md5_hash.update(str(partial_func.keywords).encode('utf-8')) return md5_hash.hexdigest() - def _get_cached_data(self, partial_func, seconds=None): - cache_id = self._create_id_from_func(partial_func) - return self._get(cache_id, seconds=seconds), cache_id - - def get_cached_only(self, func, *args, **keywords): - partial_func = partial(func, *args, **keywords) + def get_result(self, func, *args, **kwargs): + partial_func = partial(func, *args, **kwargs) # if caching is disabled call the function if not self._enabled: return partial_func() # only return before cached data - data, _ = self._get_cached_data(partial_func) - return data + cache_id = self._create_id_from_func(partial_func) + return self._get(cache_id) - def get(self, func, seconds, *args, **keywords): + def run(self, func, seconds, *args, _refresh=False, **kwargs): """ Returns the cached data of the given function. :param func, function to cache - :param seconds: time to live in seconds + :param seconds: time to live in + :param _refresh: bool, updates cache with new func result :return: """ - - partial_func = partial(func, *args, **keywords) + partial_func = partial(func, *args, **kwargs) # if caching is disabled call the function if not self._enabled: return partial_func() - data, cache_id = self._get_cached_data(partial_func, seconds=seconds) + cache_id = self._create_id_from_func(partial_func) + data = None if _refresh else self._get(cache_id, seconds=seconds) if data is None: data = partial_func() self._set(cache_id, data) + return data def _optimize_item_count(self, limit=-1, defer=False): diff --git a/resources/lib/youtube_plugin/youtube/helper/resource_manager.py b/resources/lib/youtube_plugin/youtube/helper/resource_manager.py index 31f673872..7c124349f 100644 --- a/resources/lib/youtube_plugin/youtube/helper/resource_manager.py +++ b/resources/lib/youtube_plugin/youtube/helper/resource_manager.py @@ -16,7 +16,7 @@ def __init__(self, context, client): self._context = context self._client = client self._data_cache = context.get_data_cache() - self._func_cache = context.get_function_cache() + self._function_cache = context.get_function_cache() self._show_fanart = context.get_settings().get_bool( 'youtube.channel.fanart.show', True ) @@ -30,6 +30,7 @@ def _list_batch(input_list, n=50): yield input_list[i:i + n] def get_channels(self, ids, defer_cache=False): + refresh = self._context.get_param('refresh') updated = [] for channel_id in ids: if not channel_id: @@ -39,9 +40,12 @@ def get_channels(self, ids, defer_cache=False): updated.append(channel_id) continue - data = self._func_cache.get(self._client.get_channel_by_username, - self._func_cache.ONE_DAY, - channel_id) + data = self._function_cache.run( + self._client.get_channel_by_username, + self._function_cache.ONE_DAY, + _refresh=refresh, + username=channel_id + ) items = data.get('items', [{'id': 'mine'}]) try: @@ -52,7 +56,10 @@ def get_channels(self, ids, defer_cache=False): .format(data=data)) ids = updated - result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) + if refresh: + result = {} + else: + result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) to_update = [id_ for id_ in ids if id_ not in result or result[id_].get('partial')] @@ -115,7 +122,11 @@ def get_fanarts(self, channel_ids, defer_cache=False): def get_playlists(self, ids, defer_cache=False): ids = tuple(ids) - result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) + refresh = self._context.get_param('refresh') + if refresh: + result = {} + else: + result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) to_update = [id_ for id_ in ids if id_ not in result or result[id_].get('partial')] @@ -158,6 +169,8 @@ def get_playlist_items(self, ids=None, batch_id=None, defer_cache=False): if not ids and not batch_id: return None + refresh = self._context.get_param('refresh') + if batch_id: ids = [batch_id[0]] page_token = batch_id[1] @@ -174,8 +187,14 @@ def get_playlist_items(self, ids=None, batch_id=None, defer_cache=False): while 1: batch_id = (playlist_id, page_token) batch_ids.append(batch_id) - batch = self._data_cache.get_item(batch_id, - self._data_cache.ONE_HOUR) + if refresh: + batch = None + else: + batch = self._data_cache.get_item( + batch_id, + self._data_cache.ONE_HOUR if page_token + else self._data_cache.ONE_MINUTE * 5 + ) if not batch: to_update.append(batch_id) break @@ -246,7 +265,11 @@ def get_videos(self, suppress_errors=False, defer_cache=False): ids = tuple(ids) - result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) + refresh = self._context.get_param('refresh') + if refresh: + result = {} + else: + result = self._data_cache.get_items(ids, self._data_cache.ONE_MONTH) to_update = [id_ for id_ in ids if id_ not in result or result[id_].get('partial')] diff --git a/resources/lib/youtube_plugin/youtube/helper/signature/cipher.py b/resources/lib/youtube_plugin/youtube/helper/signature/cipher.py index a8549afcd..f6f59b0a7 100644 --- a/resources/lib/youtube_plugin/youtube/helper/signature/cipher.py +++ b/resources/lib/youtube_plugin/youtube/helper/signature/cipher.py @@ -25,12 +25,9 @@ def __init__(self, context, javascript): def get_signature(self, signature): function_cache = self._context.get_function_cache() - json_script = function_cache.get_cached_only(self._load_javascript, - self._javascript) - if not json_script: - json_script = function_cache.get(self._load_javascript, - function_cache.ONE_DAY, - self._javascript) + json_script = function_cache.run(self._load_javascript, + function_cache.ONE_DAY, + javascript=self._javascript) if json_script: json_script_engine = JsonScriptEngine(json_script) diff --git a/resources/lib/youtube_plugin/youtube/helper/url_resolver.py b/resources/lib/youtube_plugin/youtube/helper/url_resolver.py index 0fc73f676..8d744560d 100644 --- a/resources/lib/youtube_plugin/youtube/helper/url_resolver.py +++ b/resources/lib/youtube_plugin/youtube/helper/url_resolver.py @@ -205,7 +205,7 @@ def resolve(self, url, url_components, method='HEAD'): class UrlResolver(object): def __init__(self, context): self._context = context - self._cache = context.get_function_cache() + self._function_cache = context.get_function_cache() self._resolver_map = { 'common_resolver': CommonResolver(context), 'youtube_resolver': YouTubeResolver(context), @@ -236,7 +236,12 @@ def _resolve(self, url): return resolved_url def resolve(self, url): - resolved_url = self._cache.get(self._resolve, self._cache.ONE_DAY, url) + resolved_url = self._function_cache.run( + self._resolve, + self._function_cache.ONE_DAY, + _refresh=self._context.get_param('refresh'), + url=url + ) if not resolved_url or resolved_url == '/': return url diff --git a/resources/lib/youtube_plugin/youtube/helper/yt_playlist.py b/resources/lib/youtube_plugin/youtube/helper/yt_playlist.py index c3cf87cf0..cbd880c40 100644 --- a/resources/lib/youtube_plugin/youtube/helper/yt_playlist.py +++ b/resources/lib/youtube_plugin/youtube/helper/yt_playlist.py @@ -141,12 +141,13 @@ def _process_select_playlist(provider, context): # Get listitem path asap, relies on listitems focus path = context.get_infolabel('Container.ListItem(0).FileNameAndPath') + params = context.get_params() ui = context.get_ui() keymap_action = False page_token = '' current_page = 0 - video_id = context.get_param('video_id', '') + video_id = params.get('video_id', '') if not video_id: if context.is_plugin_path(path, 'play/'): video_id = find_video_id(path) @@ -160,18 +161,14 @@ def _process_select_playlist(provider, context): client = provider.get_client(context) while True: current_page += 1 - if not page_token: - json_data = function_cache.get(client.get_playlists_of_channel, - function_cache.ONE_MINUTE // 3, - channel_id='mine') - else: - json_data = function_cache.get(client.get_playlists_of_channel, - function_cache.ONE_MINUTE // 3, - channel_id='mine', - page_token=page_token) + json_data = function_cache.run(client.get_playlists_of_channel, + function_cache.ONE_MINUTE // 3, + _refresh=params.get('refresh'), + channel_id='mine', + page_token=page_token) playlists = json_data.get('items', []) - page_token = json_data.get('nextPageToken', False) + page_token = json_data.get('nextPageToken', '') items = [] if current_page == 1: diff --git a/resources/lib/youtube_plugin/youtube/helper/yt_specials.py b/resources/lib/youtube_plugin/youtube/helper/yt_specials.py index 1f5cc53b3..5dd84e491 100644 --- a/resources/lib/youtube_plugin/youtube/helper/yt_specials.py +++ b/resources/lib/youtube_plugin/youtube/helper/yt_specials.py @@ -28,19 +28,22 @@ def _process_related_videos(provider, context): context.set_content(content.VIDEO_CONTENT) function_cache = context.get_function_cache() - video_id = context.get_param('video_id', '') + params = context.get_params() + video_id = params.get('video_id', '') if video_id: - json_data = function_cache.get( + json_data = function_cache.run( provider.get_client(context).get_related_videos, function_cache.ONE_HOUR, + _refresh=params.get('refresh'), video_id=video_id, - page_token=context.get_param('page_token', ''), + page_token=params.get('page_token', ''), ) else: - json_data = function_cache.get( + json_data = function_cache.run( provider.get_client(context).get_related_for_home, function_cache.ONE_HOUR, - page_token=context.get_param('page_token', ''), + _refresh=params.get('refresh'), + page_token=params.get('page_token', ''), ) if not json_data: @@ -85,9 +88,10 @@ def _process_recommendations(provider, context): params = context.get_params() function_cache = context.get_function_cache() - json_data = function_cache.get( + json_data = function_cache.run( provider.get_client(context).get_recommended_for_home, function_cache.ONE_HOUR, + _refresh=params.get('refresh'), visitor=params.get('visitor', ''), page_token=params.get('page_token', ''), click_tracking=params.get('click_tracking', ''), @@ -119,7 +123,7 @@ def _process_browse_channels(provider, context): json_data = client.get_guide_category(guide_id) else: function_cache = context.get_function_cache() - json_data = function_cache.get(client.get_guide_categories, + json_data = function_cache.run(client.get_guide_categories, function_cache.ONE_MONTH) if not json_data: @@ -183,9 +187,10 @@ def _extract_urls(video_id): description = strip_html_from_text(snippet['description']) function_cache = context.get_function_cache() - urls = function_cache.get(extract_urls, - function_cache.ONE_WEEK, - description) + urls = function_cache.run(utils.extract_urls, + function_cache.ONE_DAY, + _refresh=params.get('refresh'), + text=description) progress_dialog.set_total(len(urls)) diff --git a/resources/lib/youtube_plugin/youtube/provider.py b/resources/lib/youtube_plugin/youtube/provider.py index 7c3556e77..c80fa141a 100644 --- a/resources/lib/youtube_plugin/youtube/provider.py +++ b/resources/lib/youtube_plugin/youtube/provider.py @@ -391,6 +391,7 @@ def _on_channel(self, context, re_match): localize = context.localize create_uri = context.create_uri function_cache = context.get_function_cache() + params = context.get_params() ui = context.get_ui() method = re_match.group('method') @@ -421,9 +422,10 @@ def _on_channel(self, context, re_match): if method == 'user' or channel_id == 'mine': context.log_debug('Trying to get channel id for user "%s"' % channel_id) - json_data = function_cache.get(client.get_channel_by_username, + json_data = function_cache.run(client.get_channel_by_username, function_cache.ONE_DAY, - channel_id) + _refresh=params.get('refresh'), + username=channel_id) if not json_data: return False @@ -441,7 +443,6 @@ def _on_channel(self, context, re_match): channel_fanarts = resource_manager.get_fanarts((channel_id, )) - params = context.get_params() page = params.get('page', 1) page_token = params.get('page_token', '') incognito = params.get('incognito') @@ -491,9 +492,10 @@ def _on_channel(self, context, re_match): playlists = resource_manager.get_related_playlists(channel_id) upload_playlist = playlists.get('uploads', '') if upload_playlist: - json_data = function_cache.get(client.get_playlist_items, + json_data = function_cache.run(client.get_playlist_items, function_cache.ONE_MINUTE * 5, - upload_playlist, + _refresh=params.get('refresh'), + playlist_id=upload_playlist, page_token=page_token) if not json_data: return result @@ -787,8 +789,9 @@ def on_search(self, search_text, context, re_match): result.append(live_item) function_cache = context.get_function_cache() - json_data = function_cache.get(self.get_client(context).search, + json_data = function_cache.run(self.get_client(context).search, function_cache.ONE_MINUTE * 10, + _refresh=params.get('refresh'), q=search_text, search_type=search_type, event_type=event_type,