-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCConnector.py
97 lines (88 loc) · 3.38 KB
/
IRCConnector.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
import socket
from threading import Thread
import re
import requests
import json
class IRC:
PONG = 808
def __init__(self, HOST = "irc.chat.twitch.tv", PORT=6667, **kwargs):
self.con = socket.socket()
try:
self.con.connect((HOST, PORT))
except Exception as e:
print(e)
self.con.setblocking(False)
self.con.settimeout(120)
self.debugging = False
for key, value in kwargs.items():
if key == "debugging" and value == True:
self.debugging = True
print("Debugging enabled")
def Login(self,OAuth = None, NICK = None, CHANNEL = None):
PASSCMD = "PASS {}\r\n".format(OAuth)
NICKCMD = "NICK {}\r\n".format(NICK)
JOINCMD = "JOIN #{}\r\n".format(CHANNEL)
self.con.send(bytes(PASSCMD, "UTF-8"))
self.con.send(bytes(NICKCMD, "UTF-8"))
self.con.send(bytes(JOINCMD, "UTF-8"))
self.Channel = CHANNEL
self.Nickname = NICK
self.REGEX = "^:(\w+)!\w+@\w+.tmi.twitch.tv\s?(?:PRIVMSG)\s#(\w+)\s?:(.*)\r?\n?$"
self.ChannelList = [CHANNEL]
self.ChannelMetadata = {}
def GetMessage(self, callback):
MSG = ""
self.MessageCallback = callback
while True:
try:
MSG = self.con.recv(1024).decode('UTF-8')
if self.debugging:
print(MSG)
if len(MSG) > 0:
if MSG[0:4] == "PING":
self.SendPong()
continue
if MSG[0:7] != "PRIVMSG":
m = re.search(self.REGEX, str(MSG).strip("\r\n"))
if m != None:
callback(m.group(1), m.group(3), m.group(2))
else:
pass
except socket.timeout as ex:
pass
except socket.error as ex:
print(ex)
except Exception as ex:
print(ex)
def OnMessage(self, callback):
t = Thread(target=self.GetMessage, args=(callback,))
t.start()
self.RetrieveThread = t
return t
def Say(self, message, channel = None):
if channel == None:
self.con.send(bytes("PRIVMSG #{} :{}\r\n".format(self.Channel,message), "UTF-8"))
else:
self.con.send(bytes("PRIVMSG #{} :{}\r\n".format(channel,message), "UTF-8"))
self.MessageCallback(self.Nickname, message, self.Channel)
def Close(self):
self.con.send(bytes("PART #{}\r\n".format(self.Channel), "UTF-8"))
self.con.close()
def Join(self, channel):
self.con.send(bytes("JOIN #{}\r\n".format(channel), "UTF-8"))
self.ChannelList.append(channel)
self.Channel = channel
def SendPong(self):
self.con.send(bytes("PONG :tmi.twitch.tv\r\n", "UTF-8"))
def ChangeChannel(self, channel):
if channel in self.ChannelList:
self.Channel = channel
def Clearchat(self):
self.Say("/clear")
def GetInformation(self):
r=requests.get("http://tmi.twitch.tv/group/user/{}/chatters".format(self.Channel))
j = json.loads(r.text)
if self.Channel not in self.ChannelMetadata:
self.ChannelMetadata[self.Channel] = []
for m in j["chatters"]["moderators"]:
self.ChannelMetadata[self.Channel].append(m)