diff --git a/realdebrid.py b/realdebrid.py index 31333d4..71e5b90 100755 --- a/realdebrid.py +++ b/realdebrid.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -import httpx -import subprocess +import argparse import logging -import typer +import subprocess from pathlib import Path +import httpx + logging.basicConfig( level=logging.DEBUG, format="%(levelname)s %(asctime)s: %(message)s", @@ -15,8 +16,6 @@ logging.getLogger("httpx").setLevel(logging.WARNING) # get api token from https://real-debrid.com/apitoken key = "P4SUGS4VSXLDIRXSOAB46DXMU2HVFJTOWYCACNULX4V7TOZRW2BA" -cli = typer.Typer() - http = httpx.Client( base_url="https://api.real-debrid.com/rest/1.0/", headers={"Authorization": f"Bearer {key}"}, @@ -29,9 +28,7 @@ def get_download_url(url: str) -> str: return res.json()["download"] -def download_with_aria2( - url: str, cwd: Path = Path.cwd(), extra_args: list[str] = None -) -> None: +def download_with_aria2(url: str, cwd: Path = Path.cwd(), extra_args: list[str] = None) -> None: if not extra_args: extra_args = [] args = ["aria2c", "-x", "5", url, *extra_args] @@ -39,33 +36,34 @@ def download_with_aria2( subprocess.run(args, text=True, cwd=str(cwd.resolve())) -@cli.command( - context_settings={ - "ignore_unknown_options": True, - "allow_extra_args": True, - } -) -def main( - ctx: typer.Context, - url: str, - cwd: Path = typer.Option(Path("."), "-o", "--out", "--cwd", writable=True), -): +def parse_args(): + arger = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + arger.add_argument('url', help='URL to unrestrict & download') + arger.add_argument( + '--output-dir', '-o', '--out', dest='cwd', help='Output directory', type=Path, default=Path.cwd() + ) + args, extra = arger.parse_known_args() + return args, extra + + +def main(): + args, aria2c_args = parse_args() try: logging.debug("generating premium link") - download_url = get_download_url(url) + download_url = get_download_url(args.url) except (httpx.HTTPStatusError, KeyError): logging.error("cannot generate premium link") - raise typer.Exit(1) + raise SystemExit(1) logging.info("got url=%s", download_url) logging.debug("passing url to aria2") try: - download_with_aria2(download_url, cwd=cwd, extra_args=ctx.args) + download_with_aria2(download_url, cwd=args.cwd, extra_args=aria2c_args) except KeyboardInterrupt: logging.error("download interrupted. exiting") - raise typer.Exit(2) + raise SystemExit(1) if __name__ == "__main__": - cli() + main()