diff options
author | James Taylor <user234683@users.noreply.github.com> | 2020-09-18 17:32:28 -0700 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2020-09-18 17:32:28 -0700 |
commit | 753f6c5389be82f4a17dafb1698e2f65388d35b6 (patch) | |
tree | 08593c8b283a9d10adf2c1c31e0954f5ac6232f1 /youtube/__init__.py | |
parent | e9989af03a0d6044106030f164f807cee42c1420 (diff) | |
download | yt-local-753f6c5389be82f4a17dafb1698e2f65388d35b6.tar.lz yt-local-753f6c5389be82f4a17dafb1698e2f65388d35b6.tar.xz yt-local-753f6c5389be82f4a17dafb1698e2f65388d35b6.zip |
Jump video to timestamp in description or comments
Diffstat (limited to 'youtube/__init__.py')
-rw-r--r-- | youtube/__init__.py | 17 |
1 files changed, 17 insertions, 0 deletions
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 ( + '<a href="#" onclick="document.querySelector(\'video\').currentTime=' + + str(time_seconds) + + '">' + match.group(0) + + '</a>' + ) + +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 |