Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active July 20, 2016 21:21
Show Gist options
  • Save Lokno/1c9ceb74739dd0cd9df0ab71e675029f to your computer and use it in GitHub Desktop.
Save Lokno/1c9ceb74739dd0cd9df0ab71e675029f to your computer and use it in GitHub Desktop.
Script to update a ratio via commands posted in a twitch channel
# Script to update a ratio via commands posted in a twitch channel
# See: http://help.twitch.tv/customer/portal/articles/1302780-twitch-irc
import socket
import re,os
server = "irc.chat.twitch.tv"
channel = "#CHANNEL"
botnick = "NICKNAME"
password = "OAUTHPASS"
path = "OUTPUTPATH"
cmdRE = re.compile("!(total|add|reset|msg) ?(.*)")
nameRE = re.compile("^:NICKNAME!")
digitsRE = re.compile("\d+")
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "connecting to:" + server
irc.connect((server, 6667))
irc.send("PASS " + password + "\n")
irc.send("NICK " + botnick + "\n")
irc.send("JOIN " + channel + "\n")
current = 0
total = 420
msg = "(blaziken it)"
os.system('echo "%d / %d %s" > %s/rowcount.txt' % (current,total,msg,path))
while 1:
text = irc.recv(2040)
cmdM = cmdRE.search(text)
nameM = nameRE.search(text)
if nameM and cmdM:
if cmdM.group(1) == "total" and digitsRE.match(cmdM.group(2)):
total = int(cmdM.group(2))
elif cmdM.group(1) == "add" and digitsRE.match(cmdM.group(2)):
current = min(total,current+int(cmdM.group(2)))
elif cmdM.group(1) == "reset":
current = 0
elif cmdM.group(1) == "msg":
msg = cmdM.group(2)
os.system('echo "%d / %d %s" > %s/rowcount.txt' % (current,total,msg,path))
# sends 'PONG' if 'PING' received to prevent pinging out
if text.find('PING') != -1:
irc.send('PONG ' + text.split() [1] + '\r\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment