Store the metadata from channels or playlists

This commit is contained in:
tcely 2025-07-18 05:35:34 -04:00 committed by GitHub
parent 802716047c
commit 106da4616f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -81,7 +81,10 @@ def get_channel_id(url):
else:
return channel_id
def get_channel_image_info(url):
def get_image_info(url):
avatar_url = None
banner_url = None
thumbnail_url = None
opts = get_yt_opts()
opts.update({
'skip_download': True,
@ -94,20 +97,47 @@ def get_channel_image_info(url):
with yt_dlp.YoutubeDL(opts) as y:
try:
response = y.extract_info(url, download=False)
avatar_url = None
banner_url = None
except yt_dlp.utils.DownloadError as e:
raise YouTubeError(f'Failed to extract info for "{url}": {e}') from e
else:
height = 0
for thumbnail in response['thumbnails']:
if thumbnail['id'] == 'avatar_uncropped':
avatar_url = thumbnail['url']
if thumbnail['id'] == 'banner_uncropped':
elif thumbnail['id'] == 'banner_uncropped':
banner_url = thumbnail['url']
if banner_url is not None and avatar_url is not None:
break
return avatar_url, banner_url
except yt_dlp.utils.DownloadError as e:
raise YouTubeError(f'Failed to extract channel info for "{url}": {e}') from e
elif (thumbnail.get('height') or 0) > height:
thumbnail_url = thumbnail['url']
try:
entry_type = response['entries'][0].get('_type')
except IndexError:
# an empty entries list
pass
else:
if 'url' == entry_type:
del response['entries']
elif 'playlist' == entry_type:
for playlist in response['entries']:
del playlist['entries']
from .models import Metadata
t = Metadata.objects.defer('value').filter(
source__isnull=True,
media__isnull=True,
).get_or_create(
key=response['id'],
site=response['extractor_key'],
)
md = t[0]
field_defaults = {
f.attname: f.get_default()
for f in md._meta.fields
if f.has_default()
}
if 'retrieved' in field_defaults:
md.retrieved = field_defaults['retrieved']
md.value = response
md.save()
return avatar_url, banner_url, thumbnail_url
def _subscriber_only(msg='', response=None):