Prefix youtube URLs in video descriptions and channel about page
Closes #75 Signed-off-by: Jesús <heckyel@hyperbola.info>
This commit is contained in:
parent
2039972ab3
commit
6c6c469fbd
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user