finished work with recorders

This commit is contained in:
Vitalii Lebedynskyi
2022-08-14 23:03:22 +03:00
parent 3133f24bdd
commit 0089dc2982
3 changed files with 66 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
import logging
from datetime import datetime
import multiprocessing
logger = logging.getLogger(__name__)
@@ -8,12 +9,18 @@ CHAT_DIVIDER = "<~|~>"
class TwitchChatRecorder:
is_running = False
chat_process = None
def __init__(self, api, debug=False):
self.debug = debug
self.api = api
def run(self, streamer_name, output_file):
self.is_running = True
self.chat_process = multiprocessing.Process(target=self._record_chat, args=(streamer_name, output_file))
self.chat_process.start()
def _record_chat(self, streamer_name, output_file):
with open(output_file, "w") as stream:
def on_message(twitch_msg):
user, msg = self.parse_msg(twitch_msg)
@@ -25,9 +32,21 @@ class TwitchChatRecorder:
if self.debug:
logger.info("Chat: %s", msg_line)
self.is_running = True
self.api.start_chat(streamer_name, on_message)
def stop(self):
try:
if self.chat_process:
self.chat_process.terminate()
self.chat_process = None
logger.error("Chat stopped")
except BaseException as e:
logger.error("Unable to stop chat")
logger.error(e)
self.is_running = False
def parse_msg(self, msg):
try:
return msg[1:].split('!')[0], msg.split(":", 2)[2]