Some working architecture for messages

This commit is contained in:
Vitalii Lebedynskyi
2022-08-09 14:30:30 +03:00
parent 86ac82f28d
commit e2ee3d1496
6 changed files with 96 additions and 147 deletions

View File

@@ -1,20 +1,40 @@
import logging
import os
logger = logging.getLogger(__name__)
def parse_msg(msg):
"""Breaks a message from an IRC server into its prefix, command, and arguments.
"""
prefix = ''
trailing = []
if not msg:
raise ValueError("Empty line.")
if msg[0] == ':':
prefix, msg = msg[1:].split(' ', 1)
if msg.find(' :') != -1:
msg, trailing = msg.split(' :', 1)
args = msg.split()
args.append(trailing)
else:
args = msg.split()
command = args.pop(0)
return prefix, command, args
class TwitchChatRecorder:
def __init__(self, api, streamer_name, ignored_prefix=["!"], on_finish=None):
self.ignored_prefix = ignored_prefix
self.on_finish = on_finish
def __init__(self, api, streamer_name, recording_folder):
self.recording_folder = recording_folder
self.streamer_name = streamer_name
self.api = api
def run(self):
self.api.start_chat(self.streamer_name)
def run(self, file_template):
file_name = os.path.join(self.recording_folder, f"{file_template}.txt", )
with open(file_name, "w") as stream:
def on_message(twitch_msg):
prefix, command, args = parse_msg(twitch_msg)
stream.writelines(str(args))
def on_error(self, error):
logger.error(error)
def on_message(self, msg):
logger.info("New message %s", msg)
self.api.start_chat(self.streamer_name, on_message)