Just a very minimalistic twitch irc client in python (great to gather statistics about the chat)
Everything is explained with comments.
Feel free to suggest improvements.
Code:
import socket
# twitch irc host
HOST = "irc.twitch.tv
# default irc port
PORT = 6667
# channelname
CHAN = "#channelname"
# nickname, "justinfan[randomnumber]" is used to connect anonymously to irc.twitch.tv
# if you want to send messages you need to use a oauth token and send it like that:
# con.send("PASS" + OAUTH)
NICK = "justinfan6213234863"
# init a new socket
con = socket.socket()
# connect to irc.twitch.tv
con.connect((HOST, PORT))
# login and join channel
con.send("USER " + NICK + "\r\n")
con.send("NICK " + NICK + "\r\n")
con.send("JOIN " + CHAN + "\r\n")
# infinite loop to receive data
# I recommend using threads in a production enviroment - more control!
while 1:
# receive some data
data = str(con.recv(1024))
# check if you received something
if not data:
break
# check for a message
elif "PRIVMSG" in data:
msg = data.split(":")[2:]
msg = "".join(msg).rstrip()
print "MESSAGE: " + msg
# you will receive a ping from twitch from time to time
# - send a pong back, to make sure you don't get disconnected
elif "PING" in data:
print "PING - PONG"
con.send("PONG tmi.twitch.tv\r\n")
# some other data, that i don't care about
else:
print "SOMETHINGELSE: "+data
# close the connection
con.close()