Merge pull request #479 from exislow/457-cli-ux-enhancement-assume-dl-command-when-a-link-is-specified-as-the-first-argument
This commit is contained in:
+31
-20
@@ -1,8 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import signal
|
import signal
|
||||||
|
import sys
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import typer
|
import typer
|
||||||
from rich.console import Console, Group
|
from rich.console import Console, Group
|
||||||
@@ -31,13 +33,13 @@ from tidal_dl_ng.helper.wrapper import LoggerWrapped
|
|||||||
from tidal_dl_ng.model.cfg import HelpSettings
|
from tidal_dl_ng.model.cfg import HelpSettings
|
||||||
|
|
||||||
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, add_completion=False)
|
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, add_completion=False)
|
||||||
dl_fav_group = typer.Typer(
|
app_dl_fav = typer.Typer(
|
||||||
context_settings={"help_option_names": ["-h", "--help"]},
|
context_settings={"help_option_names": ["-h", "--help"]},
|
||||||
add_completion=True,
|
add_completion=True,
|
||||||
help="Download from a favorites collection.",
|
help="Download from a favorites collection.",
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_typer(dl_fav_group, name="dl_fav")
|
app.add_typer(app_dl_fav, name="dl_fav")
|
||||||
|
|
||||||
|
|
||||||
def version_callback(value: bool):
|
def version_callback(value: bool):
|
||||||
@@ -52,6 +54,20 @@ def version_callback(value: bool):
|
|||||||
raise typer.Exit()
|
raise typer.Exit()
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback()
|
||||||
|
def callback_app(
|
||||||
|
ctx: typer.Context,
|
||||||
|
version: Annotated[bool | None, typer.Option("--version", "-v", callback=version_callback, is_eager=True)] = None,
|
||||||
|
):
|
||||||
|
"""App callback to initialize context and handle version option.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ctx (typer.Context): Typer context object.
|
||||||
|
version (bool | None, optional): Version flag. Defaults to None.
|
||||||
|
"""
|
||||||
|
ctx.obj = {"tidal": None}
|
||||||
|
|
||||||
|
|
||||||
def _handle_track_or_video(
|
def _handle_track_or_video(
|
||||||
dl: Download, ctx: typer.Context, item: str, media: object, file_template: str, idx: int, urls_pos_last: int
|
dl: Download, ctx: typer.Context, item: str, media: object, file_template: str, idx: int, urls_pos_last: int
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -256,20 +272,6 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@app.callback()
|
|
||||||
def callback_app(
|
|
||||||
ctx: typer.Context,
|
|
||||||
version: Annotated[bool | None, typer.Option("--version", "-v", callback=version_callback, is_eager=True)] = None,
|
|
||||||
):
|
|
||||||
"""App callback to initialize context and handle version option.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
ctx (typer.Context): Typer context object.
|
|
||||||
version (bool | None, optional): Version flag. Defaults to None.
|
|
||||||
"""
|
|
||||||
ctx.obj = {"tidal": None}
|
|
||||||
|
|
||||||
|
|
||||||
@app.command(name="cfg")
|
@app.command(name="cfg")
|
||||||
def settings_management(
|
def settings_management(
|
||||||
names: Annotated[list[str] | None, typer.Argument()] = None,
|
names: Annotated[list[str] | None, typer.Argument()] = None,
|
||||||
@@ -397,7 +399,7 @@ def download(
|
|||||||
return _download(ctx, urls)
|
return _download(ctx, urls)
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@app_dl_fav.command(
|
||||||
name="tracks",
|
name="tracks",
|
||||||
help="Download your favorite track collection.",
|
help="Download your favorite track collection.",
|
||||||
)
|
)
|
||||||
@@ -416,7 +418,7 @@ def download_fav_tracks(ctx: typer.Context) -> bool:
|
|||||||
return _download_fav_factory(ctx, func_name_favorites)
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@app_dl_fav.command(
|
||||||
name="artists",
|
name="artists",
|
||||||
help="Download your favorite artist collection.",
|
help="Download your favorite artist collection.",
|
||||||
)
|
)
|
||||||
@@ -435,7 +437,7 @@ def download_fav_artists(ctx: typer.Context) -> bool:
|
|||||||
return _download_fav_factory(ctx, func_name_favorites)
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@app_dl_fav.command(
|
||||||
name="albums",
|
name="albums",
|
||||||
help="Download your favorite album collection.",
|
help="Download your favorite album collection.",
|
||||||
)
|
)
|
||||||
@@ -454,7 +456,7 @@ def download_fav_albums(ctx: typer.Context) -> bool:
|
|||||||
return _download_fav_factory(ctx, func_name_favorites)
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@app_dl_fav.command(
|
||||||
name="videos",
|
name="videos",
|
||||||
help="Download your favorite video collection.",
|
help="Download your favorite video collection.",
|
||||||
)
|
)
|
||||||
@@ -519,4 +521,13 @@ if __name__ == "__main__":
|
|||||||
signal.signal(signal.SIGINT, handle_sigint_term)
|
signal.signal(signal.SIGINT, handle_sigint_term)
|
||||||
signal.signal(signal.SIGTERM, handle_sigint_term)
|
signal.signal(signal.SIGTERM, handle_sigint_term)
|
||||||
|
|
||||||
|
# Check if the first argument is a URL. Hacky solution, since Typer does not support positional arguments without options / commands.
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
first_arg = sys.argv[1]
|
||||||
|
parsed_url = urlparse(first_arg)
|
||||||
|
|
||||||
|
if parsed_url.scheme in ["http", "https"] and parsed_url.netloc:
|
||||||
|
# Rewrite sys.argv to simulate `dl <URL>`
|
||||||
|
sys.argv.insert(1, "dl")
|
||||||
|
|
||||||
app()
|
app()
|
||||||
|
|||||||
Reference in New Issue
Block a user