You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.6 KiB

#!/usr/bin/python3
from subprocess import call
import os
import socket
import re
config = {
"token": None,
"nickname": 'ianmethyst',
"channel": '#ianmethyst',
"supress_own": True,
"short_notification": True
}
try:
config['token'] = os.environ['TWITCH_OAUTH']
except KeyError:
print("Get your OAUTH token here: https://twitchapps.com/tmi/",
"\nThen pass it as an environment variable:",
f"\nTWITCH_OAUTH=<token> {__file__}")
exit(1)
HOST = 'irc.chat.twitch.tv'
PORT = 6667
MESSAGE_REGEX = ':(.*)\!.*@.* PRIVMSG #(.*) :(.*)'
sock = socket.socket()
connected = False
def connect():
sock.connect((HOST, PORT))
sock.send(f"PASS {config['token']}\n".encode('utf-8'))
sock.send(f"NICK {config['nickname']}\n".encode('utf-8'))
sock.send(f"JOIN {config['channel']}\n".encode('utf-8'))
connect()
while True:
resp = sock.recv(2048).decode('utf-8')
if resp.startswith('PING'):
sock.send("PONG\n".encode('utf-8'))
elif len(resp) > 0:
if connected:
user, channel, message = re.search(MESSAGE_REGEX, resp).groups()
if config['short_notification']:
notification_text = f"@{user}: {message}"
else:
notification_text = f"@{user} in #{channel}\n{message}"
if not (config['supress_own'] and config['nickname'] == user):
call(["notify-send", notification_text])
if "End of /NAMES list" in resp:
connected = True
call([
"notify-send",
f"connected to {config['channel']} as {config['nickname']}"])