From 753f6c5389be82f4a17dafb1698e2f65388d35b6 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Fri, 18 Sep 2020 17:32:28 -0700 Subject: Jump video to timestamp in description or comments --- youtube/__init__.py | 17 +++++++++++++++++ youtube/templates/comments.html | 10 +++++++--- youtube/templates/watch.html | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) (limited to 'youtube') diff --git a/youtube/__init__.py b/youtube/__init__.py index 8675c4b..6c2ec48 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -2,6 +2,7 @@ from youtube import util import flask import settings import traceback +import re from sys import exc_info yt_app = flask.Flask(__name__) yt_app.url_map.strict_slashes = False @@ -34,6 +35,22 @@ def commatize(num): num = int(num) return '{:,}'.format(num) +def timestamp_replacement(match): + time_seconds = 0 + for part in match.group(0).split(':'): + time_seconds = 60*time_seconds + int(part) + return ( + '' + match.group(0) + + '' + ) + +TIMESTAMP_RE = re.compile(r'\b(\d?\d:)?\d?\d:\d\d\b') +@yt_app.template_filter('timestamps') +def timestamps(text): + return TIMESTAMP_RE.sub(timestamp_replacement, text) + @yt_app.errorhandler(500) def error_page(e): if (exc_info()[0] == util.FetchError diff --git a/youtube/templates/comments.html b/youtube/templates/comments.html index 396852a..f2cdf65 100644 --- a/youtube/templates/comments.html +++ b/youtube/templates/comments.html @@ -1,6 +1,6 @@ {% import "common_elements.html" as common_elements %} -{% macro render_comment(comment, include_avatar) %} +{% macro render_comment(comment, include_avatar, timestamp_links=False) %}
@@ -14,7 +14,11 @@ - {{ common_elements.text_runs(comment['text']) }} + {% if timestamp_links %} + {{ common_elements.text_runs(comment['text'])|timestamps|safe }} + {% else %} + {{ common_elements.text_runs(comment['text']) }} + {% endif %}
@@ -36,7 +40,7 @@
{% for comment in comments_info['comments'] %} - {{ render_comment(comment, comments_info['include_avatars']) }} + {{ render_comment(comment, comments_info['include_avatars'], True) }} {% endfor %}
{% if 'more_comments_url' is in comments_info %} diff --git a/youtube/templates/watch.html b/youtube/templates/watch.html index 74b9887..5ecf7ae 100644 --- a/youtube/templates/watch.html +++ b/youtube/templates/watch.html @@ -401,7 +401,7 @@ Reload without invidious (for usage of new identity button). - {{ common_elements.text_runs(description)|urlize }} + {{ common_elements.text_runs(description)|escape|urlize|timestamps|safe }}
{% if music_list.__len__() != 0 %}
-- cgit v1.2.3