Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

twitch.py

Wituz Feb 24th, 2014 4,950 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import socket
  2. import sys
  3. import re
  4.  
  5. class Twitch:
  6.  
  7.     user = "";
  8.     oauth = "";
  9.     s = None;
  10.  
  11.     def twitch_login_status(self, data):
  12.         if not re.match(r'^:(testserver\.local|tmi\.twitch\.tv) NOTICE \* :Login unsuccessful\r\n$', data): return True
  13.         else: return False
  14.  
  15.     def twitch_connect(self, user, key):
  16.         self.user = user;
  17.         self.oauth= key;
  18.         print("Connecting to twitch.tv");
  19.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
  20.         s.settimeout(0.6);
  21.         connect_host = "irc.twitch.tv";
  22.         connect_port = 6667;
  23.         try:
  24.             s.connect((connect_host, connect_port));
  25.         except:
  26.             print("Failed to connect to twitch");
  27.             sys.exit();
  28.         print("Connected to twitch");
  29.         print("Sending our details to twitch...");
  30.         s.send('USER %s\r\n' % user);
  31.         s.send('PASS %s\r\n' % key);
  32.         s.send('NICK %s\r\n' % user);
  33.  
  34.         if not self.twitch_login_status(s.recv(1024)):
  35.             print("... and they didn't accept our details");
  36.             sys.exit();
  37.         else:
  38.             print("... they accepted our details");
  39.             print("Connected to twitch.tv!")
  40.             self.s = s;
  41.             s.send('JOIN #%s\r\n' % user)
  42.             s.recv(1024);
  43.  
  44.     def check_has_message(self, data):
  45.         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)
  46.  
  47.     def parse_message(self, data):
  48.         return {
  49.             'channel': re.findall(r'^:.+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+.+ PRIVMSG (.*?) :', data)[0],
  50.             'username': re.findall(r'^:([a-zA-Z0-9_]+)\!', data)[0],
  51.             'message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)', data)[0].decode('utf8')
  52.         }
  53.  
  54.     def twitch_recieve_messages(self, amount=1024):
  55.         data = None
  56.         try: data = self.s.recv(1024);
  57.         except: return False;
  58.  
  59.         if not data:
  60.             print("Lost connection to Twitch, attempting to reconnect...");
  61.             self.twitch_connect(self.user, self.oauth);
  62.             return None
  63.  
  64.         #self.ping(data)
  65.  
  66.         if self.check_has_message(data):
  67.             return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top