Skip to content

Commit

Permalink
Merge pull request #105 from ungaul/cli_args
Browse files Browse the repository at this point in the history
Cli args
  • Loading branch information
ungaul authored Jan 22, 2025
2 parents 0c9dd6f + 0b9ce68 commit 4078583
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/onthespot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,8 +25,27 @@
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

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)
logger = logging.getLogger("cli")

class QueueWorker(threading.Thread):
def __init__(self):
Expand Down Expand Up @@ -66,13 +86,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)
Expand All @@ -97,6 +122,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()


Expand Down
20 changes: 20 additions & 0 deletions src/onthespot/otsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,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()

0 comments on commit 4078583

Please sign in to comment.