From 173fcc098f323ea67050574df1cb0223257209bf Mon Sep 17 00:00:00 2001 From: Vitalii Lebedynskyi Date: Mon, 15 Aug 2022 17:07:59 +0300 Subject: [PATCH] pretty good status --- clipper/analyser.py | 10 ++++++++++ clipper/api.py | 12 ++++++++---- clipper/chat.py | 29 ++++++++++++++--------------- clipper/clipper.py | 1 + clipper/recorder.py | 10 +++++----- clipper/video.py | 10 ++++------ 6 files changed, 42 insertions(+), 30 deletions(-) diff --git a/clipper/analyser.py b/clipper/analyser.py index a553aba..9d4da0d 100644 --- a/clipper/analyser.py +++ b/clipper/analyser.py @@ -11,6 +11,10 @@ logger = logging.getLogger(__name__) class ChatAnalyser: + def __init__(self, ignore_commands=True, ignored_users=["moobot", "nightbot"]): + self.ignored_users = ignored_users + self.ignore_commands = ignore_commands + def run(self, chat_file, peaks_output_file, peaks_output_chart): dates = self._read_message_dates(chat_file) messages_per_minute = self._group_dates(dates) @@ -32,6 +36,12 @@ class ChatAnalyser: # Wrong line format continue + if message_data[1].lower() in self.ignore_commands: + continue + + if self.ignore_commands and message_data[2].startswith("!"): + continue + date = message_data[0] dates.append(self._parse_date(date)) return dates diff --git a/clipper/api.py b/clipper/api.py index fd83098..ad2899b 100644 --- a/clipper/api.py +++ b/clipper/api.py @@ -75,15 +75,19 @@ class ChatConnection: self.connection = socket.socket() self.connection.connect((TW_CHAT_SERVER, TW_CHAT_PORT)) # public data to join hat - self.connection.send(f"PASS couldBeRandomString\n".encode("utf-8")) - self.connection.send(f"NICK justinfan113\n".encode("utf-8")) - self.connection.send(f"JOIN {channel}\n".encode("utf-8")) + self.connection.send(f"PASS couldBeRandomString\r\n".encode("utf-8")) + self.connection.send(f"NICK justinfan113\r\n".encode("utf-8")) + self.connection.send(f"JOIN {channel}\r\n".encode("utf-8")) logger.info("Connected to %s", channel) try: while True: - msg = self.connection.recv(8192).decode('utf-8') + msg = self.connection.recv(1024).decode('utf-8') + if "PING :tmi.twitch.tv" in msg: + self.connection.send(bytes("PONG :tmi.twitch.tv\r\n", "UTF-8")) + logger.info("RECEIVED Ping from server. Answered") + continue if self.on_message: self.on_message(msg) except BaseException as e: diff --git a/clipper/chat.py b/clipper/chat.py index af6d743..73ac84d 100644 --- a/clipper/chat.py +++ b/clipper/chat.py @@ -8,7 +8,6 @@ CHAT_DIVIDER = "<~|~>" class TwitchChatRecorder: - is_running = False chat_process = None def __init__(self, api, debug=False): @@ -16,10 +15,23 @@ class TwitchChatRecorder: 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 stop(self): + try: + if self.chat_process: + self.chat_process.terminate() + + self.chat_process = None + logger.info("Chat stopped") + except BaseException as e: + logger.error("Unable to stop chat") + logger.error(e) + + def is_running(self): + return self.chat_process is not None and self.chat_process.is_alive() + def _record_chat(self, streamer_name, output_file): with open(output_file, "w") as stream: def on_message(twitch_msg): @@ -34,19 +46,6 @@ class TwitchChatRecorder: 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] diff --git a/clipper/clipper.py b/clipper/clipper.py index bc99d09..093f90e 100644 --- a/clipper/clipper.py +++ b/clipper/clipper.py @@ -1,2 +1,3 @@ class Clipper: def run(self): + pass diff --git a/clipper/recorder.py b/clipper/recorder.py index 2181411..4e03b6d 100644 --- a/clipper/recorder.py +++ b/clipper/recorder.py @@ -1,6 +1,5 @@ import logging import os -import subprocess import time import sys from datetime import datetime @@ -33,9 +32,9 @@ class Recorder: def run(self): logger.info("Start recording streamer %s", self.config.tw_streamer) - status = self.api.get_user_status(self.config.tw_streamer) while True: + status = self.api.get_user_status(self.config.tw_streamer) if status == TwitchStreamStatus.ONLINE: logger.info("Streamer %s is online. Start recording", self.config.tw_streamer) @@ -59,7 +58,7 @@ class Recorder: time.sleep(300) if status == TwitchStreamStatus.ERROR: - logger.critical("Error occurred. Exit", self.config.tw_streamer) + logger.critical("Error occurred %s. Exit", self.config.tw_streamer) sys.exit(1) elif status == TwitchStreamStatus.NOT_FOUND: @@ -68,8 +67,8 @@ class Recorder: def _loop_recording(self): while True: - if self.video_recorder.is_running or self.chat_recorder.is_running: - if not (self.video_recorder.is_running and self.chat_recorder.is_running): + if self.video_recorder.is_running() or self.chat_recorder.is_running(): + if not (self.video_recorder.is_running() and self.chat_recorder.is_running()): self.video_recorder.stop() self.chat_recorder.stop() break @@ -79,5 +78,6 @@ class Recorder: break def _post_process_video(self, output_chat_file, output_chat_peaks_file, output_chat_chart_file): + logger.info("Start looking for peaks in file %s", output_chat_file) peaks = self.chat_analyser.run(output_chat_file, output_chat_peaks_file, output_chat_chart_file) logger.info("Found peaks: %s for file %s", len(peaks), output_chat_file) diff --git a/clipper/video.py b/clipper/video.py index bea109a..522e113 100644 --- a/clipper/video.py +++ b/clipper/video.py @@ -5,12 +5,10 @@ logger = logging.getLogger(__name__) class TwitchVideoRecorder: - is_running = False refresh_timeout = 15 streamlink_process = None - def run(self, streamer_name, output_file, quality="720p"): - self.is_running = True + def run(self, streamer_name, output_file, quality="360p"): self._record_stream(streamer_name, output_file, quality) def stop(self): @@ -19,12 +17,13 @@ class TwitchVideoRecorder: self.streamlink_process.terminate() self.streamlink_process = None - logger.error("Video stopped") + logger.info("Video stopped") except BaseException as e: logger.error("Unable to stop video") logger.error(e) - self.is_running = False + def is_running(self) -> bool: + return self.streamlink_process is not None and self.streamlink_process.poll() is None def _record_stream(self, streamer_name, output_file, quality): try: @@ -40,4 +39,3 @@ class TwitchVideoRecorder: except BaseException as e: logger.error("Unable to run streamlink") logger.error(e) - self.is_running = False