[invidious] Improved heuristics to get the best image.
This commit is contained in:
parent
6e9cbd0593
commit
71172a3374
1 changed files with 38 additions and 25 deletions
|
@ -1,4 +1,5 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Collection, Optional
|
||||||
|
|
||||||
from marshmallow import EXCLUDE, fields, pre_dump
|
from marshmallow import EXCLUDE, fields, pre_dump
|
||||||
from marshmallow.schema import Schema
|
from marshmallow.schema import Schema
|
||||||
|
@ -108,14 +109,25 @@ class InvidiousVideoSchema(Schema):
|
||||||
|
|
||||||
@pre_dump
|
@pre_dump
|
||||||
def fill_image(self, data: dict, **_):
|
def fill_image(self, data: dict, **_):
|
||||||
thumbnails = data.get('videoThumbnails')
|
images = {img['quality']: img for img in data.get('videoThumbnails', [])}
|
||||||
if not thumbnails:
|
img = None
|
||||||
return data
|
|
||||||
|
|
||||||
data['image'] = next(
|
if images.get('high'):
|
||||||
(t['url'] for t in thumbnails),
|
img = images['high']
|
||||||
None,
|
elif images.get('sddefault'):
|
||||||
)
|
img = images['sddefault']
|
||||||
|
elif images.get('medium'):
|
||||||
|
img = images['medium']
|
||||||
|
elif images.get('maxresdefault'):
|
||||||
|
img = images['maxresdefault']
|
||||||
|
elif images.get('default'):
|
||||||
|
img = images['default']
|
||||||
|
else:
|
||||||
|
# Fallback to the first image
|
||||||
|
img = next(iter(images.values()), None)
|
||||||
|
|
||||||
|
if img:
|
||||||
|
data['image'] = img['url']
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -332,27 +344,28 @@ class InvidiousChannelSchema(Schema):
|
||||||
|
|
||||||
@pre_dump
|
@pre_dump
|
||||||
def fill_images(self, data: dict, **_):
|
def fill_images(self, data: dict, **_):
|
||||||
images = data.get('authorThumbnails', [])
|
def get_image(data: Optional[Collection[dict]]) -> Optional[str]:
|
||||||
banners = data.get('authorBanners', [])
|
if not data:
|
||||||
data['banner'] = next(
|
return None
|
||||||
iter(
|
|
||||||
sorted(
|
|
||||||
banners,
|
|
||||||
key=lambda i: i.get('width', 0),
|
|
||||||
reverse=True,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
|
|
||||||
if data['banner']:
|
img = next(
|
||||||
data['banner'] = data['banner']['url']
|
iter(
|
||||||
|
sorted(
|
||||||
|
data,
|
||||||
|
key=lambda i: i.get('width', 0),
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
data['image'] = next(
|
if img:
|
||||||
(t['url'] for t in images),
|
return img['url']
|
||||||
None,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
data['banner'] = get_image(data.get('authorBanners', []))
|
||||||
|
data['image'] = get_image(data.get('authorThumbnails', []))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@pre_dump
|
@pre_dump
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue