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. ​

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

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

  7.  

  8. import socket

  9. import sys

  10. import re

  11.  

  12. class Twitch:

  13.  

  14.     user = "";

  15.     oauth = "";

  16.     s = None;

  17. ​

  18.     def twitch_login_status(self, data):

  19.         if not re.match(r'^:(testserver\.local|tmi\.twitch\.tv) NOTICE \* :Login unsuccessful\r\n$', data): return True

  20.         else: return False

  21. ​

  22.     def twitch_connect(self, user, key):

  23.         self.user = user;

  24.         self.oauth= key;

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

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

  27.         s.settimeout(0.6);

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

  29.         connect_port = 6667;

  30.         try:

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

  32.         except:

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

  34.             sys.exit();

  35.         print("Connected to twitch");

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

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

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

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

  40. ​

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

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

  43.             sys.exit();

  44.         else:

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

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

  47.             self.s = s;

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

  49.             s.recv(1024);

  50. ​

  51.     def check_has_message(self, data):

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

  53. ​

  54.     def parse_message(self, data):

  55.         return {

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

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

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

  59.         }

  60. ​

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

  62.         data = None

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

  64.         except: return False;

  65. ​

  66.         if not data:

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

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

  69.             return None

  70. ​

  71.         #self.ping(data)

  72. ​

  73.         if self.check_has_message(data):

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

End of Document

bottom of page