Reorganized chat downloader

This commit is contained in:
Vitalii Lebedynskyi
2022-08-14 22:04:08 +03:00
parent e2ee3d1496
commit 3133f24bdd
5 changed files with 60 additions and 81 deletions

View File

@@ -1,40 +1,35 @@
import logging
import os
from datetime import datetime
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
CHAT_DIVIDER = "<~|~>"
class TwitchChatRecorder:
def __init__(self, api, streamer_name, recording_folder):
self.recording_folder = recording_folder
self.streamer_name = streamer_name
is_running = False
def __init__(self, api, debug=False):
self.debug = debug
self.api = api
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 run(self, streamer_name, output_file):
with open(output_file, "w") as stream:
def on_message(twitch_msg):
prefix, command, args = parse_msg(twitch_msg)
stream.writelines(str(args))
user, msg = self.parse_msg(twitch_msg)
if msg:
msg_line = f"{str(datetime.now())}{CHAT_DIVIDER}{user}{CHAT_DIVIDER}{msg}"
stream.write(msg_line)
stream.flush()
self.api.start_chat(self.streamer_name, on_message)
if self.debug:
logger.info("Chat: %s", msg_line)
self.is_running = True
self.api.start_chat(streamer_name, on_message)
def parse_msg(self, msg):
try:
return msg[1:].split('!')[0], msg.split(":", 2)[2]
except BaseException as e:
return None, None