From 0dd8571d4bd3a26ea275724577e4cfcb3bded832 Mon Sep 17 00:00:00 2001 From: jr-k Date: Mon, 13 May 2024 13:07:54 +0200 Subject: [PATCH 1/2] direct youtube id is now ok --- src/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils.py b/src/utils.py index b3c682d..bb01f5b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -159,7 +159,10 @@ def get_ip_address() -> Optional[str]: return None -def get_yt_video_id(url: str) -> str: +def get_yt_video_id(url_or_id: str) -> str: + if len(url_or_id) == 14: + return url + if not url.startswith('http'): url = 'http://' + url From 53f641a48d71e0377f989bb8b14f3a308456a8bb Mon Sep 17 00:00:00 2001 From: jr-k Date: Mon, 13 May 2024 14:10:06 +0200 Subject: [PATCH 2/2] change youtube id extract algorithm + variable as string if null fix + add youtube icon and fix icons on slideshow list + fix slide preview location --- src/model/entity/Variable.py | 3 ++ src/utils.py | 55 ++++++++++++++-------- views/slideshow/component/table.jinja.html | 30 +++++++----- 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/model/entity/Variable.py b/src/model/entity/Variable.py index fdfade7..700e49d 100644 --- a/src/model/entity/Variable.py +++ b/src/model/entity/Variable.py @@ -165,6 +165,9 @@ class Variable: return bool(int(self._value)) def as_string(self) -> str: + if self._value is None: + return '' + return str(self._value) def as_int(self) -> int: diff --git a/src/utils.py b/src/utils.py index bb01f5b..104a7f7 100644 --- a/src/utils.py +++ b/src/utils.py @@ -3,7 +3,6 @@ import re import subprocess import platform -from urllib.parse import urlparse, parse_qs from typing import Optional, List, Dict from enum import Enum from cron_descriptor import ExpressionDescriptor @@ -159,24 +158,40 @@ def get_ip_address() -> Optional[str]: return None -def get_yt_video_id(url_or_id: str) -> str: - if len(url_or_id) == 14: +def regex_search(pattern: str, string: str, group: int): + """Shortcut method to search a string for a given pattern. + :param str pattern: + A regular expression pattern. + :param str string: + A target string to search. + :param int group: + Index of group to return. + :rtype: + str or tuple + :returns: + Substring pattern matches. + """ + regex = re.compile(pattern) + results = regex.search(string) + if not results: + return False + + return results.group(group) + + +def get_yt_video_id(url: str) -> str: + if len(url) <= 14: return url - if not url.startswith('http'): - url = 'http://' + url - - query = urlparse(url) - - try: - if 'youtube' in query.hostname: - if query.path == '/watch': - return parse_qs(query.query)['v'][0] - elif query.path.startswith(('/embed/', '/v/')): - return query.path.split('/')[2] - elif 'youtu.be' in query.hostname: - return query.path[1:] - except: - return '' - - return '' + """Extract the ``video_id`` from a YouTube url. + This function supports the following patterns: + - :samp:`https://youtube.com/watch?v={video_id}` + - :samp:`https://youtube.com/embed/{video_id}` + - :samp:`https://youtu.be/{video_id}` + :param str url: + A YouTube url containing a video id. + :rtype: str + :returns: + YouTube video id. + """ + return regex_search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url, group=1) diff --git a/views/slideshow/component/table.jinja.html b/views/slideshow/component/table.jinja.html index be7dcff..42a3876 100644 --- a/views/slideshow/component/table.jinja.html +++ b/views/slideshow/component/table.jinja.html @@ -33,10 +33,12 @@ {% set icon_type = 'globe' %} - {% if slide.type == 'video' %} - {% set icon_type = 'video' %} - {% elif slide.type == 'picture' %} + {% if slide.type == enum_slide_type.VIDEO %} + {% set icon_type = 'film' %} + {% elif slide.type == enum_slide_type.PICTURE %} {% set icon_type = 'image' %} + {% elif slide.type == enum_slide_type.YOUTUBE %} + {% set icon_type = 'video' %} {% endif %} @@ -79,15 +81,21 @@ - {% if not fleet_mode or not slide.has_file() %} - - - - {% elif var_external_url.value|length > 0 %} - - - + + {% set location = slide.location %} + + {% if var_external_url.as_string().strip()|length > 0 and slide.has_file() %} + {% set location = var_external_url.value ~ '/' ~ slide.location %} {% endif %} + + {% if slide.type == enum_slide_type.YOUTUBE %} + {% set location = 'https://www.youtube.com/watch?v=' ~ slide.location %} + {% endif %} + + + + +