#!/usr/bin/env python3 import httpx import subprocess import logging import typer from pathlib import Path logging.basicConfig( level=logging.DEBUG, format="%(levelname)s %(asctime)s: %(message)s", datefmt=logging.Formatter.default_time_format, ) 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}"}, ) def get_download_url(url: str) -> str: res = http.post("/unrestrict/link", data={"link": url}) res.raise_for_status() return res.json()["download"] 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] logging.debug("calling aria2 with args=%r", args) 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), ): try: logging.debug("generating premium link") download_url = get_download_url(url) except (httpx.HTTPStatusError, KeyError): logging.error("cannot generate premium link") raise typer.Exit(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) except KeyboardInterrupt: logging.error("download interrupted. exiting") raise typer.Exit(2) if __name__ == "__main__": cli()