forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.py
205 lines (175 loc) · 6.88 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
201
202
203
204
205
# coding=utf-8
# noinspection PyCompatibility
import regex
from globalvars import GlobalVars
import datahandling
# noinspection PyBroadException,PyMissingTypeHints
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
# noinspection PyBroadException
def get_api_sitename_from_url(url):
match = regex.compile(r"(?:https?:)?(?://)?([\w.]+)/?").search(url)
if match is None:
return None
try:
if match.group(1) == 'mathoverflow.net':
return 'mathoverflow.net'
else:
return match.group(1).split('.')[0]
except:
return None
# noinspection PyBroadException,PyMissingTypeHints
def fetch_post_url_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:+\(\)-]+: \[.+]\(((?:http:)" \
r"?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)(?:\?smokeypost=true)?\)" \
r" 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
# noinspection PyBroadException,PyUnusedLocal,PyRedundantParentheses,PyMissingTypeHints
def fetch_post_id_and_site_from_url(url):
if url is None:
return None
trimmed_url = url.replace("‌​", "")
post_type_regex = r"\/\d+(‌​\d+)?#\d+$"
post_type = ""
search_regex = ""
if regex.compile(post_type_regex).search(trimmed_url):
post_type = "answer"
search_regex = r"^(?:https?:)?\/\/([\w.]+)\/questions\/\d+\/.+\/(\d+(‌​\d+)?)#\d+$"
else:
post_type = "question"
search_regex = r"^(?:https?:)?\/\/([\w.]+)/questions/(\d+)(?:/.*)?$"
found = regex.compile(search_regex).search(trimmed_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(trimmed_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
# noinspection PyMissingTypeHints
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)
# noinspection PyBroadException,PyMissingTypeHints
def fetch_owner_url_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:+\(\)-]+: \[.+]\((?:(?:http:)" \
r"?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\) by \[.+\]\((.+)\)" \
r" 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
# noinspection PyBroadException,PyMissingTypeHints
def fetch_title_from_msg_content(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:+\(\)-]+: \[(.+)]\((?:(?:http:)" \
r"?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\) by \[?.*\]?\(?.*\)?" \
r" 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
# noinspection PyBroadException,PyMissingTypeHints
def edited_message_after_postgone_command(content):
search_regex = r"^\[ \[SmokeDetector\]\([^)]*\)(?: \| \[.+\]\(.+\))? \] [\w\s,:+\(\)-]+: (\[.+]\((?:(?:http:)" \
r"?\/\/[\w.]+\/questions\/\d+(?:\/.*)?|(?:http:)?\/\/[\w.]+\/[qa]\/\d+/?)\)) by \[?.*\]?\(?.*\)?" \
r" 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
# noinspection PyMissingTypeHints
def unescape_title(title_escaped):
return GlobalVars.parser.unescape(title_escaped).strip()
# noinspection PyMissingTypeHints
def escape_special_chars_in_title(title_unescaped):
return regex.sub(r"([_*\\`\[\]])", r"\\\1", title_unescaped)
# noinspection PyMissingTypeHints
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) == 1:
uid_site = get_user_from_url(cmd_parts[0])
if uid_site is not None:
uid, site = uid_site
elif len(cmd_parts) == 2:
uid = cmd_parts[0]
site = cmd_parts[1]
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
# noinspection PyMissingTypeHints
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])
# noinspection PyMissingTypeHints
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://{}/users/{}".format(user_id_and_site[1], user_id_and_site[0])
# noinspection PyMissingTypeHints
def to_protocol_relative(url):
if url.startswith("http://"):
return url[5:]
elif url.startswith("https://"):
return url[6:]
else:
return url