compile search regex

master
Aevann 2023-10-05 19:14:17 +03:00
parent 689be547d7
commit c4d4d864ae
2 changed files with 9 additions and 6 deletions

View File

@ -173,6 +173,9 @@ video_link_regex = re.compile(f"https://(videos\.)?{SITE}\/(videos\/)?[0-9a-zA-Z
asset_image_link_regex = re.compile(f"https:\/\/(i\.)?{SITE}\/assets\/images\/[\w\/]+.webp(\?x=\d+)?", flags=re.A)
search_regex_1 = re.compile(r'[\0():|&*!<>]', flags=re.A)
search_regex_2 = re.compile(r"'", flags=re.A)
search_regex_3 = re.compile(r'\s+', flags=re.A)
###REDDIT

View File

@ -227,11 +227,11 @@ def searchcomments(v):
else: comments = comments.filter(Comment.author_id == author.id)
if 'q' in criteria:
tokens = map(lambda x: re.sub(r'[\0():|&*!<>]', '', x), criteria['q'])
tokens = map(lambda x: search_regex_1.sub('', x), criteria['q'])
tokens = filter(lambda x: len(x) > 0, tokens)
tokens = map(lambda x: re.sub(r"'", "\\'", x), tokens)
tokens = map(lambda x: search_regex_2.sub("\\'", x), tokens)
tokens = map(lambda x: x.strip(), tokens)
tokens = map(lambda x: re.sub(r'\s+', ' <-> ', x), tokens)
tokens = map(lambda x: search_regex_3.sub(' <-> ', x), tokens)
comments = comments.filter(Comment.body_ts.match(
' & '.join(tokens),
postgresql_regconfig='english'))
@ -328,11 +328,11 @@ def searchmessages(v):
else: comments = comments.filter(Comment.author_id == author.id)
if 'q' in criteria:
tokens = map(lambda x: re.sub(r'[\0():|&*!<>]', '', x), criteria['q'])
tokens = map(lambda x: search_regex_1.sub('', x), criteria['q'])
tokens = filter(lambda x: len(x) > 0, tokens)
tokens = map(lambda x: re.sub(r"'", "\\'", x), tokens)
tokens = map(lambda x: search_regex_2.sub("\\'", x), tokens)
tokens = map(lambda x: x.strip(), tokens)
tokens = map(lambda x: re.sub(r'\s+', ' <-> ', x), tokens)
tokens = map(lambda x: search_regex_3.sub(' <-> ', x), tokens)
comments = comments.filter(Comment.body_ts.match(
' & '.join(tokens),
postgresql_regconfig='english'))