diff --git a/files/classes/comment.py b/files/classes/comment.py index 3ea2fe799..37527e468 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -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 diff --git a/files/classes/post.py b/files/classes/post.py index 08a8493fb..7b64e4086 100644 --- a/files/classes/post.py +++ b/files/classes/post.py @@ -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 diff --git a/files/helpers/regex.py b/files/helpers/regex.py index 65e8f5a95..9bffdc999 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -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('(?