fix youtube timestamps

master
Aevann 2023-01-23 04:06:56 +02:00
parent 58864684b7
commit a560958c3a
3 changed files with 28 additions and 28 deletions

View File

@ -85,7 +85,7 @@ imgur_regex = re.compile(f'(https:\/\/i\.imgur\.com\/[a-z0-9]+)\.({image_regex_e
giphy_regex = re.compile('(https:\/\/media\.giphy\.com\/media\/[a-z0-9]+\/giphy)\.gif', flags=re.I|re.A) giphy_regex = re.compile('(https:\/\/media\.giphy\.com\/media\/[a-z0-9]+\/giphy)\.gif', flags=re.I|re.A)
youtube_regex = re.compile('(<p>[^<]*)(https:\/\/youtube\.com\/watch\?v\=([a-z0-9-_]{5,20})[\w\-.#&/=\?@%+]*)', flags=re.I|re.A) youtube_regex = re.compile('(<p>[^<]*)(https:\/\/youtube\.com\/watch\?[\w\-.#&/=?@%+]{7,})', flags=re.I|re.A)
yt_id_regex = re.compile('[a-z0-9-_]{5,20}', flags=re.I|re.A) yt_id_regex = re.compile('[a-z0-9-_]{5,20}', flags=re.I|re.A)
link_fix_regex = re.compile("(\[.*?\]\()(?!http|\/)(.*?\))(?!([^<]*<\/(code|pre|a)>|[^`]*`))", flags=re.A) link_fix_regex = re.compile("(\[.*?\]\()(?!http|\/)(.*?\))(?!([^<]*<\/(code|pre|a)>|[^`]*`))", flags=re.A)

View File

@ -237,6 +237,27 @@ chud_images = listdir("files/assets/images/chud")
chud_images = [f'![](/i/chud/{f})' for f in chud_images] chud_images = [f'![](/i/chud/{f})' for f in chud_images]
chud_images.extend([':#trumpjaktalking:', ':#reposthorse:']) chud_images.extend([':#trumpjaktalking:', ':#reposthorse:'])
def handle_youtube(url):
html = None
params = parse_qs(urlparse(url).query, keep_blank_values=True)
id = params.get('v')[0]
t = None
split = id.split('?t=')
if len(split) == 2:
id = split[0]
t = split[1]
if yt_id_regex.fullmatch(id):
if not t:
t = params.get('t', params.get('start', [0]))[0]
if isinstance(t, str): t = t.replace('s','')
html = f'<lite-youtube videoid="{id}" params="autoplay=1&modestbranding=1'
if t:
html += f'&start={int(t)}'
html += '"></lite-youtube>'
return html
@with_sigalrm_timeout(10) @with_sigalrm_timeout(10)
def sanitize(sanitized, golden=True, limit_pings=0, showmore=True, count_marseys=False, torture=False, sidebar=False, snappy=False): def sanitize(sanitized, golden=True, limit_pings=0, showmore=True, count_marseys=False, torture=False, sidebar=False, snappy=False):
sanitized = sanitized.strip() sanitized = sanitized.strip()
@ -356,17 +377,9 @@ def sanitize(sanitized, golden=True, limit_pings=0, showmore=True, count_marseys
if i.group(0) in captured: continue if i.group(0) in captured: continue
captured.append(i.group(0)) captured.append(i.group(0))
params = parse_qs(urlparse(i.group(2)).query, keep_blank_values=True) html = handle_youtube(i.group(0))
t = params.get('t', params.get('start', [0]))[0] if html:
if isinstance(t, str): t = t.replace('s','') sanitized = sanitized.replace(i.group(0), html)
htmlsource = f'{i.group(1)}<lite-youtube videoid="{i.group(3)}" params="autoplay=1&modestbranding=1'
if t:
try: htmlsource += f'&start={int(t)}'
except: pass
htmlsource += '"></lite-youtube>'
sanitized = sanitized.replace(i.group(0), htmlsource)
sanitized = video_sub_regex.sub(r'\1<p class="resizable"><video controls preload="none" src="\2"></video></p>', sanitized) sanitized = video_sub_regex.sub(r'\1<p class="resizable"><video controls preload="none" src="\2"></video></p>', sanitized)
sanitized = audio_sub_regex.sub(r'\1<audio controls preload="none" src="\2"></audio>', sanitized) sanitized = audio_sub_regex.sub(r'\1<audio controls preload="none" src="\2"></audio>', sanitized)
@ -472,12 +485,12 @@ def normalize_url(url):
url = url.replace("https://youtu.be/", "https://youtube.com/watch?v=") \ url = url.replace("https://youtu.be/", "https://youtube.com/watch?v=") \
.replace("https://music.youtube.com/watch?v=", "https://youtube.com/watch?v=") \ .replace("https://music.youtube.com/watch?v=", "https://youtube.com/watch?v=") \
.replace("https://www.youtube.com", "https://youtube.com") \ .replace("https://www.youtube.com", "https://youtube.com") \
.replace("https://m.youtube.com", "https://youtube.com") \
.replace("https://youtube.com/shorts/", "https://youtube.com/watch?v=") \ .replace("https://youtube.com/shorts/", "https://youtube.com/watch?v=") \
.replace("https://youtube.com/v/", "https://youtube.com/watch?v=") \ .replace("https://youtube.com/v/", "https://youtube.com/watch?v=") \
.replace("https://mobile.twitter.com", "https://twitter.com") \ .replace("https://mobile.twitter.com", "https://twitter.com") \
.replace("https://m.facebook.com", "https://facebook.com") \ .replace("https://m.facebook.com", "https://facebook.com") \
.replace("https://m.wikipedia.org", "https://wikipedia.org") \ .replace("https://m.wikipedia.org", "https://wikipedia.org") \
.replace("https://m.youtube.com", "https://youtube.com") \
.replace("https://www.twitter.com", "https://twitter.com") \ .replace("https://www.twitter.com", "https://twitter.com") \
.replace("https://www.instagram.com", "https://instagram.com") \ .replace("https://www.instagram.com", "https://instagram.com") \
.replace("https://www.tiktok.com", "https://tiktok.com") \ .replace("https://www.tiktok.com", "https://tiktok.com") \

View File

@ -630,21 +630,8 @@ def submit_post(v:User, sub=None):
embed = requests.get("https://publish.twitter.com/oembed", params={"url":url, "omit_script":"t"}, timeout=5).json()["html"] embed = requests.get("https://publish.twitter.com/oembed", params={"url":url, "omit_script":"t"}, timeout=5).json()["html"]
embed = embed.replace('<a href', '<a rel="nofollow noopener" href') embed = embed.replace('<a href', '<a rel="nofollow noopener" href')
except: pass except: pass
elif url.startswith('https://youtube.com/watch?v='): elif url.startswith('https://youtube.com/watch?'):
url = unquote(url).replace('?t', '&t') embed = handle_youtube(url)
yt_id = url.split('https://youtube.com/watch?v=')[1].split('&')[0].split('%')[0]
if yt_id_regex.fullmatch(yt_id):
params = parse_qs(urlparse(url).query, keep_blank_values=True)
t = params.get('t', params.get('start', [0]))[0]
if isinstance(t, str): t = t.replace('s','')
embed = f'<lite-youtube videoid="{yt_id}" params="autoplay=1&modestbranding=1'
if t:
try: embed += f'&start={int(t)}'
except: pass
embed += '"></lite-youtube>'
elif SITE in domain and "/post/" in url and "context" not in url and url.count('/') < 6: elif SITE in domain and "/post/" in url and "context" not in url and url.count('/') < 6:
id = url.split("/post/")[1] id = url.split("/post/")[1]
if "/" in id: id = id.split("/")[0] if "/" in id: id = id.split("/")[0]