more reliable youtube thumbnails

This commit is contained in:
rimu 2024-10-06 07:03:58 +13:00
parent 0c646c5cf4
commit eff0edf817
2 changed files with 14 additions and 1 deletions

View file

@ -1154,6 +1154,19 @@ class Post(db.Model):
return ''
def youtube_video_id(self) -> str:
if self.url:
parsed_url = urlparse(self.url)
query_params = parse_qs(parsed_url.query)
if 'v' in query_params:
return query_params['v'][0]
if '/shorts/' in parsed_url.path:
video_id = parsed_url.path.split('/shorts/')[1].split('/')[0]
return f'{video_id}'
return ''
def peertube_embed(self):
if self.url:
return self.url.replace('watch', 'embed')

View file

@ -39,7 +39,7 @@
{% if 'youtube.com' in post.url -%}
<div class="video-wrapper" data-src="https://www.youtube.com/embed/{{ post.youtube_embed() }}">
<a href="{{ url_for('activitypub.post_ap', post_id=post.id, sort='new' if sort == 'active' else None, autoplay='true') }}" rel="nofollow ugc" aria-label="{{ _('Read article') }}">
<img src="https://img.youtube.com/vi/{{ post.youtube_embed(rel=False).replace('v=', '').replace('?', '') }}/hqdefault.jpg" alt="Video Thumbnail" width="512" height="288" loading="lazy">
<img src="https://img.youtube.com/vi/{{ post.youtube_video_id() }}/hqdefault.jpg" alt="Video Thumbnail" width="512" height="288" loading="lazy">
</a>
</div>
{% endif -%}