Advertisement
Guest User

TwitchCollectsInputs

a guest
Feb 15th, 2014
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import re, socket, sys, threading
  2.  
  3. HOSTNAME = "irc.twitch.tv"
  4. PORT = 6667
  5. CHANNEL = "#twitchplayspokemon"
  6. NICKNAME = "YOUR_NICK_HERE"
  7. REALNAME = "YOUR_NAME_HERE"
  8. OAUTH = "OAUTH_KEY_HERE"
  9. RECV_BUFFER = 4096
  10. CONTROLS = { "left": 0, "right": 0, "up": 0, "down": 0, "a": 0, "b": 0, "start": 0, "select": 0 }
  11. REGEX = r":\w+"
  12.  
  13. # Open the IRC socket
  14. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  15.  
  16. print("Connecting to {}".format(HOSTNAME))
  17. irc.connect((HOSTNAME, PORT))
  18. irc.send("PASS {}\r\n".format(OAUTH))
  19. irc.send("NICK {}\r\n".format(NICKNAME))
  20. irc.send("USER {} {} {} :{}\r\n".format(NICKNAME, HOSTNAME, "", REALNAME))
  21. irc.send("JOIN {}\r\n".format(CHANNEL))
  22.  
  23. # Function definitions
  24. def send(msg):
  25.     print("REPLY: {}".format(msg))
  26.     irc.send(msg)
  27.  
  28. def parse_privmsg(msg, controls):
  29.     try:
  30.         CONTROLS[msg] += 1
  31.         print("{}: {}".format(msg, CONTROLS[msg]))
  32.     except KeyError:
  33.         pass
  34.  
  35. def reset():
  36.     global CONTROLS
  37.  
  38.     max_key = max(CONTROLS.iterkeys(), key = (lambda key: CONTROLS[key]))
  39.  
  40.     print("""
  41. #################################################
  42.                    RESET
  43. #################################################
  44.  
  45.    WINNER: {}
  46.    COUNT: {}
  47.  
  48.    TOTAL COMMANDS: {}
  49.  
  50. #################################################
  51. """.format(max_key, CONTROLS[max_key], CONTROLS["right"], CONTROLS["down"], sum(CONTROLS.values())))
  52.  
  53.     CONTROLS = { "left": 0, "right": 0, "up": 0, "down": 0, "a": 0, "b": 0, "start": 0, "select": 0 }
  54.     threading.Timer(15.0, reset).start()
  55.  
  56. # Start the timer
  57. reset()
  58.  
  59. # Loop endlessly
  60. while True:
  61.     # Receive messages from socket
  62.     data = irc.recv(4096)
  63.  
  64.     # Respond to PING messages with PONG
  65.     if data.find("PING") != -1:
  66.         send(data.replace("PING", "PONG"))
  67.  
  68.     # Collect PRIVMSG, they are what is said by users in the chat
  69.     if data.find("PRIVMSG") != -1:
  70.         # Chat messages follow the regex ":\w+", i.e. :right
  71.         for command in re.findall(REGEX, data):
  72.             parse_privmsg(command[1:], CONTROLS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement