Add video_thumbnail_url to API

This commit is contained in:
rubenwardy 2024-06-07 06:32:22 +01:00
parent 5bd97598a8
commit 1c85e12f9e
2 changed files with 18 additions and 13 deletions

@ -392,18 +392,6 @@ class Package(db.Model):
def donate_url_actual(self): def donate_url_actual(self):
return self.donate_url or self.author.donate_url return self.donate_url or self.author.donate_url
@property
def video_thumbnail_url(self):
from app.utils.url import get_youtube_id
if self.video_url is None:
return None
id_ = get_youtube_id(self.video_url)
if id_:
return url_for("thumbnails.youtube", id_=id_)
return None
enable_game_support_detection = db.Column(db.Boolean, nullable=False, default=True) enable_game_support_detection = db.Column(db.Boolean, nullable=False, default=True)
@ -623,6 +611,7 @@ class Package(db.Model):
"issue_tracker": self.issueTracker, "issue_tracker": self.issueTracker,
"forums": self.forums, "forums": self.forums,
"video_url": self.video_url, "video_url": self.video_url,
"video_thumbnail_url": self.get_video_thumbnail_url(True),
"donate_url": self.donate_url_actual, "donate_url": self.donate_url_actual,
"translation_url": self.translation_url, "translation_url": self.translation_url,
@ -680,6 +669,22 @@ class Package(db.Model):
return "[![ContentDB]({})]({})" \ return "[![ContentDB]({})]({})" \
.format(self.get_shield_url(type), self.get_url("packages.view", True)) .format(self.get_shield_url(type), self.get_url("packages.view", True))
def get_video_thumbnail_url(self, absolute: bool = False):
from app.utils.url import get_youtube_id
if self.video_url is None:
return None
id_ = get_youtube_id(self.video_url)
if id_ is None:
return None
if absolute:
from app.utils import abs_url_for
return abs_url_for("thumbnails.youtube", id_=id_)
else:
return url_for("thumbnails.youtube", id_=id_)
def get_set_state_url(self, state): def get_set_state_url(self, state):
if type(state) == str: if type(state) == str:
state = PackageState[state] state = PackageState[state]

@ -292,7 +292,7 @@
{% if package.video_url %} {% if package.video_url %}
<li> <li>
<a href="{{ package.video_url }}" class="video-embed"> <a href="{{ package.video_url }}" class="video-embed">
{% set thumbnail_url = package.video_thumbnail_url %} {% set thumbnail_url = package.get_video_thumbnail_url() %}
{% if thumbnail_url %} {% if thumbnail_url %}
<img src="{{ thumbnail_url }}" alt="{{ _('Thumbnail for video') }}" /> <img src="{{ thumbnail_url }}" alt="{{ _('Thumbnail for video') }}" />
{% endif %} {% endif %}