-
Notifications
You must be signed in to change notification settings - Fork 1
/
twirc.py
211 lines (161 loc) · 5.71 KB
/
twirc.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import re
import socket
import time
from collections import namedtuple
from conf import settings
PING_RE = re.compile("^PING\ :(\w+\.twitch\.tv)")
# 1: Username
# 2: Action/Command
# 3: Channel
METADATA_PARSER = re.compile("^([0-9a-zA-Z\_]+)![0-9a-zA-Z\_]+@[0-9a-zA-Z\_]+\.[0-9a-zA-Z]+\.twitch\.tv\ ([0-9a-zA-Z\_]+)\ ([\#0-9a-zA-Z_]+)$")
def to_bytes(val):
if isinstance(val, bytes):
return val
return val.encode("utf-8")
def from_bytes(val):
if isinstance(val, str):
return val
return val.decode()
BaseMessage = namedtuple("Message", ["tags_str", "username", "action", "channel", "content"])
class Message(BaseMessage):
@classmethod
def from_text(cls, data):
"""
Given a Twitch IRC message text including tags, parse
the message and return a BaseMessage tuple.
Each message has three disctinct parts:
1. tags https://dev.twitch.tv/docs/irc/tags
2. metadata - username, action/command, channel
3. content - the actual content of the message such as the text someone entered
"""
try:
tags_str, metadata_str, content = [p.strip() for p in data.split(":")]
except ValueError:
return None
metadata = METADATA_PARSER.match(metadata_str)
if metadata is not None:
return cls(
tags_str,
metadata.group(1),
metadata.group(2),
metadata.group(3),
content,
)
return None
def _parse_tags(self):
"""
Lazy parsing for tags.
"""
tokens = self.tags_str[1:].split(";")
self._tags = {
k.strip(): v
for token in tokens
for k, v in [token.split("=")]
}
@property
def tags(self):
"""
Tags exist as a string on the object until this property is
accessed when the tags will be parsed.
This will return a dictionary of tags for the message.
"""
if not hasattr(self, "_tags"):
self._parse_tags()
return self._tags
class Client:
"""
Handles a connection to a Twitch IRC channel as well as callbacks for events.
"""
def __init__(self, password, nick, channel, addr=None):
self.addr = addr.split(":") if addr is not None else ("irc.twitch.tv", 6667)
self.password = password
self.nick = nick
self.channel = "#{}".format(channel) if not channel.startswith("#") else channel
self.sock = None
self.is_connected = False
self._registry = dict()
def connect(self):
"""
Connects to the IRC server and joins the given channel.
This also will request tags in messages.
"""
self.sock = socket.socket()
self.sock.connect(self.addr)
self.send("PASS {}".format(self.password))
self.send("NICK {}".format(self.nick))
self.send("JOIN {}".format(self.channel))
self.send("CAP REQ :twitch.tv/tags")
self.is_connected = True
# TODO: error handling
def _listen(self):
"""
Main listen loop.
First ensures the client is connected. Following that it will
receive messages every second and call the handler function.
"""
if not self.is_connected:
self.connect()
while True:
data = self.recv()
ping = PING_RE.match(data)
if ping:
self.handle_ping(ping.group(1))
else:
result = self.handle_message(data)
if result:
print(result)
time.sleep(1)
def listen(self):
"""
Wrapper function to cleanly handle interrupts from the
listen loop.
"""
try:
self._listen()
except KeyboardInterrupt:
self.sock.close()
self.is_connected = False
def recv(self):
"""
Receive messages from the server and convert to string.
"""
return from_bytes(self.sock.recv(512))
def send(self, val, end="\n"):
"""
Send message to the server, handling line ending and conversion to bytestring.
"""
self.sock.send(to_bytes("{val}{end}".format(val=val, end=end)))
def handle_ping(self, host):
"""
Called when Twitch IRC sends a PING message and responds with PONG.
See: https://dev.twitch.tv/docs/irc/guide#connecting-to-twitch-irc
"""
self.send("PONG :{}".format(host))
def handle_message(self, data):
"""
Currently not final. Debugs message parsing.
"""
message = Message.from_text(data)
if message is not None:
print(message.username, message.action, message.channel, message.content)
self._callback("message", message) # TODO: add additional callbacks
def register_handler(self, event, fn, unique=False):
"""
Accepts an event and function and registers that function as
an event handler to be called.
If unique is set to true then all other handlers will be removed.
"""
if event not in self._registry or unique:
self._registry[event] = [fn]
else:
self._registry[event].append(fn)
return fn
def on_message(self, fn, unique=False):
return self.register_handler("message", fn, unique=unique)
def _callback(self, event, message):
# TODO: error handling
for fn in self._registry.get(event, []):
fn(self, message)
# Client for testing things, remove me later
#client = Client(settings.TWITCH_IRC_TOKEN, settings.TWITCH_NICK, settings.TWITCH_CHANNEL)
#client.connect()