forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletionwatcher.py
180 lines (145 loc) · 6.68 KB
/
deletionwatcher.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
# coding=utf-8
import json
import os.path
import time
import threading
from urllib.parse import urlparse
import requests
# noinspection PyPackageRequirements
import websocket
from globalvars import GlobalVars
import metasmoke
import datahandling
from helpers import log, get_se_api_default_params_questions_answers_posts_add_site, get_se_api_url_for_route
from parsing import fetch_post_id_and_site_from_url, to_protocol_relative
from tasks import Tasks
PICKLE_FILENAME = "deletionIDs.p"
# noinspection PyClassHasNoInit,PyBroadException,PyMethodParameters
class DeletionWatcher:
next_request_time = time.time() - 1
def __init__(self):
if GlobalVars.no_deletion_watcher:
return
self.posts = {}
self.posts_lock = threading.Lock()
self.save_handle = None
self.save_handle_lock = threading.Lock()
try:
self.socket = websocket.create_connection("wss://qa.sockets.stackexchange.com/")
except websocket.WebSocketException:
self.socket = None
log('error', 'DeletionWatcher failed to create a websocket connection')
return
if datahandling.has_pickle(PICKLE_FILENAME):
pickle_data = datahandling.load_pickle(PICKLE_FILENAME)
for post in DeletionWatcher._check_batch(pickle_data):
self.subscribe(post, pickle=False)
self._schedule_save()
threading.Thread(name="deletion watcher", target=self._start, daemon=True).start()
def _start(self):
while True:
msg = self.socket.recv()
if msg:
msg = json.loads(msg)
action = msg["action"]
if action == "hb":
self.socket.send("hb")
else:
data = json.loads(msg["data"])
if data["a"] == "post-deleted":
try:
with self.posts_lock:
post_id, _, _, post_url, callbacks = self.posts[action]
if post_id == str(data["aId"] if "aId" in data else data["qId"]):
with self.posts_lock:
del self.posts[action]
Tasks.do(self._unsubscribe, action)
Tasks.do(metasmoke.Metasmoke.send_deletion_stats_for_post, post_url, True)
for callback, max_time in callbacks:
if not max_time or time.time() < max_time:
callback()
except KeyError:
pass
def subscribe(self, post_url, callback=None, pickle=True, timeout=None):
if GlobalVars.no_deletion_watcher:
return
post_id, post_site, post_type = fetch_post_id_and_site_from_url(post_url)
with GlobalVars.site_id_dict_lock:
site_id = GlobalVars.site_id_dict.get(post_site, None)
if not site_id:
log("warning", "unknown site {} when subscribing to {}".format(post_site, post_url))
return
if post_type == "answer":
question_id = datahandling.get_post_site_id_link((post_id, post_site, post_type))
if question_id is None:
return
else:
question_id = post_id
action = "{}-question-{}".format(site_id, question_id)
max_time = (time.time() + timeout) if timeout else None
with self.posts_lock:
if action not in self.posts:
self.posts[action] = (post_id, post_site, post_type, post_url,
[(callback, max_time)] if callback else [])
Tasks.do(self._subscribe, action)
elif callback:
_, _, _, _, callbacks = self.posts[action]
callbacks.append((callback, max_time))
else:
return
if pickle:
self._schedule_save()
def _subscribe(self, action):
if self.socket:
try:
self.socket.send(action)
except websocket.WebSocketException:
log('error', 'DeletionWatcher failed to subscribe to {}'.format(action))
else:
log('warning', 'DeletionWatcher tried to subscribe to {}, but no WebSocket available.'.format(action))
def _schedule_save(self):
with self.save_handle_lock:
if self.save_handle:
self.save_handle.cancel()
save_handle = Tasks.do(self._save)
def _save(self):
pickle_output = {}
with self.posts_lock:
for post_id, post_site, _, _, _ in self.posts.values():
if post_site not in pickle_output:
pickle_output[post_site] = [post_id]
else:
pickle_output[post_site].append(post_id)
datahandling.dump_pickle(PICKLE_FILENAME, pickle_output)
@staticmethod
def _check_batch(saved):
if time.time() < DeletionWatcher.next_request_time:
time.sleep(DeletionWatcher.next_request_time - time.time())
for site, posts in saved.items():
ids = ";".join(post_id for post_id in posts if not DeletionWatcher._ignore((post_id, site)))
uri = get_se_api_url_for_route("posts/{}".format(ids))
params = get_se_api_default_params_questions_answers_posts_add_site(site)
res = requests.get(uri, params=params, timeout=GlobalVars.default_requests_timeout)
json = res.json()
if "items" not in json:
log('warning',
'DeletionWatcher API request received no items in response (code {})'.format(res.status_code))
log('warning', res.text)
return
if 'backoff' in json:
DeletionWatcher.next_request_time = time.time() + json['backoff']
for post in json['items']:
if time.time() - post["creation_date"] < 7200:
yield to_protocol_relative(post["link"]).replace("/q/", "/questions/")
def _unsubscribe(self, action):
if self.socket:
try:
self.socket.send("-" + action)
except websocket.WebSocketException:
log('error', 'DeletionWatcher failed to unsubscribe to {}'.format(action))
else:
log('warning', 'DeletionWatcher tried to unsubscribe to {}, but no WebSocket available.'.format(action))
@staticmethod
def _ignore(post_site_id):
return datahandling.is_false_positive(post_site_id) or datahandling.is_ignored_post(post_site_id) or \
datahandling.is_auto_ignored_post(post_site_id)