forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletionwatcher.py
68 lines (61 loc) · 2.57 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
import json
import requests
import time
import websocket
from bs4 import BeautifulSoup
from globalvars import GlobalVars
from datahandling import is_false_positive, is_ignored_post, get_post_site_id_link
class DeletionWatcher:
@classmethod
def update_site_id_list(self):
soup = BeautifulSoup(requests.get("http://meta.stackexchange.com/topbar/site-switcher/site-list").text)
site_id_dict = {}
for site in soup.findAll("a", attrs={"data-id": True}):
site_name = site["href"][2:]
site_id = site["data-id"]
site_id_dict[site_name] = site_id
GlobalVars.site_id_dict = site_id_dict
@classmethod
def check_websocket_for_deletion(self, post_site_id, timeout):
time_to_check = time.time() + timeout
post_id = post_site_id[0]
post_type = post_site_id[2]
if post_type == "answer":
question_id = str(get_post_site_id_link(post_site_id))
if question_id is None:
return
else:
question_id = post_id
post_site = post_site_id[1]
if post_site not in GlobalVars.site_id_dict:
return
site_id = GlobalVars.site_id_dict[post_site]
ws = websocket.create_connection("ws://qa.sockets.stackexchange.com/")
ws.send(site_id + "-question-" + question_id)
while time.time() < time_to_check:
ws.settimeout(time_to_check - time.time())
try:
a = ws.recv()
except websocket.WebSocketTimeoutException:
return False
if a is not None and a != "":
try:
d = json.loads(json.loads(a)["data"])
except:
continue
if d["a"] == "post-deleted" and str(d["qId"]) == question_id and ((post_type == "answer" and "aId" in d and str(d["aId"]) == post_id) or post_type == "question"):
return True
return False
@classmethod
def check_if_report_was_deleted(self, post_site_id, message):
was_report_deleted = self.check_websocket_for_deletion(post_site_id, 120)
if was_report_deleted:
try:
message.delete()
except:
pass
@classmethod
def post_message_if_not_deleted(self, post_site_id, message_text, room):
was_report_deleted = self.check_websocket_for_deletion(post_site_id, 300)
if not was_report_deleted and not is_false_positive(post_site_id[0:2]) and not is_ignored_post(post_site_id[0:2]):
room.send_message(message_text)