dedup shit and make it work in all contexts

master
Aevann 2023-09-09 01:41:17 +03:00
parent ee5748abd1
commit b3d61fb515
3 changed files with 23 additions and 25 deletions

View File

@ -19,13 +19,31 @@ from files.helpers.sorting_and_time import *
from .saves import CommentSaveRelationship
def normalize_urls_runtime(body, v):
if not v: return body
if v.reddit != 'old.reddit.com':
if v and v.reddit != 'old.reddit.com':
body = reddit_to_vreddit_regex.sub(rf'\1https://{v.reddit}/\2/', body)
if v.nitter:
if v and v.nitter:
body = twitter_to_nitter_regex.sub(r'\1https://nitter.net/', body)
if v.imginn:
if v and v.imginn:
body = body.replace('https://instagram.com/', 'https://imginn.com/')
if not v or v.controversial:
captured = []
for i in controversial_regex.finditer(body):
if i.group(0) in captured: continue
captured.append(i.group(0))
url = i.group(0)
p = urlparse(url).query
p = parse_qs(p, keep_blank_values=True)
if 'sort' not in p:
p['sort'] = ['controversial']
url_noquery = url.split('?')[0]
body = body.replace(f'{url}', f'{url_noquery}?{urlencode(p, True)}')
return body
@ -351,21 +369,6 @@ class Comment(Base):
body = censor_slurs(body, v)
body = normalize_urls_runtime(body, v)
if not v or v.controversial:
captured = []
for i in controversial_regex.finditer(body):
if i.group(1) in captured: continue
captured.append(i.group(1))
url = i.group(1)
p = urlparse(url).query
p = parse_qs(p, keep_blank_values=True)
if 'sort' not in p: p['sort'] = ['controversial']
url_noquery = url.split('?')[0]
body = body.replace(f'"{url}"', f'"{url_noquery}?{urlencode(p, True)}"')
body = body.replace(f'>{url}<', f'>{url_noquery}?{urlencode(p, True)}<')
return body

View File

@ -268,11 +268,6 @@ class Post(Base):
url = normalize_urls_runtime(url, v)
if url.startswith("https://old.reddit.com/r/") and '/comments/' in url and "sort=" not in url:
if "?" in url: url += "&context=9"
else: url += "?context=8"
if not v or v.controversial: url += "&sort=controversial"
if url.startswith('/'): return SITE_FULL + url
return url

View File

@ -40,7 +40,7 @@ html_comment_regex = re.compile("<!--.*-->", flags=re.A)
title_regex = re.compile("[^\w ]", flags=re.A)
controversial_regex = re.compile('["> ](https:\/\/old\.reddit\.com/r/\w{2,20}\/comments\/[\w\-.#&/=\?@%+]{5,250})["< ]', flags=re.A)
controversial_regex = re.compile('https:\/\/old\.reddit\.com/r/\w{2,20}\/comments\/[\w\-.#&/=\?@%+]{5,250}', flags=re.A)
spoiler_regex = re.compile('\|\|(.+?)\|\|' + NOT_IN_CODE_OR_LINKS, flags=re.A)
sub_regex = re.compile('(?<![\w/])\/?([hH]\/(\w|-){3,25})' + NOT_IN_CODE_OR_LINKS, flags=re.A)