Skip to content

Commit

Permalink
Overhaul update of fanart and other channel info for listings
Browse files Browse the repository at this point in the history
- Add additional channel info to all items in listings in one pass
  - channel name thumbnail in info dialog
  - channel name in context menu items
  - channel fanart if enabled
- Reduce network requests
- Improve cache usage
- Reduce thread usage
  • Loading branch information
MoojMidge committed Dec 7, 2024
1 parent ab9fef1 commit 65496fe
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 137 deletions.
89 changes: 80 additions & 9 deletions resources/lib/youtube_plugin/youtube/helper/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from __future__ import absolute_import, division, unicode_literals

from .utils import get_thumbnail


class ResourceManager(object):
def __init__(self, provider, context):
Expand Down Expand Up @@ -64,7 +66,7 @@ def get_channels(self, ids, defer_cache=False):
.format(exc=exc, data=data))

ids = updated
if refresh:
if refresh or not ids:
result = {}
else:
result = data_cache.get_items(ids, data_cache.ONE_MONTH)
Expand Down Expand Up @@ -116,38 +118,107 @@ def get_channels(self, ids, defer_cache=False):

return result

def get_fanarts(self, channel_ids, force=False, defer_cache=False):
def get_channel_info(self,
ids,
force=False,
channel_data=None,
defer_cache=False):
if force:
pass
elif self._fanart_type != self._context.get_settings().FANART_CHANNEL:
return {}

result = self.get_channels(channel_ids, defer_cache=defer_cache)
context = self._context
refresh = context.get_param('refresh')
if not refresh and channel_data:
result = channel_data
else:
result = {}

to_check = [id_ for id_ in ids
if id_ not in result
or not result[id_]
or result[id_].get('_partial')]
if to_check:
data_cache = context.get_data_cache()
result.update(data_cache.get_items(to_check, data_cache.ONE_MONTH))
to_update = [id_ for id_ in ids
if id_ not in result
or not result[id_]
or result[id_].get('_partial')]

if result:
context.debug_log and context.log_debug(
'ResourceManager.get_fanarts'
' - Using cached data for channels'
'\n\tChannel IDs: {ids}'
.format(ids=list(result))
)

if to_update:
client = self._provider.get_client(context)
new_data = [client.get_channels(list_of_50)
for list_of_50 in self._list_batch(to_update, n=50)]
if not any(new_data):
new_data = None
else:
new_data = None

if new_data:
context.debug_log and context.log_debug(
'ResourceManager.get_fanarts'
' - Retrieved new data for channels'
'\n\tChannel IDs: {ids}'
.format(ids=to_update)
)
new_data = {
yt_item['id']: yt_item
for batch in new_data
for yt_item in batch.get('items', [])
if yt_item
}
result.update(new_data)
self.cache_data(new_data, defer=defer_cache)

banners = (
'bannerTvMediumImageUrl',
'bannerTvLowImageUrl',
'bannerTvImageUrl',
'bannerExternalUrl',
)
untitled = context.localize('untitled')
thumb_size = context.get_settings().get_thumbnail_size()

# transform
for key, item in result.items():
channel_info = {
'name': None,
'image': None,
'fanart': None,
}
images = item.get('brandingSettings', {}).get('image', {})
for banner in banners:
image = images.get(banner)
if image:
result[key] = image
channel_info['fanart'] = image
break
else:
# set an empty url
result[key] = ''
snippet = item.get('snippet')
if snippet:
localised_info = snippet.get('localized') or {}
channel_info['name'] = (localised_info.get('title')
or snippet.get('title')
or untitled)
channel_info['image'] = get_thumbnail(thumb_size,
snippet.get('thumbnails'))
result[key] = channel_info

return result

def get_playlists(self, ids, defer_cache=False):
context = self._context
ids = tuple(ids)
refresh = context.get_param('refresh')
if refresh:
if refresh or not ids:
result = {}
else:
data_cache = context.get_data_cache()
Expand Down Expand Up @@ -315,7 +386,7 @@ def get_videos(self,
context = self._context
ids = tuple(ids)
refresh = context.get_param('refresh')
if refresh:
if refresh or not ids:
result = {}
else:
data_cache = context.get_data_cache()
Expand Down
10 changes: 5 additions & 5 deletions resources/lib/youtube_plugin/youtube/helper/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def tv_videos_to_items(provider, context, json_data):

item_filter = context.get_settings().item_filter()

utils.update_video_infos(
utils.update_video_items(
provider,
context,
video_id_dict,
channel_items_dict=channel_items_dict,
item_filter=item_filter,
)
utils.update_fanarts(provider, context, channel_items_dict)
utils.update_channel_info(provider, context, channel_items_dict)

if item_filter:
result = utils.filter_videos(video_id_dict.values(), **item_filter)
Expand Down Expand Up @@ -102,11 +102,11 @@ def saved_playlists_to_items(provider, context, json_data):
playlist_id_dict[playlist_id] = playlist_item

channel_items_dict = {}
utils.update_playlist_infos(provider,
utils.update_playlist_items(provider,
context,
playlist_id_dict,
channel_items_dict)
utils.update_fanarts(provider, context, channel_items_dict)
channel_items_dict=channel_items_dict)
utils.update_channel_info(provider, context, channel_items_dict)

# next page
next_page_token = json_data.get('next_page_token')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ def get_video_items(self, provider, context, skip_title=False):
if self._video_items:
return self._video_items

channel_id_dict = {}
utils.update_video_infos(
channel_items_dict = {}
utils.update_video_items(
provider,
context,
self._video_id_dict,
channel_items_dict=channel_id_dict,
channel_items_dict=channel_items_dict,
)
utils.update_fanarts(provider, context, channel_id_dict)
utils.update_channel_info(provider, context, channel_items_dict)

self._video_items = [
video_item
Expand All @@ -233,11 +233,11 @@ def get_playlist_items(self, provider, context, skip_title=False):
if self._playlist_items:
return self._playlist_items

channel_id_dict = {}
utils.update_playlist_infos(provider, context,
channel_items_dict = {}
utils.update_playlist_items(provider, context,
self._playlist_id_dict,
channel_items_dict=channel_id_dict)
utils.update_fanarts(provider, context, channel_id_dict)
channel_items_dict=channel_items_dict)
utils.update_channel_info(provider, context, channel_items_dict)

self._playlist_items = [
playlist_item
Expand All @@ -246,13 +246,10 @@ def get_playlist_items(self, provider, context, skip_title=False):
]
return self._playlist_items

def get_channel_items(self, provider, context, skip_title=False):
def get_channel_items(self, _provider, _context, skip_title=False):
if self._channel_items:
return self._channel_items

channel_id_dict = {}
utils.update_fanarts(provider, context, channel_id_dict)

self._channel_items = [
channel_item
for channel_item in self._channel_id_dict.values()
Expand Down
Loading

0 comments on commit 65496fe

Please sign in to comment.