forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodyfetcher.py
297 lines (268 loc) · 13.6 KB
/
bodyfetcher.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from spamhandling import handle_spam, check_if_spam
from datahandling import add_or_update_api_data, clear_api_data, store_bodyfetcher_queue
from globalvars import GlobalVars
from operator import itemgetter
from datetime import datetime
import json
import time
import threading
import requests
import regex
class BodyFetcher:
queue = {}
special_cases = {
"math.stackexchange.com": 15,
"pt.stackoverflow.com": 10,
"ru.stackoverflow.com": 10,
"serverfault.com": 10,
"blender.stackexchange.com": 5,
"codegolf.stackexchange.com": 5,
"codereview.stackexchange.com": 5,
"es.stackoverflow.com": 5,
"physics.stackexchange.com": 5,
"stackoverflow.com": 5,
"stats.stackexchange.com": 5,
"tex.stackexchange.com": 5,
"magento.stackexchange.com": 3,
"gis.stackexchange.com": 3,
"3dprinting.stackexchange.com": 1,
"academia.stackexchange.com": 1,
"alcohol.stackexchange.com": 1,
"civicrm.stackexchange.com": 1,
"drupal.stackexchange.com": 1,
"engineering.stackexchange.com": 1,
"expatriates.stackexchange.com": 1,
"genealogy.stackexchange.com": 1,
"ham.stackexchange.com": 1,
"health.stackexchange.com": 1,
"history.stackexchange.com": 1,
"meta.stackexchange.com": 1,
"money.stackexchange.com": 1,
"outdoors.stackexchange.com": 1,
"parenting.stackexchange.com": 1,
"patents.stackexchange.com": 1,
"pets.stackexchange.com": 1,
"startups.stackexchange.com": 1,
"travel.stackexchange.com": 1,
"webapps.stackexchange.com": 1,
"woodworking.stackexchange.com": 1,
"writers.stackexchange.com": 1
}
time_sensitive = ["askubuntu.com", "superuser.com", "security.stackexchange.com", "movies.stackexchange.com",
"mathoverflow.net", "gaming.stackexchange.com", "webmasters.stackexchange.com",
"arduino.stackexchange.com", "workplace.stackexchange.com"]
threshold = 2
last_activity_date = 0
api_data_lock = threading.Lock()
queue_modify_lock = threading.Lock()
def add_to_queue(self, post, should_check_site=False):
mse_sandbox_id = 3122
d = json.loads(json.loads(post)["data"])
site_base = d["siteBaseHostAddress"]
post_id = d["id"]
if post_id == mse_sandbox_id and site_base == "meta.stackexchange.com":
return # don't check meta sandbox, it's full of weird posts
self.queue_modify_lock.acquire()
if site_base in self.queue:
self.queue[site_base].append(post_id)
else:
self.queue[site_base] = [post_id]
self.queue_modify_lock.release()
if should_check_site:
self.make_api_call_for_site(site_base)
else:
self.check_queue()
return
def check_queue(self):
for site, values in self.queue.iteritems():
if site in self.special_cases:
if len(values) >= self.special_cases[site]:
print "site {0} met special case quota, fetching...".format(site)
self.make_api_call_for_site(site)
return
if site in self.time_sensitive:
if len(values) >= 1 and datetime.utcnow().hour in range(4, 12):
print "site {0} has activity during peak spam time, fetching...".format(site)
self.make_api_call_for_site(site)
return
# if we don't have any sites with their queue filled, take the first one without a special case
for site, values in self.queue.iteritems():
if site not in self.special_cases and len(values) >= self.threshold:
self.make_api_call_for_site(site)
return
# We're not making an API request, so explicitly store the queue
self.queue_modify_lock.acquire()
store_bodyfetcher_queue()
self.queue_modify_lock.release()
def print_queue(self):
return '\n'.join("{0}: {1}".format(key, str(len(values))) for (key, values) in self.queue.iteritems())
def make_api_call_for_site(self, site):
self.queue_modify_lock.acquire()
if site not in self.queue:
GlobalVars.charcoal_hq.send_message("Attempted API call to {} but there are no posts to fetch.".format(site))
return
posts = self.queue.pop(site)
store_bodyfetcher_queue()
self.queue_modify_lock.release()
question_modifier = ""
pagesize_modifier = ""
if site == "stackoverflow.com":
# Not all SO questions are shown in the realtime feed. We now
# fetch all recently modified SO questions to work around that.
if self.last_activity_date != 0:
pagesize = "50"
else:
pagesize = "25"
pagesize_modifier = "&pagesize={pagesize}&min={time_length}".format(pagesize=pagesize, time_length=str(self.last_activity_date))
else:
question_modifier = "/{0}".format(";".join(str(post) for post in posts))
url = "http://api.stackexchange.com/2.2/questions{q_modifier}?site={site}&filter=!)E0g*ODaEZ(SgULQhYvCYbu09*ss(bKFdnTrGmGUxnqPptuHP&key=IAkbitmze4B8KpacUfLqkw(({optional_min_query_param}".format(q_modifier=question_modifier, site=site, optional_min_query_param=pagesize_modifier)
# wait to make sure API has/updates post data
time.sleep(3)
# Respect backoff, if we were given one
if GlobalVars.api_backoff_time > time.time():
time.sleep(GlobalVars.api_backoff_time - time.time() + 2)
try:
time_request_made = datetime.strftime(datetime.now(), '%H:%M:%S')
response = requests.get(url, timeout=20).json()
except requests.exceptions.Timeout:
return # could add some retrying logic here, but eh.
self.api_data_lock.acquire()
add_or_update_api_data(site)
self.api_data_lock.release()
message_hq = ""
if "quota_remaining" in response:
if response["quota_remaining"] - GlobalVars.apiquota >= 5000 and GlobalVars.apiquota >= 0:
GlobalVars.charcoal_hq.send_message("API quota rolled over with {0} requests remaining. Current quota: {1}.".format(GlobalVars.apiquota, response["quota_remaining"]))
sorted_calls_per_site = sorted(GlobalVars.api_calls_per_site.items(), key=itemgetter(1), reverse=True)
api_quota_used_per_site = ""
for site_name, quota_used in sorted_calls_per_site:
api_quota_used_per_site += site_name.replace('.com', '').replace('.stackexchange', '') + ": {0}\n".format(str(quota_used))
api_quota_used_per_site = api_quota_used_per_site.strip()
GlobalVars.charcoal_hq.send_message(api_quota_used_per_site, False)
clear_api_data()
if response["quota_remaining"] == 0:
GlobalVars.charcoal_hq.send_message("API reports no quota left! May be a glitch.")
GlobalVars.charcoal_hq.send_message(str(response)) # No code format for now?
if GlobalVars.apiquota == -1:
GlobalVars.charcoal_hq.send_message("Restart: API quota is {quota}.".format(quota=response["quota_remaining"]))
GlobalVars.apiquota = response["quota_remaining"]
else:
message_hq = "The quota_remaining property was not in the API response."
if "error_message" in response:
message_hq += " Error: {} at {} UTC.".format(response["error_message"], time_request_made)
if "error_id" in response and response["error_id"] == 502:
if GlobalVars.api_backoff_time < time.time() + 12: # Add a backoff of 10 + 2 seconds as a default
GlobalVars.api_backoff_time = time.time() + 12
message_hq += " Backing off on requests for the next 12 seconds."
if "backoff" in response:
if GlobalVars.api_backoff_time < time.time() + response["backoff"]:
GlobalVars.api_backoff_time = time.time() + response["backoff"]
match = regex.compile('/2.2/([^.]*)').search(url)
url_part = match.group(1) if match else url
message_hq += "\nBackoff received of {} seconds on request to `{}` at {} UTC".format(str(response["backoff"]), url_part, time_request_made)
if len(message_hq) > 0:
GlobalVars.charcoal_hq.send_message(message_hq.strip())
if "items" not in response:
return
if site == "stackoverflow.com":
items = response["items"]
if len(items) > 0 and "last_activity_date" in items[0]:
self.last_activity_date = items[0]["last_activity_date"]
for post in response["items"]:
if "title" not in post or "body" not in post:
continue
title = GlobalVars.parser.unescape(post["title"])
body = GlobalVars.parser.unescape(post["body"])
link = post["link"]
post_score = post["score"]
up_vote_count = post["up_vote_count"]
down_vote_count = post["down_vote_count"]
try:
owner_name = GlobalVars.parser.unescape(post["owner"]["display_name"])
owner_link = post["owner"]["link"]
owner_rep = post["owner"]["reputation"]
except:
owner_name = ""
owner_link = ""
owner_rep = 0
q_id = str(post["question_id"])
is_spam, reason, why = check_if_spam(title=title,
body=body,
user_name=owner_name,
user_url=owner_link,
post_site=site,
post_id=q_id,
is_answer=False,
body_is_summary=False,
owner_rep=owner_rep,
post_score=post_score)
if is_spam:
try:
handle_spam(title=title,
body=body,
poster=owner_name,
site=site,
post_url=link,
poster_url=owner_link,
post_id=q_id,
reasons=reason,
is_answer=False,
why=why,
owner_rep=owner_rep,
post_score=post_score,
up_vote_count=up_vote_count,
down_vote_count=down_vote_count,
question_id=None)
except:
print "NOP"
try:
for answer in post["answers"]:
answer_title = ""
body = answer["body"]
print "got answer from owner with name " + owner_name
link = answer["link"]
a_id = str(answer["answer_id"])
post_score = answer["score"]
up_vote_count = answer["up_vote_count"]
down_vote_count = answer["down_vote_count"]
try:
owner_name = GlobalVars.parser.unescape(answer["owner"]["display_name"])
owner_link = answer["owner"]["link"]
owner_rep = answer["owner"]["reputation"]
except:
owner_name = ""
owner_link = ""
owner_rep = 0
is_spam, reason, why = check_if_spam(title=answer_title,
body=body,
user_name=owner_name,
user_url=owner_link,
post_site=site,
post_id=a_id,
is_answer=True,
body_is_summary=False,
owner_rep=owner_rep,
post_score=post_score)
if is_spam:
try:
handle_spam(title=title,
body=body,
poster=owner_name,
site=site,
post_url=link,
poster_url=owner_link,
post_id=a_id,
reasons=reason,
is_answer=True,
why=why,
owner_rep=owner_rep,
post_score=post_score,
up_vote_count=up_vote_count,
down_vote_count=down_vote_count,
question_id=q_id)
except:
print "NOP"
except:
print "no answers"
return