forked from Manishearth/ChatExchange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSEChatWrapper.py
More file actions
63 lines (52 loc) · 1.64 KB
/
SEChatWrapper.py
File metadata and controls
63 lines (52 loc) · 1.64 KB
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
import SEChatBrowser
import re
import time
TOO_FAST_RE = "You can perform this action again in (\d+) seconds"
class SEChatWrapper(object):
def __init__(self, site="SE"):
self.br = SEChatBrowser.SEChatBrowser()
self.site = site
self.previous = None
self.logged_in = False
def login(self, username, password):
assert not self.logged_in
self.br.loginSEOpenID(username, password)
if self.site == "SE":
self.br.loginSECOM()
self.br.loginChatSE()
elif self.site == "SO":
self.br.loginSO()
elif self.site == "MSO":
self.br.loginMSO()
self.logged_in = True
def logout(self):
assert self.logged_in
self.logged_in = False
pass # There are no threads to stop
def __del__(self):
assert not self.logged_in, "You forgot to log out."
def sendMessage(self, room, text):
assert self.logged_in
sent = False
if text == self.previous:
text = " " + text
while not sent:
wait = 0
response = self.br.postSomething("/chats/"+room+"/messages/new",
{"text": text})
if isinstance(response, str): # Whoops, too fast.
match = re.match(TOO_FAST_RE, response)
if match:
wait = int(match.group(1)) * 1.5
elif isinstance(response, dict):
if response["id"] is None: # Duplicate message?
text = text + " " # Let's not risk turning the message
wait = 1 # into a codeblock accidentally.
if wait:
print "Waiting %.1f seconds" % wait
time.sleep(wait)
else:
sent = True
self.previous = text
time.sleep(5)
return response