From 9fbd04ce62684e11e2c5c1a21002a45e3536c65a Mon Sep 17 00:00:00 2001 From: ungaul Date: Tue, 21 Jan 2025 07:04:00 +0900 Subject: [PATCH 1/2] Initial --- src/onthespot/cli.py | 40 +++++++++++++++++++++++++++++++++++--- src/onthespot/otsconfig.py | 20 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/onthespot/cli.py b/src/onthespot/cli.py index b0452ac..85494cc 100644 --- a/src/onthespot/cli.py +++ b/src/onthespot/cli.py @@ -7,6 +7,7 @@ import threading import time import traceback +import argparse from cmd import Cmd from .accounts import FillAccountPool, get_account_token from .api.apple_music import apple_music_get_track_metadata @@ -24,8 +25,23 @@ from .runtimedata import account_pool, pending, download_queue, download_queue_lock, pending_lock from .search import get_search_results -logging.disable(logging.CRITICAL) +def parse_args(): + parser = argparse.ArgumentParser(description="OnTheSpot CLI Downloader") + + parser.add_argument('--download', help="Rechercher et télécharger automatiquement un élément via une requête") + + args, unknown_args = parser.parse_known_args() + overrides = {} + for arg in unknown_args: + if arg.startswith('--') and '=' in arg: + key, value = arg[2:].split('=', 1) + overrides[key] = value + + return args, overrides + +# logging.disable(logging.CRITICAL) +logger = logging.getLogger("cli") class QueueWorker(threading.Thread): def __init__(self): @@ -66,13 +82,18 @@ def run(self): pending[local_id] = item def main(): + args, cli_overrides = parse_args() + config.apply_overrides(cli_overrides) + + print("Final configuration after overriding:") + for key, value in cli_overrides.items(): + print(f"{key}={value}") + print('\033[32mLogging In...\033[0m\n', end='', flush=True) fill_account_pool = FillAccountPool() - fill_account_pool.finished.connect(lambda: print("Finished filling account pool.")) fill_account_pool.progress.connect(lambda message, status: print(f"{message} {'Success' if status else 'Failed'}")) - fill_account_pool.start() thread = threading.Thread(target=parsingworker) @@ -97,6 +118,19 @@ def main(): mirrorplayback = MirrorSpotifyPlayback() mirrorplayback.start() + if args.download: + print(f"\033[32mSearching and downloading: {args.download}\033[0m") + CLI().onecmd(f"search {args.download}") + + while not any(item['item_status'] in ("Waiting", "Downloading") for item in download_queue.values()): + time.sleep(0.1) + + while any(item['item_status'] not in ("Downloaded", "Failed", "Cancelled") for item in download_queue.values()): + time.sleep(1) + + print("\033[32mDownload finished. Exiting...\033[0m") + os._exit(0) + CLI().cmdloop() diff --git a/src/onthespot/otsconfig.py b/src/onthespot/otsconfig.py index 51cd695..a75923b 100755 --- a/src/onthespot/otsconfig.py +++ b/src/onthespot/otsconfig.py @@ -302,4 +302,24 @@ def rollback(self): cf.write(json.dumps(self.__template_data, indent=4)) self.__config = self.__template_data + def apply_overrides(self, overrides): + for key, value in overrides.items(): + if key in self.__config or key in self.__template_data: + current_value = self.get(key) + if isinstance(current_value, bool): + value = value.lower() in ("true", "1", "yes") + elif isinstance(current_value, int): + value = int(value) + elif isinstance(current_value, float): + value = float(value) + + print(f"Overriding configuration : {key} = {value}") + self.set(key, value) + elif key=="download": + print(f"Direct downloading {value}.") + else: + print(f"Warning: parameter {key} doesn't exist in configuration and will be discarded.") + + self.update() + config = Config() From 0b9ce68b7666030528eb1021c23fdc2474c11bd6 Mon Sep 17 00:00:00 2001 From: ungaul Date: Thu, 23 Jan 2025 06:36:53 +0900 Subject: [PATCH 2/2] Update cli.py --- src/onthespot/cli.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/onthespot/cli.py b/src/onthespot/cli.py index 85494cc..14c3e00 100644 --- a/src/onthespot/cli.py +++ b/src/onthespot/cli.py @@ -38,6 +38,10 @@ def parse_args(): key, value = arg[2:].split('=', 1) overrides[key] = value + if args.download: + if not (args.download.startswith("http://") or args.download.startswith("https://")): + parser.error("Parameter --download only accept URLs.") + return args, overrides # logging.disable(logging.CRITICAL)