Skip to content

Instantly share code, notes, and snippets.

@abdusco
Created May 22, 2021 10:02
Show Gist options
  • Save abdusco/00bac425edd5c2f05761754bf35a4b95 to your computer and use it in GitHub Desktop.
Save abdusco/00bac425edd5c2f05761754bf35a4b95 to your computer and use it in GitHub Desktop.
RealDebrid + aria2 downloader script
#!/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()
realdebrid.py 'https://url/to/file' -- -extra -aria2 -args here
@abdusco
Copy link
Author

abdusco commented May 22, 2021

The script needs Python 3.9+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment