Created
May 22, 2021 10:02
-
-
Save abdusco/00bac425edd5c2f05761754bf35a4b95 to your computer and use it in GitHub Desktop.
RealDebrid + aria2 downloader script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = "ASDASDASDADASDASDASDASDASD" | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
realdebrid.py 'https://url/to/file' -- -extra -aria2 -args here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script needs Python 3.9+