daily pastebin goal
59%
SHARE
TWEET

twitch.py - Twitch Functions

WiesenWiesel Apr 3rd, 2015 (edited) 419 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import sys
  3. import re
  4.  
  5. connect_host = "irc.twitch.tv"
  6. connect_port = 6667
  7.  
  8. class Twitch:
  9.  
  10.     user = ""
  11.     oauth = ""
  12.     channel_reconnect = ""
  13.  
  14.     s = None
  15.  
  16.     def twitch_login_status(self, data):
  17.         if not re.match(r'^:(testserver\.local|tmi\.twitch\.tv) NOTICE \* :Login unsuccessful\r\n$', data): return True
  18.         else: return False
  19.  
  20.     def twitch_connect(self, user, key, channel):
  21.         self.channel_reconnect = channel  # Saving the channel name, for later reconnect
  22.         self.user = user
  23.         self.oauth= key
  24.         print("Connecting to twitch.tv")
  25.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26.         s.settimeout(0.6)
  27.  
  28.         try:
  29.             s.connect((connect_host, connect_port))
  30.         except socket.error:
  31.             print("Failed to connect to twitch")
  32.             sys.exit()
  33.  
  34.         print("Connected to " + connect_host + "\n")
  35.         print("Sending Details:\n")
  36.         print('USER %s\r\n' % user)
  37.         print('PASS %s\r\n' % key)
  38.         print('NICK %s\r\n' % user)
  39.  
  40.         s.send('USER %s\r\n' % user)
  41.         s.send('PASS %s\r\n' % key)
  42.         s.send('NICK %s\r\n' % user)
  43.  
  44.         if not self.twitch_login_status(s.recv(1024)):
  45.             print("Authentication failure.")
  46.             sys.exit()
  47.         else:
  48.             print("Authentication successful!")
  49.             print("Connected to " + connect_host + ":" +str(connect_port))
  50.             self.s = s
  51.             s.send('JOIN #%s\r\n' % channel)
  52.             try:
  53.                 s.recv(1024)
  54.             except socket.error:
  55.                 print("ERROR: Socket Error")
  56.  
  57.  
  58.     def check_has_message(self, data):
  59.         return re.match(r'^:[a-zA-Z0-9_]+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\.tmi\.twitch\.tv|\.testserver\.local) PRIVMSG #[a-zA-Z0-9_]+ :.+$', data)
  60.  
  61.     def parse_message(self, data):
  62.         return {
  63.             'channel': re.findall(r'^:.+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+.+ PRIVMSG (.*?) :', data)[0],
  64.             'username': re.findall(r'^:([a-zA-Z0-9_]+)\!', data)[0],
  65.             'message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)', data)[0].decode('utf8')
  66.         }
  67.  
  68.     def send_message(self, channel, data):
  69.             self.s.send('PRIVMSG #' + channel + ' :/me '+data+'\r\n')
  70.  
  71.     def twitch_receive_messages(self, amount=1024):
  72.         data = None
  73.         try: data = self.s.recv(1024);
  74.         except: return False;
  75.  
  76.         if not data:
  77.             print("Lost connection to Twitch, attempting to reconnect...");
  78.             self.twitch_connect(self.user, self.oauth, self.channel_reconnect);
  79.             return None
  80.  
  81.         #self.ping(data)
  82.  
  83.         if self.check_has_message(data):
  84.             print(data);
  85.             return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];
RAW Paste Data
Top