Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, socket, sys, threading
- HOSTNAME = "irc.twitch.tv"
- PORT = 6667
- CHANNEL = "#twitchplayspokemon"
- NICKNAME = "YOUR_NICK_HERE"
- REALNAME = "YOUR_NAME_HERE"
- OAUTH = "OAUTH_KEY_HERE"
- RECV_BUFFER = 4096
- CONTROLS = { "left": 0, "right": 0, "up": 0, "down": 0, "a": 0, "b": 0, "start": 0, "select": 0 }
- REGEX = r":\w+"
- # Open the IRC socket
- irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- print("Connecting to {}".format(HOSTNAME))
- irc.connect((HOSTNAME, PORT))
- irc.send("PASS {}\r\n".format(OAUTH))
- irc.send("NICK {}\r\n".format(NICKNAME))
- irc.send("USER {} {} {} :{}\r\n".format(NICKNAME, HOSTNAME, "", REALNAME))
- irc.send("JOIN {}\r\n".format(CHANNEL))
- # Function definitions
- def send(msg):
- print("REPLY: {}".format(msg))
- irc.send(msg)
- def parse_privmsg(msg, controls):
- try:
- CONTROLS[msg] += 1
- print("{}: {}".format(msg, CONTROLS[msg]))
- except KeyError:
- pass
- def reset():
- global CONTROLS
- max_key = max(CONTROLS.iterkeys(), key = (lambda key: CONTROLS[key]))
- print("""
- #################################################
- RESET
- #################################################
- WINNER: {}
- COUNT: {}
- TOTAL COMMANDS: {}
- #################################################
- """.format(max_key, CONTROLS[max_key], CONTROLS["right"], CONTROLS["down"], sum(CONTROLS.values())))
- CONTROLS = { "left": 0, "right": 0, "up": 0, "down": 0, "a": 0, "b": 0, "start": 0, "select": 0 }
- threading.Timer(15.0, reset).start()
- # Start the timer
- reset()
- # Loop endlessly
- while True:
- # Receive messages from socket
- data = irc.recv(4096)
- # Respond to PING messages with PONG
- if data.find("PING") != -1:
- send(data.replace("PING", "PONG"))
- # Collect PRIVMSG, they are what is said by users in the chat
- if data.find("PRIVMSG") != -1:
- # Chat messages follow the regex ":\w+", i.e. :right
- for command in re.findall(REGEX, data):
- parse_privmsg(command[1:], CONTROLS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement