From b7ace1d8d0c0ca0c4e29351b832a164cc802f96c Mon Sep 17 00:00:00 2001 From: nathom Date: Mon, 22 Mar 2021 14:53:28 -0700 Subject: [PATCH] Restructure cli --- music_dl/cli.py | 93 ++++++++++++++++++++++++++-------------------- music_dl/config.py | 2 +- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/music_dl/cli.py b/music_dl/cli.py index 24faf73..c0bf1b4 100644 --- a/music_dl/cli.py +++ b/music_dl/cli.py @@ -1,69 +1,63 @@ # For tests import logging +from getpass import getpass import os import click -from qobuz_dl_rewrite.config import Config -from qobuz_dl_rewrite.constants import CACHE_DIR, CONFIG_DIR, CONFIG_PATH -from qobuz_dl_rewrite.core import MusicDL -from qobuz_dl_rewrite.utils import init_log +from .config import Config +from .constants import CACHE_DIR, CONFIG_DIR, CONFIG_PATH +from .core import MusicDL logger = logging.getLogger(__name__) +config = Config(CONFIG_PATH) def _get_config(ctx): + print(f"{ctx.obj=}") if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) if not os.path.isdir(CACHE_DIR): os.makedirs(CONFIG_DIR) - config = Config(ctx.obj.get("config")) + config = Config(CONFIG_PATH) config.update_from_cli(**ctx.obj) return config -# fmt: off @click.group() @click.option("--debug", default=False, is_flag=True, help="Enable debug logging") @click.option("--flush-cache", metavar="PATH", help="Flush the cache before running (only for extreme cases)") +@click.option("-c", '--convert', metavar='CODEC') @click.pass_context -# fmt: on def cli(ctx, **kwargs): """cli. $ rip www.qobuz.com/album/id1089374 convert -c ALAC -sr 48000 + > download and convert to alac, downsample to 48kHz + $ rip config --read + > Config(...) - $ rip config --qobuzpwd MyQobuzPwd123 --qobuzemail person@email.com - $ rip config --tidalpwd MyTidalPwd123 --tidalemail person@email.com - > sets the credentials + $ rip www.qobuz.com/artist/id223049 filter --studio-albums --no-repeats + > download discography with given filters - - :param ctx: - :param kwargs: """ - ctx.ensure_object(dict) - - for key in kwargs.keys(): - ctx.obj[key] = kwargs.get(key) - - if ctx.obj["debug"]: - init_log(path=ctx.obj.get("log_file")) - else: - click.secho("Debug is not enabled", fg="yellow") + print(f"{ctx=}") + print(f"{kwargs=}") @click.command(name="dl") @click.option("-q", "--quality", metavar="INT", help="Quality integer ID (5, 6, 7, 27)") -@click.option("--large-cover", is_flag=True, help="Download large covers (it might fail with embed)") @click.option("-f", "--folder", metavar="PATH", help="Custom download folder") +@click.option("-s", "--search", metavar='QUERY') +@click.option("-nd", "--no-db", is_flag=True) @click.argument("items", nargs=-1) @click.pass_context -def download(ctx, items): +def download(ctx, quality, folder, search, items): """ Download an URL, space separated URLs or a text file with URLs. Mixed arguments are also supported. @@ -84,6 +78,7 @@ def download(ctx, items): * Tidal (album, artist, track, playlist) """ + ctx.ensure_object(dict) config = _get_config(ctx) core = MusicDL(config) for item in items: @@ -100,14 +95,27 @@ def download(ctx, items): ) -@click.command() -@click.argument("--path") -@click.argument("--read") -def config(path, read): - if path: - click.echo(CONFIG_PATH) - if read: - click.echo(repr(config)) +@click.command(name='config') +@click.option('-o', "--open", is_flag=True) +@click.option("-q", '--qobuz', is_flag=True) +@click.option("-t", '--tidal', is_flag=True) +def edit_config(open, qobuz, tidal): + if open: + # open in text editor + click.launch(CONFIG_PATH) + return + + if qobuz: + config['qobuz']['email'] = input("Qobuz email: ") + config['qobuz']['password'] = getpass("Qobuz password: ") + config.save() + click.secho(f"Config saved at {CONFIG_PATH}", fg='green') + + if tidal: + config['tidal']['email'] = input("Tidal email: ") + config['tidal']['password'] = getpass("Tidal password: ") + config.save() + click.secho(f"Config saved at {CONFIG_PATH}", fg='green') @click.command() @@ -115,15 +123,15 @@ def config(path, read): help='Type to search for. Can be album, artist, playlist, track') @click.argument("QUERY") def search(media_type, query): - pass + print(f"searching for {media_type} with {query=}") @click.command() -@click.option("-c", "--codec", default='ALAC') @click.option("-sr", '--sampling-rate') @click.option("-bd", "--bit-depth") -def convert(codec, sampling_rate, bit_depth): - pass +@click.argument("codec") +def convert(sampling_rate, bit_depth, codec): + print(codec, sampling_rate, bit_depth) @click.command() @@ -137,18 +145,23 @@ def interactive(): @click.option("--studio-albums", is_flag=True, help="Ignore non-studio albums") @click.option("--remaster-only", is_flag=True, help="Ignore non-remastered albums") @click.option("--albums-only", is_flag=True, help="Ignore non-album downloads") -def filter(): - pass +def filter(*args): + print(f"filter {args=}") @click.command() @click.option("--default-comment", metavar="COMMENT", help="Custom comment tag for audio files") -def tags(): - pass +@click.option("--no-cover", help='Do not embed cover into audio file.') +def tags(default_comment, no_cover): + print(f"{default_comment=}, {no_cover=}") def main(): cli.add_command(download) + cli.add_command(filter) + cli.add_command(tags) + cli.add_command(edit_config) + cli.add_command(convert) cli(obj={}) diff --git a/music_dl/config.py b/music_dl/config.py index e5ae086..d28ec1e 100644 --- a/music_dl/config.py +++ b/music_dl/config.py @@ -98,7 +98,7 @@ class Config: def update_from_cli(self, **kwargs): for category in (self.downloads, self.metadata, self.filters): for key in category.keys(): - if kwargs[key] is None: + if kwargs.get(key) is None: continue # For debugging's sake