Files
playground/realdebrid.py
T
2024-12-24 20:49:41 +03:00

85 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import logging
import subprocess
from pathlib import Path
import httpx
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"
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()))
def download_torrent(torrent_path: Path):
res = http.put('/torrents/addTorrent', content=torrent_path.read_bytes())
res.raise_for_status()
data = res.json()
res = http.get(f'/torrents/info/{data["id"]}')
res.raise_for_status()
data = res.json()
print()
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(args.url)
except (httpx.HTTPStatusError, KeyError):
logging.error("cannot generate premium link")
raise SystemExit(1)
logging.info("got url=%s", download_url)
logging.debug("passing url to aria2")
try:
download_with_aria2(download_url, cwd=args.cwd, extra_args=aria2c_args)
except KeyboardInterrupt:
logging.error("download interrupted. exiting")
raise SystemExit(1)
if __name__ == "__main__":
download_torrent(
Path(
'/Users/abdus/Downloads/[Empornium][PornWorld] All Her Holes Pounded Hard [Veronica Leal] [1080p] {Se7enSeas} [x265].torrent'
)
)