diff options
author | James Taylor <user234683@users.noreply.github.com> | 2021-08-08 20:29:42 -0700 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2021-08-09 12:27:49 -0500 |
commit | 6c6c469fbd4277d80b3309b796466c32ec19a0d0 (patch) | |
tree | c65314b3680b62c8e4c2d482b027b9900b3d042b | |
parent | 2039972ab32ac7d7ceb6be811a7d7d9019377b2c (diff) | |
download | yt-local-6c6c469fbd4277d80b3309b796466c32ec19a0d0.tar.lz yt-local-6c6c469fbd4277d80b3309b796466c32ec19a0d0.tar.xz yt-local-6c6c469fbd4277d80b3309b796466c32ec19a0d0.zip |
Prefix youtube URLs in video descriptions and channel about page
Closes #75
Signed-off-by: Jesús <heckyel@hyperbola.info>
-rw-r--r-- | youtube/__init__.py | 15 | ||||
-rw-r--r-- | youtube/channel.py | 4 | ||||
-rw-r--r-- | youtube/util.py | 7 |
3 files changed, 26 insertions, 0 deletions
diff --git a/youtube/__init__.py b/youtube/__init__.py index 08f4f6f..3604b21 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -2,6 +2,7 @@ from youtube import util from .get_app_version import app_version import flask from flask import request +import jinja2 import settings import traceback import re @@ -135,3 +136,17 @@ def get_css(): ), mimetype='text/css', ) + + +# This is okay because the flask urlize function puts the href as the first +# property +YOUTUBE_LINK_RE = re.compile(r'<a href="(' + util.YOUTUBE_URL_RE_STR + ')"') +old_urlize = jinja2.filters.urlize + + +def prefix_urlize(*args, **kwargs): + result = old_urlize(*args, **kwargs) + return YOUTUBE_LINK_RE.sub(r'<a href="/\1"', result) + + +jinja2.filters.urlize = prefix_urlize diff --git a/youtube/channel.py b/youtube/channel.py index 69092d3..4393076 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -231,6 +231,10 @@ def post_process_channel_info(info): for item in info['items']: util.prefix_urls(item) util.add_extra_html_info(item) + if info['current_tab'] == 'about': + for i, (text, url) in enumerate(info['links']): + if util.YOUTUBE_URL_RE.fullmatch(url): + info['links'][i] = (text, util.prefix_url(url)) def get_channel_first_page(base_url=None, channel_id=None): diff --git a/youtube/util.py b/youtube/util.py index 462d371..4070f12 100644 --- a/youtube/util.py +++ b/youtube/util.py @@ -515,6 +515,13 @@ def update_query_string(query_string, items): return urllib.parse.urlencode(parameters, doseq=True) +YOUTUBE_DOMAINS = ('youtube.com', 'youtu.be', 'youtube-nocookie.com') +YOUTUBE_URL_RE_STR = r'https?://(?:[a-zA-Z0-9_-]*\.)?(?:' +YOUTUBE_URL_RE_STR += r'|'.join(map(re.escape, YOUTUBE_DOMAINS)) +YOUTUBE_URL_RE_STR += r')(?:/[^"]*)?' +YOUTUBE_URL_RE = re.compile(YOUTUBE_URL_RE_STR) + + def prefix_url(url): if url is None: return None |