diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b69c02c --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.gpg binary diff --git a/data/twitch-oauth.gpg b/data/twitch-oauth.gpg new file mode 100644 index 0000000..03428a6 Binary files /dev/null and b/data/twitch-oauth.gpg differ diff --git a/chromium-twitch-chat b/launch-twitch-chat similarity index 100% rename from chromium-twitch-chat rename to launch-twitch-chat diff --git a/launch-twitch-notify b/launch-twitch-notify new file mode 100755 index 0000000..ab0c551 --- /dev/null +++ b/launch-twitch-notify @@ -0,0 +1,3 @@ +#!/bin/bash + +TWITCH_OAUTH=$(gpg -q --decrypt data/twitch-oauth.gpg) python3 twitch-notify diff --git a/twitch-notify b/twitch-notify new file mode 100644 index 0000000..0eccc2d --- /dev/null +++ b/twitch-notify @@ -0,0 +1,66 @@ +#!/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= {__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']}"])