From 61a525ff944cbe3337d891b83ac4fdae2397d3da Mon Sep 17 00:00:00 2001 From: Benexl Date: Sun, 23 Feb 2025 20:18:23 +0300 Subject: [PATCH 1/4] feat(config): pass custom mpv args --- fastanime/cli/config.py | 22 ++++++++++++++++++++-- fastanime/cli/utils/mpv.py | 4 ++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fastanime/cli/config.py b/fastanime/cli/config.py index 9e5aeb1..144a5c1 100644 --- a/fastanime/cli/config.py +++ b/fastanime/cli/config.py @@ -1,9 +1,13 @@ import json import logging import os +import sys +import time from configparser import ConfigParser from typing import TYPE_CHECKING +from rich import print + from ..constants import ( ASSETS_DIR, S_PLATFORM, @@ -60,6 +64,7 @@ class Config(object): "normalize_titles": "True", "notification_duration": "120", "max_cache_lifetime": "03:00:00", + "mpv_args": "", "per_page": "15", "player": "mpv", "preferred_history": "local", @@ -96,8 +101,16 @@ class Config(object): self.configparser.add_section("anilist") # --- set config values from file or using defaults --- - if os.path.exists(USER_CONFIG_PATH) and not no_config: - self.configparser.read(USER_CONFIG_PATH, encoding="utf-8") + try: + if os.path.exists(USER_CONFIG_PATH) and not no_config: + self.configparser.read(USER_CONFIG_PATH, encoding="utf-8") + except Exception as e: + print( + "[yellow]Warning[/]: Failed to read config file using default configuration", + file=sys.stderr, + ) + logger.error(f"Failed to read config file: {e}") + time.sleep(5) # get the configuration self.auto_next = self.configparser.getboolean("stream", "auto_next") @@ -149,6 +162,7 @@ class Config(object): + max_cache_lifetime[1] * 3600 + max_cache_lifetime[2] * 60 ) + self.mpv_args = self.configparser.get("general", "mpv_args") self.per_page = self.configparser.get("anilist", "per_page") self.player = self.configparser.get("stream", "player") self.preferred_history = self.configparser.get("stream", "preferred_history") @@ -448,6 +462,10 @@ recent = {self.recent} # https://discord.com/oauth2/authorize?client_id=1292070065583165512 discord = {self.discord} +# comma separated list of args that will be passed to mpv +# example: --vo=kitty,--fullscreen,--volume=50 +mpv_args = {self.mpv_args} + [stream] # the quality of the stream [1080,720,480,360] diff --git a/fastanime/cli/utils/mpv.py b/fastanime/cli/utils/mpv.py index 25cd253..0fd2014 100644 --- a/fastanime/cli/utils/mpv.py +++ b/fastanime/cli/utils/mpv.py @@ -208,5 +208,9 @@ def run_mpv( mpv_args.append(f"--title={title}") if ytdl_format: mpv_args.append(f"--ytdl-format={ytdl_format}") + + if user_args := os.environ.get("FASTANIME_MPV_ARGS"): + mpv_args.extend(user_args.split(",")) + stop_time, total_time = stream_video(MPV, link, mpv_args, custom_args) return stop_time, total_time From f40dd2363a9d4df40dec65872a1cc8b7760edfdd Mon Sep 17 00:00:00 2001 From: Benexl Date: Sun, 23 Feb 2025 20:24:26 +0300 Subject: [PATCH 2/4] feat(updater): add instructions for post update --- fastanime/cli/app_updater.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fastanime/cli/app_updater.py b/fastanime/cli/app_updater.py index e9962f8..c5def9a 100644 --- a/fastanime/cli/app_updater.py +++ b/fastanime/cli/app_updater.py @@ -133,10 +133,14 @@ def update_app(force=False): if sys.prefix == sys.base_prefix: # ensure NOT in a venv, where --user flag can cause an error. # TODO: Get value of 'include-system-site-packages' in pyenv.cfg. - args.append('--user') + args.append("--user") process = subprocess.run(args) if process.returncode == 0: + print( + "[green]Its recommended to run the following after updating:\n\tfastanime config --update (to get the latest config docs)\n\tfastanime cache --clean (to get rid of any potential issues)[/]", + file=sys.stderr, + ) return True, release_json else: return False, release_json From e6b9df25dd68921705ed85db1f0ff365d4a34757 Mon Sep 17 00:00:00 2001 From: Benexl Date: Sun, 23 Feb 2025 20:28:41 +0300 Subject: [PATCH 3/4] feat: ensure the environs externally provided by user are preferred --- fastanime/cli/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fastanime/cli/config.py b/fastanime/cli/config.py index 144a5c1..5d37ed5 100644 --- a/fastanime/cli/config.py +++ b/fastanime/cli/config.py @@ -212,7 +212,10 @@ class Config(object): def set_fastanime_config_environs(self): current_config = [] for key in self.default_config: - current_config.append((f"FASTANIME_{key.upper()}", str(getattr(self, key)))) + if not os.environ.get(f"FASTANIME_{key.upper()}"): + current_config.append( + (f"FASTANIME_{key.upper()}", str(getattr(self, key))) + ) os.environ.update(current_config) def update_user(self, user): From 6f69b785d86227f0598f0adc2ae97ee0d420f6c2 Mon Sep 17 00:00:00 2001 From: Benexl Date: Sun, 23 Feb 2025 20:52:52 +0300 Subject: [PATCH 4/4] feat(config): mpv pre args --- fastanime/cli/config.py | 6 ++++++ fastanime/cli/utils/mpv.py | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/fastanime/cli/config.py b/fastanime/cli/config.py index 5d37ed5..f074d03 100644 --- a/fastanime/cli/config.py +++ b/fastanime/cli/config.py @@ -65,6 +65,7 @@ class Config(object): "notification_duration": "120", "max_cache_lifetime": "03:00:00", "mpv_args": "", + "mpv_pre_args": "", "per_page": "15", "player": "mpv", "preferred_history": "local", @@ -163,6 +164,7 @@ class Config(object): + max_cache_lifetime[2] * 60 ) self.mpv_args = self.configparser.get("general", "mpv_args") + self.mpv_pre_args = self.configparser.get("general", "mpv_pre_args") self.per_page = self.configparser.get("anilist", "per_page") self.player = self.configparser.get("stream", "player") self.preferred_history = self.configparser.get("stream", "preferred_history") @@ -469,6 +471,10 @@ discord = {self.discord} # example: --vo=kitty,--fullscreen,--volume=50 mpv_args = {self.mpv_args} +# command line options passed before the mpv command +# example: kitty +# useful incase of wanting to run sth like: kitty mpv --vo=kitty +mpv_pre_args = {self.mpv_pre_args} [stream] # the quality of the stream [1080,720,480,360] diff --git a/fastanime/cli/utils/mpv.py b/fastanime/cli/utils/mpv.py index 0fd2014..7da4b41 100644 --- a/fastanime/cli/utils/mpv.py +++ b/fastanime/cli/utils/mpv.py @@ -12,12 +12,13 @@ logger = logging.getLogger(__name__) mpv_av_time_pattern = re.compile(r"AV: ([0-9:]*) / ([0-9:]*) \(([0-9]*)%\)") -def stream_video(MPV, url, mpv_args, custom_args): +def stream_video(MPV, url, mpv_args, custom_args, pre_args=[]): last_time = "0" total_time = "0" if os.environ.get("FASTANIME_DISABLE_MPV_POPEN", "False") == "False": process = subprocess.Popen( - [ + pre_args + + [ MPV, url, *mpv_args, @@ -59,7 +60,7 @@ def stream_video(MPV, url, mpv_args, custom_args): process.wait() else: proc = subprocess.run( - [MPV, url, *mpv_args, *custom_args], + pre_args + [MPV, url, *mpv_args, *custom_args], capture_output=True, text=True, encoding="utf-8", @@ -212,5 +213,10 @@ def run_mpv( if user_args := os.environ.get("FASTANIME_MPV_ARGS"): mpv_args.extend(user_args.split(",")) - stop_time, total_time = stream_video(MPV, link, mpv_args, custom_args) + pre_args = [] + if user_args := os.environ.get("FASTANIME_MPV_PRE_ARGS"): + pre_args = user_args.split(",") + stop_time, total_time = stream_video( + MPV, link, mpv_args, custom_args, pre_args + ) return stop_time, total_time