This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.py
200 lines (170 loc) · 6.77 KB
/
parsing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import regex
from globalvars import GlobalVars
import datahandling
def get_user_from_url(url):
match = regex.compile(r"(?:https?:)?//([\w.]+)/u(?:sers)?/(\d+)(/(?:.+/?)?)?").search(url)
if match is None:
return None
try:
site = match.group(1)
user_id = match.group(2)
return user_id, site
except:
return None
def fetch_post_url_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:\(\)-]+: \[.+]\(((?:http:)?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\) by \[?.*\]?\(?(?:.*)\)? on `[\w.]+`(?: \(@.+\))?(?: \[.+\]\(.+\))?$"
match = regex.compile(search_regex).search(content)
if match is None:
return None
try:
url = match.group(1)
return url
except:
return None
def fetch_post_id_and_site_from_url(url):
if url is None:
return None
post_type_regex = r"\/\d+#\d+$"
post_type = ""
search_regex = ""
if regex.compile(post_type_regex).search(url):
post_type = "answer"
search_regex = r"^(?:https?:)?\/\/([\w.]+)/questions/\d+/.+/(\d+)#\d+$"
else:
post_type = "question"
search_regex = r"^(?:https?:)?\/\/([\w.]+)/questions/(\d+)(?:/.*)?$"
found = regex.compile(search_regex).search(url)
if found is not None:
try:
post_id = found.group(2)
post_site = found.group(1)
return (post_id, post_site, post_type)
except:
return None
search_regex = r"^(?:https?:)?\/\/([\w.]+)/(q|a)/(\d+)(?:/\d+)?/?"
found = regex.compile(search_regex).search(url)
if found is None:
return None
try:
post_id = found.group(3)
post_site = found.group(1)
post_type = "question" if found.group(2) == "q" else "answer"
return (post_id, post_site, post_type)
except:
return None
def fetch_post_id_and_site_from_msg_content(content):
url = fetch_post_url_from_msg_content(content)
return fetch_post_id_and_site_from_url(url)
def fetch_owner_url_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:\(\)-]+: \[.+]\((?:(?:http:)?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\) by \[.+\]\((.+)\) on `[\w.]+`(?: \(@.+\))?(?: \[.+\]\(.+\))?$"
match = regex.compile(search_regex).search(content)
if match is None:
return None
try:
owner_url = match.group(1)
return owner_url
except:
return None
def fetch_title_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:\(\)-]+: \[(.+)]\((?:(?:http:)?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\) by \[?.*\]?\(?.*\)? on `[\w.]+`(?: \(@.+\))?(?: \[.+\]\(.+\))?$"
match = regex.compile(search_regex).search(content)
if match is None:
return None
try:
title = match.group(1)
return title
except:
return None
def fetch_user_from_allspam_report(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\) \] All of this user's posts are spam: \[user \d+ on [\w\.]+\]\((//[\w\.]+/users/\d+\D*)\)(?: \[.+\]\(.+\))?$"
match = regex.compile(search_regex).search(content)
if match is None:
return None
try:
user_link = match.group(1)
return get_user_from_url(user_link)
except:
return None
def edited_message_after_postgone_command(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:\(\)-]+: (\[.+]\((?:(?:http:)?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\)) by \[?.*\]?\(?.*\)? on `[\w.]+`(?: \(@.+\))?(?: \[.+\]\(.+\))?$"
match = regex.compile(search_regex).search(content)
if match is None:
return None
try:
link = match.group(1)
return content.replace(link, "*(gone)*")
except:
return None
def unescape_title(title_escaped):
return GlobalVars.parser.unescape(title_escaped).strip()
def escape_special_chars_in_title(title_unescaped):
return regex.sub(r"([_*\\`\[\]])", r"\\\1", title_unescaped)
def get_user_from_list_command(cmd): # for example, !!/addblu is a list command
cmd_merged_spaces = regex.sub("\\s+", " ", cmd)
cmd_parts = cmd_merged_spaces.split(" ")
uid = -1
site = ""
if len(cmd_parts) == 2:
uid_site = get_user_from_url(cmd_parts[1])
if uid_site is not None:
uid = uid_site[0]
site = uid_site[1]
elif len(cmd_parts) == 3:
uid = cmd_parts[1]
site = cmd_parts[2]
digit_re = regex.compile("^[0-9]+$")
site_re = regex.compile(r"^(\w+\.stackexchange\.com|\w+\.(com|net))$")
if not digit_re.match(uid):
uid = -1
site = ""
elif not site_re.match(site):
exists, name = datahandling.check_site_and_get_full_name(site)
if exists:
return uid, name
else:
return -2, name
return uid, site
def url_to_shortlink(url):
id_and_site = fetch_post_id_and_site_from_url(url)
if id_and_site is None:
return url
if id_and_site[2] == "question":
return "http://{}/questions/{}".format(id_and_site[1], id_and_site[0])
# We're using "/questions" and not "/q" here because when the URL
# is made protocol-relative, /q would redirect to http even if the
# shortlink is https. Same for /a. But there we still use /a because
# there is no /answers or something like that.
else:
return "http://{}/a/{}".format(id_and_site[1], id_and_site[0])
def user_url_to_shortlink(url):
user_id_and_site = get_user_from_url(url)
if user_id_and_site is None:
return url
return "http://{}/u/{}".format(user_id_and_site[1], user_id_and_site[0])
def to_protocol_relative(url):
if url.startswith("http://"):
return url[5:]
elif url.startswith("https://"):
return url[6:]
else:
return url
def preprocess_shortcut_command(cmd):
cmd = regex.sub(r"(\d)\s+", r"\1", cmd)
parts = cmd.split(" ")
new_cmd = ["sd"]
for i in xrange(1, len(parts)):
current = parts[i]
if current == "":
continue
if not current[0].isdigit():
new_cmd.append(current)
else:
match = regex.search(r"^\d+", current)
t = int(match.group())
if t > 100:
# If someone made an unreasonable request, limit the # of commands (so we don't
# have any overflows) but do one more than the max messages we keep so it errors out
t = 101
for j in xrange(0, t):
new_cmd.append(current[match.end():])
return " ".join(new_cmd)