-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigil.py
166 lines (151 loc) · 5.92 KB
/
vigil.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
import json
import pika
import re
import time
import websocket
from os import environ
import servitor_utils
try:
import thread
except ImportError:
import _thread as thread
import time
settings = servitor_utils.make_settings(environ.get('SETTINGS_FILE'))
auth_data = servitor_utils.make_auth(environ.get('SECRETS_FILE'))
def check_message(ws, message):
if message[0] == "@":
arg_regx = r"([^=;]*)=([^ ;]*)"
arg_regx = re.compile(arg_regx, re.UNICODE)
args = dict(re.findall(arg_regx, message[1:]))
regex = (r'^@[^ ]* :([^!]*)![^!]*@[^.]*.tmi.twitch.tv' # username
r' PRIVMSG #([^ ]*)' # channel
r' :(.*)') # message
regex = re.compile(regex, re.UNICODE)
match = re.search(regex, message)
if match:
args['username'] = match.group(1)
args['channel'] = match.group(2)
args['message'] = match.group(3)
args['type'] = "PRIVMSG"
print args['type'] + " <" + args['username'] + "> " + args['message']
return True
elif message[0] == ":":
regex = (r':([^!]*)![^!]*@[^.]*.tmi.twitch.tv' # username
r' [#]?PRIVMSG ([^ ]*)' # channel
r' :(.*)') # message
regex = re.compile(regex, re.UNICODE)
match = re.search(regex, message)
args = dict()
if match:
args['username'] = match.group(1)
args['channel'] = match.group(2)
args['message'] = match.group(3).rstrip()
if args['username'] == "jtv":
args['type'] = "SYSNOTICE"
if is_hosting(args['message']):
args['sub-type'] = "HOST"
args['sub-type-username'] = args['message'].split(" ")[0]
print args['type'] + " " + args['sub-type'] + " " + args['sub-type-username']
servitor_utils.send_amqp_notice(host=settings['amqp_server'],
exchange=settings['amqp_exchange'],
topic=settings['topics']['irc'],
message=args)
print "sent something"
else:
print args['type'] + " " + args['username'] + " " + args['message']
else:
args['type'] = "OTHERMSG"
print args['type'] + " " + args['username'] + " " + args['message']
else:
print message
def is_subscription(usernotice):
if "subscribed" in usernotice:
return True
else:
return False
def is_hosting(message):
if "hosting" in message:
return True
else:
return False
def check_usernotice(ws, message):
if message[0] == "@":
arg_regx = r"([^=;]*)=([^ ;]*)"
arg_regx = re.compile(arg_regx, re.UNICODE)
args = dict(re.findall(arg_regx, message[1:]))
regex = (
r'^@[^ ]* :tmi.twitch.tv'
r' USERNOTICE #(?P<channel>[^ ]*)' # channel
r'((?: :)?(?P<message>.*))?') # message
regex = re.compile(regex, re.UNICODE)
match = re.search(regex, message)
if match:
args['channel'] = match.group(1)
args['message'] = match.group(2)
args['type'] = "USERNOTICE"
system_message = args['system-msg'].split("\s")
args['user'] = system_message[0]
if is_subscription(system_message):
args['sub-type'] = "SUBSCRIPTION"
else:
args['sub-type'] = "OTHER"
print args['type'] + " " + args['user'] + " " + args['sub-type'] + " full message:" + re.sub(r'\\s', ' ', args['system-msg'])
return True
def check_ping(ws, message):
if re.search(r"PING :tmi\.twitch\.tv", message):
ws.send("PONG :tmi.twitch.tv")
return True
def check_error(ws, message):
if re.search(r":tmi.twitch.tv NOTICE \* :Error logging i.*", message):
ws.close()
print("closing session due to logging error")
def on_message(ws, message):
if check_message(ws, message):
return
elif check_ping(ws, message):
return
elif check_usernotice(ws, message):
return
elif check_error(ws, message):
return
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws, settings, auth_token):
def run(*args):
nickname_command = "NICK {}".format(settings['nickname'])
irc_channel_command = "JOIN {}".format(settings['irc_channel'])
pass_command = "PASS oauth:{}".format(auth_token)
time.sleep(5)
ws.send(pass_command)
time.sleep(5)
ws.send(nickname_command)
time.sleep(1)
ws.send(irc_channel_command)
time.sleep(1)
ws.send("CAP REQ :twitch.tv/membership")
time.sleep(1)
ws.send("CAP REQ :twitch.tv/tags")
time.sleep(1)
ws.send("CAP REQ :twitch.tv/commands")
message = dict()
message['message'] = "signon to Twitch IRC succeeded"
message['sub-type'] = "NOTICE"
message['sub-type-username'] = "vigil"
servitor_utils.send_amqp_notice(host=settings['amqp_server'],
exchange=settings['amqp_exchange'],
topic=settings['topics']['irc'],
message=message)
thread.start_new_thread(run, ())
if __name__ == "__main__":
twitch_token = auth_data['twitch_irc_token']
websocket.enableTrace(True)
websocket_server = settings['websocket_irc_server']
ws = websocket.WebSocketApp(websocket_server,
on_message = on_message,
on_error = on_error,
on_close = on_close,
)
ws.on_open = on_open(ws, settings, twitch_token)
ws.run_forever()