streamrip/music_dl/cli.py

170 lines
4.7 KiB
Python
Raw Normal View History

2021-03-22 12:21:27 -04:00
# For tests
import logging
2021-03-22 17:53:28 -04:00
from getpass import getpass
2021-03-22 12:21:27 -04:00
import os
import click
2021-03-22 17:53:28 -04:00
from .config import Config
from .constants import CACHE_DIR, CONFIG_DIR, CONFIG_PATH
from .core import MusicDL
2021-03-22 12:21:27 -04:00
logger = logging.getLogger(__name__)
2021-03-22 17:53:28 -04:00
config = Config(CONFIG_PATH)
2021-03-22 12:21:27 -04:00
def _get_config(ctx):
2021-03-22 17:53:28 -04:00
print(f"{ctx.obj=}")
2021-03-22 12:21:27 -04:00
if not os.path.isdir(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
if not os.path.isdir(CACHE_DIR):
os.makedirs(CONFIG_DIR)
2021-03-22 17:53:28 -04:00
config = Config(CONFIG_PATH)
2021-03-22 12:21:27 -04:00
config.update_from_cli(**ctx.obj)
return config
@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)")
2021-03-22 17:53:28 -04:00
@click.option("-c", '--convert', metavar='CODEC')
2021-03-22 12:21:27 -04:00
@click.pass_context
def cli(ctx, **kwargs):
2021-03-22 16:40:29 -04:00
"""cli.
$ rip www.qobuz.com/album/id1089374 convert -c ALAC -sr 48000
2021-03-22 17:53:28 -04:00
2021-03-22 16:40:29 -04:00
> download and convert to alac, downsample to 48kHz
2021-03-22 17:53:28 -04:00
2021-03-22 16:40:29 -04:00
$ rip config --read
2021-03-22 17:53:28 -04:00
2021-03-22 16:40:29 -04:00
> Config(...)
2021-03-22 17:53:28 -04:00
2021-03-22 16:40:29 -04:00
$ rip www.qobuz.com/artist/id223049 filter --studio-albums --no-repeats
2021-03-22 17:53:28 -04:00
> download discography with given filters
2021-03-22 16:40:29 -04:00
"""
2021-03-22 17:53:28 -04:00
print(f"{ctx=}")
print(f"{kwargs=}")
2021-03-22 12:21:27 -04:00
@click.command(name="dl")
2021-03-22 16:40:29 -04:00
@click.option("-q", "--quality", metavar="INT", help="Quality integer ID (5, 6, 7, 27)")
@click.option("-f", "--folder", metavar="PATH", help="Custom download folder")
2021-03-22 17:53:28 -04:00
@click.option("-s", "--search", metavar='QUERY')
@click.option("-nd", "--no-db", is_flag=True)
2021-03-22 12:21:27 -04:00
@click.argument("items", nargs=-1)
@click.pass_context
2021-03-22 17:53:28 -04:00
def download(ctx, quality, folder, search, items):
2021-03-22 12:21:27 -04:00
"""
Download an URL, space separated URLs or a text file with URLs.
Mixed arguments are also supported.
Examples:
* `qobuz-dl dl https://some.url/some_type/some_id`
* `qobuz-dl dl file_with_urls.txt`
* `qobuz-dl dl URL URL URL`
Supported sources and their types:
* Deezer (album, artist, track, playlist)
* Qobuz (album, artist, label, track, playlist)
* Tidal (album, artist, track, playlist)
"""
2021-03-22 17:53:28 -04:00
ctx.ensure_object(dict)
2021-03-22 12:21:27 -04:00
config = _get_config(ctx)
2021-03-22 14:17:01 -04:00
core = MusicDL(config)
2021-03-22 12:21:27 -04:00
for item in items:
try:
if os.path.isfile(item):
core.from_txt(item)
click.secho(f"File input found: {item}", fg="yellow")
else:
core.handle_url(item)
except Exception as error:
logger.error(error, exc_info=True)
click.secho(
f"{type(error).__name__} raised processing {item}: {error}", fg="red"
)
2021-03-22 17:53:28 -04:00
@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')
2021-03-22 16:40:29 -04:00
@click.command()
@click.option("-t", '--type', default='album',
help='Type to search for. Can be album, artist, playlist, track')
@click.argument("QUERY")
def search(media_type, query):
2021-03-22 17:53:28 -04:00
print(f"searching for {media_type} with {query=}")
2021-03-22 16:40:29 -04:00
@click.command()
@click.option("-sr", '--sampling-rate')
@click.option("-bd", "--bit-depth")
2021-03-22 17:53:28 -04:00
@click.argument("codec")
def convert(sampling_rate, bit_depth, codec):
print(codec, sampling_rate, bit_depth)
2021-03-22 16:40:29 -04:00
@click.command()
def interactive():
pass
@click.command()
@click.option("--no-extras", is_flag=True, help="Ignore extras")
@click.option("--no-features", is_flag=True, help="Ignore features")
@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")
2021-03-22 17:53:28 -04:00
def filter(*args):
print(f"filter {args=}")
2021-03-22 16:40:29 -04:00
@click.command()
@click.option("--default-comment", metavar="COMMENT", help="Custom comment tag for audio files")
2021-03-22 17:53:28 -04:00
@click.option("--no-cover", help='Do not embed cover into audio file.')
def tags(default_comment, no_cover):
print(f"{default_comment=}, {no_cover=}")
2021-03-22 16:40:29 -04:00
2021-03-22 12:21:27 -04:00
def main():
cli.add_command(download)
2021-03-22 17:53:28 -04:00
cli.add_command(filter)
cli.add_command(tags)
cli.add_command(edit_config)
cli.add_command(convert)
2021-03-22 12:21:27 -04:00
cli(obj={})
if __name__ == "__main__":
main()