From d3a222e27b5099de2f7fe3bf1ebbd077715740c4 Mon Sep 17 00:00:00 2001 From: Melon Bread Date: Wed, 10 Apr 2024 16:44:57 -0400 Subject: [PATCH] Better way to handle logging --- README.md | 3 +++ config.ini.example | 1 + main.py | 22 ++++++++-------------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0be5bdc..8806e8a 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ If left blank the default location is: `$HOME/Downloads/Streams/` installed the [ttvlol](https://github.com/2bc4/streamlink-ttvlol) `streamlink` plugin. Setting this to `True` without doing so will cause the `streamlink` subprocess to fail. Default value is `False` +- `log` : Takes a `True` or `False` value. If `True` there will be a `log.txt` file +in the channel's download directory with all of the output that came from its +corresponding subprocess. Default value is `False` ### Adding streams diff --git a/config.ini.example b/config.ini.example index bc7e43a..29def97 100644 --- a/config.ini.example +++ b/config.ini.example @@ -2,6 +2,7 @@ streamlink_location = "" download_location = "" skip_ads = +log = [streams] diff --git a/main.py b/main.py index faa87cd..3819cd4 100755 --- a/main.py +++ b/main.py @@ -18,6 +18,7 @@ downloading = {} streamlink_location: str = "streamlink" download_location: str = f"{Path.home()}/Downloads/Streams" skip_ads: bool = False +log: bool = False def load_config() -> None: @@ -39,6 +40,9 @@ def load_config() -> None: if config.has_option("settings", "skip_ads") and not config["settings"]["skip_ads"]: skip_ads = bool(config["settings"]["skip_ads"]) print(f"Skip ads: {skip_ads}") + if config.has_option("settings", "log") and not config["settings"]["log"]: + log = bool(config["settings"]["log"]) + print(f"Logs: {log}") if len(config["streams"]) < 1: sys.exit("ERROR: No streams found in config.ini! See README.md for more info.") @@ -49,15 +53,6 @@ def load_config() -> None: print("Config file loaded") -# TODO: Log better and properly -def write_log(channel: str) -> None: - """Writes the latest stdout of a process to log.txt""" - with open("log.txt", "a") as log: - line = downloading[channel].stdout.readline() - if line: - log.write(line.decode()) - - # TODO: Have steamlink itself check if the channel is live def is_live(channel: str) -> bool: """Checks if a channel is live on Twitch""" @@ -81,11 +76,13 @@ def download_stream(channel: str) -> None: """Downloads a given channel name in its own subprocess""" # TODO: Just clean this up at somepoint addtional_parms: str = "" + if log: + addtional_parms += f" --logfile {download_location}/{channel}/log.txt" if skip_ads: - addtional_parms = "--twitch-proxy-playlist=https://lb-eu.cdn-perfprod.com,https://lb-eu2.cdn-perfprod.com,https://lb-na.cdn-perfprod.com,https://lb-as.cdn-perfprod.com,https://as.luminous.dev --twitch-disable-ads" + addtional_parms += " --twitch-proxy-playlist=https://lb-eu.cdn-perfprod.com,https://lb-eu2.cdn-perfprod.com,https://lb-na.cdn-perfprod.com,https://lb-as.cdn-perfprod.com,https://as.luminous.dev --twitch-disable-ads" file_name: str = f"{channel}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.ts" cmd: str = ( - f"{streamlink_location} --loglevel none --retry-max 10 {addtional_parms} -o {download_location}/{channel}/{file_name} twitch.tv/{channel} best" + f"{streamlink_location} --retry-max 10{addtional_parms} -o {download_location}/{channel}/{file_name} twitch.tv/{channel} best" ) downloading[channel] = subprocess.Popen( [cmd], @@ -138,9 +135,7 @@ def main() -> None: if channel not in downloading: download_stream(channel) else: - # TODO: Format this and dont make its own line print("\033[33m(Already Downloading)\033[0m") - # write_log(channel) else: print( "\n\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.", @@ -148,7 +143,6 @@ def main() -> None: ) if channel in downloading: del downloading[channel] - # TODO: Format this and dont make its own line print("\033[35m(Stopping)\033[0m") time.sleep(1) # Wait one second before going to next channel print(