mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-02-03 00:31:25 -08:00
more reliable youtube thumbnails
This commit is contained in:
parent
0c646c5cf4
commit
eff0edf817
2 changed files with 14 additions and 1 deletions
|
@ -1154,6 +1154,19 @@ class Post(db.Model):
|
||||||
|
|
||||||
return ''
|
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):
|
def peertube_embed(self):
|
||||||
if self.url:
|
if self.url:
|
||||||
return self.url.replace('watch', 'embed')
|
return self.url.replace('watch', 'embed')
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
{% if 'youtube.com' in post.url -%}
|
{% if 'youtube.com' in post.url -%}
|
||||||
<div class="video-wrapper" data-src="https://www.youtube.com/embed/{{ post.youtube_embed() }}">
|
<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') }}">
|
<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>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
|
|
Loading…
Add table
Reference in a new issue