From eff0edf817d908384f22315a1411cc24affd3e90 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 6 Oct 2024 07:03:58 +1300 Subject: [PATCH] more reliable youtube thumbnails --- app/models.py | 13 +++++++++++++ app/templates/post/post_teaser/_video.html | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 86befeec..032701e9 100644 --- a/app/models.py +++ b/app/models.py @@ -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') diff --git a/app/templates/post/post_teaser/_video.html b/app/templates/post/post_teaser/_video.html index a08f4326..f93b3d84 100644 --- a/app/templates/post/post_teaser/_video.html +++ b/app/templates/post/post_teaser/_video.html @@ -39,7 +39,7 @@ {% if 'youtube.com' in post.url -%}
{% endif -%}