top of page

TwitchPlays_Connection.py

for Python 2.7

[ published on 2019-10-31 ]

  1. # DougDoug Note: 

  2. # This is the code that connects to twitch and checks for new messages.

  3. # You should not need to modify anything in this file, just use as is.

  4. # All code in this file is from Wituz's "Twitch Plays" tutorial at:

  5. # http://www.wituz.com/make-your-own-twitch-plays-stream.html

  6.  

  7. import socket

  8. import sys

  9. import re

  10.  

  11. class Twitch:

  12.  

  13.     user = "";

  14.     oauth = "";

  15.     s = None;

  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.     def twitch_connect(self, user, key):

  20.         self.user = user;

  21.         self.oauth= key;

  22.         print("Connecting to twitch.tv");

  23.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);

  24.         s.settimeout(0.6);

  25.         connect_host = "irc.twitch.tv";

  26.         connect_port = 6667;

  27.         try:

  28.             s.connect((connect_host, connect_port));

  29.         except:

  30.             print("Failed to connect to twitch");

  31.             sys.exit();

  32.         print("Connected to twitch");

  33.         print("Sending our details to twitch...");

  34.         s.send('USER %s\r\n' % user);

  35.         s.send('PASS %s\r\n' % key);

  36.         s.send('NICK %s\r\n' % user);

  37.         if not self.twitch_login_status(s.recv(1024)):

  38.             print("... and they didn't accept our details");

  39.             sys.exit();

  40.         else:

  41.             print("... they accepted our details");

  42.             print("Connected to twitch.tv!")

  43.             self.s = s;

  44.             s.send('JOIN #%s\r\n' % user)

  45.             s.recv(1024);

  46.     def check_has_message(self, data):

  47.         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)

  48.     def parse_message(self, data):

  49.         return {

  50.             'channel': re.findall(r'^:.+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+.+ PRIVMSG (.*?) :', data)[0],

  51.             'username': re.findall(r'^:([a-zA-Z0-9_]+)\!', data)[0],

  52.             'message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)', data)[0].decode('utf8')

  53.         }

  54.     def twitch_recieve_messages(self, amount=1024):

  55.         data = None

  56.         try: data = self.s.recv(1024);

  57.         except: return False;

  58.         if not data:

  59.             print("Lost connection to Twitch, attempting to reconnect...");

  60.             self.twitch_connect(self.user, self.oauth);

  61.             return None

  62.         #self.ping(data)

  63.         if self.check_has_message(data):

  64.             return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];

End of Document

bottom of page