forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.py
76 lines (67 loc) · 2.39 KB
/
ws.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
#requires https://pypi.python.org/pypi/websocket-client/
import websocket
import threading
import json,os,sys,getpass,time
from findspam import FindSpam
from ChatExchange.SEChatWrapper import *
import HTMLParser
parser=HTMLParser.HTMLParser()
if("ChatExchangeU" in os.environ):
username=os.environ["ChatExchangeU"]
else:
print "Username: "
username=raw_input()
if("ChatExchangeP" in os.environ):
password=os.environ["ChatExchangeP"]
else:
password=getpass.getpass("Password: ")
lasthost=None
lastid=None
wrap=SEChatWrapper("SE")
wrap.login(username,password)
wrapm=SEChatWrapper("MSO")
wrapm.login(username,password)
s="[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] SmokeDetector started"
wrap.sendMessage("11540",s)
def checkifspam(data):
global lasthost,lastid
d=json.loads(json.loads(data)["data"])
s= d["titleEncodedFancy"]
print time.strftime("%Y-%m-%d %H:%M:%S"),parser.unescape(s).encode("ascii",errors="replace")
site = d["siteBaseHostAddress"]
site=site.encode("ascii",errors="replace")
sys.stdout.flush()
test=FindSpam.testpost(s,site)
if (0<len(test)):
if(lastid==d["id"] and lasthost == d["siteBaseHostAddress"]):
return False # Don't repost. Reddit will hate you.
lastid=d["id"]
lasthost = d["siteBaseHostAddress"]
return True
return False
def handlespam(data):
try:
d=json.loads(json.loads(data)["data"])
reason=",".join(FindSpam.testpost(d["titleEncodedFancy"],d["siteBaseHostAddress"]))
s="[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] %s: [%s](%s) on `%s`" % (reason,d["titleEncodedFancy"],d["url"],d["siteBaseHostAddress"])
print parser.unescape(s).encode('ascii',errors='replace')
wrap.sendMessage("11540",s)
wrapm.sendMessage("89",s)
except UnboundLocalError:
print "NOP"
ws = websocket.create_connection("ws://sockets.ny.stackexchange.com/")
ws.send("155-questions-active")
wrap.joinRoom("11540")
def watcher(msg,wrap2):
if(msg["content"].startswith("!!/stappit")):
if(str(msg["user_id"]) in ["31768","103081","73046"]):
wrap2.sendMessage("11540","Goodbye, cruel world")
os._exit(1)
wrap.watchRoom("11540",watcher,300)
while True:
a=ws.recv()
if(a!= None and a!= ""):
if(checkifspam(a)):
threading.Thread(target=handlespam,args=(a,)).start()
s="[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] SmokeDetector aborted"
wrap.sendMessage("11540",s)