Skip to content

Bulk asynchronous downloader for multiple file hosts

add real debrid api + manager

NTFSvolume5aa3d820cb67
diff --git a/cyberdrop_dl/managers/manager.py b/cyberdrop_dl/managers/manager.py
index b425eee6e..a246dc569 100644
--- a/cyberdrop_dl/managers/manager.py
+++ b/cyberdrop_dl/managers/manager.py
@@ -11,6 +11,7 @@ from cyberdrop_dl.managers.config_manager import ConfigManager
from cyberdrop_dl.managers.console_manager import ConsoleManager
from cyberdrop_dl.managers.db_manager import DBManager
from cyberdrop_dl.managers.download_manager import DownloadManager
+from cyberdrop_dl.managers.realdebrid_manager import RealDebridManager
from cyberdrop_dl.managers.hash_manager import HashManager
from cyberdrop_dl.managers.live_manager import LiveManager
from cyberdrop_dl.managers.log_manager import LogManager
@@ -29,6 +30,7 @@ class Manager:
self.path_manager: PathManager = field(init=False)
self.config_manager: ConfigManager = field(init=False)
self.hash_manager: HashManager = field(init=False)
+ self.real_debrid_manager: RealDebridManager = field(init=False)
self.log_manager: LogManager = field(init=False)
self.db_manager: DBManager = field(init=False)
diff --git a/cyberdrop_dl/managers/real_debrid/__init__.py b/cyberdrop_dl/managers/real_debrid/__init__.py
new file mode 100755
index 000000000..e69de29bb
diff --git a/cyberdrop_dl/managers/real_debrid/api.py b/cyberdrop_dl/managers/real_debrid/api.py
new file mode 100755
index 000000000..d0acfa904
--- /dev/null
+++ b/cyberdrop_dl/managers/real_debrid/api.py
@@ -0,0 +1,358 @@
+from requests import Session
+from pathlib import Path
+from yarl import URL
+from requests.exceptions import RequestException
+from cyberdrop_dl.managers.real_debrid.errors import RealDebridError
+from datetime import date, datetime, timedelta
+import time
+from contextlib import contextmanager
+
+from typing import TYPE_CHECKING, Optional
+
+if TYPE_CHECKING:
+ from requests import Response
+
+MAGNET_PREFIX = 'magnet:?xt=urn:btih:'
+DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
+DATE_ISO_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
+DATE_JSON_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
+
+class RealDebridApi:
+ """
+ Real-Debrid API Implementation. All methods return their JSON response (if any)
+
+ For details, visit: https://api.real-debrid.com
+
+ Unless specified otherwise, all API methods require authentication.
+
+ The API is limited to 250 requests per minute. Use `rate_limiter` content manager to auto limit the requests being made
+
+ Dates are formatted according to the Javascript method `date.toJSON`.
+ Use `convert_special_types` to convert response values to `datetime.date`, `datetime.datetime`, `datetime.timedelta` and `yarl.URL` when applicable
+
+ """
+ API_ENTRYPOINT = URL('https://api.real-debrid.com/rest/1.0')
+ API_OAUTH_ENTRYPOINT = URL("https://api.real-debrid.com/oauth/v2/")
+ RATE_LIMIT = 250 # per minute
+
+ def __init__(self, api_token: Optional[str] = None, convert_special_types: bool= False):
+ self._session = Session()
+ self._last_request_time = 0
+ self._convert_special_types = convert_special_types
+ self.auth = OAuth(self)
+ self.system = System(self)
+ self.user = User(self)
+ self.unrestrict = Unrestrict(self)
+ self.traffic = Traffic(self)
+ self.streaming = Streaming(self)
+ self.downloads = Downloads(self)
+ self.torrents = Torrents(self)
+ self.hosts = Hosts(self)
+ self.settings = Settings(self)
+ self.update_token(api_token)
+
+ def get(self, path: str, **query_params):
+ response = self._session.get(self.API_ENTRYPOINT / path , params= query_params)
+ return self.handle_response(response)
+
+ def post(self, path: str, **data):
+ response = self._session.post(self.API_ENTRYPOINT / path, data=data)
+ return self.handle_response(response)
+
+ def put(self, path:str, filepath: Path, **query_params):
+ with open(filepath, 'rb') as file:
+ response = self._session.put(self.API_ENTRYPOINT / path, data=file, params=query_params)
+ return self.handle_response(response, path)
+
+ def delete(self, path: str):
+ request = self._session.delete(self.API_ENTRYPOINT / path)
+ return self.handle_response(request)
+
+ def update_token(self, new_token: str) -> None:
+ self._api_token = new_token
+ self._session.headers.update({'Authorization': f"Bearer {self._api_token}"})
+
+ def handle_response(self, response: Response) -> dict | str | None:
+ try:
+ response.raise_for_status()
+ JSONResp: dict = response.json()
+ return JSONResp
+ except RequestException:
+ raise RealDebridError(response)
+ except AttributeError:
+ return response.text
+
+ @contextmanager
+ def rate_limiter(self, buffer: float = 0.2):
+ """Context manager to rate limit API requests
+
+ Buffer is % of RATE_LIMIT"""
+ actual_rate_limit = self.RATE_LIMIT * ( 1 - buffer)
+ elapsed = time.time() - self._last_request_time
+ wait_time = max(0, (1 / actual_rate_limit ) - elapsed)
+ time.sleep(wait_time)
+ self._last_request_time = time.time()
+ yield
+
+
+class OAuth:
+ '''RealDebrid authentication via OAuth API'''
+
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+ self.api.API_ENTRYPOINT = self.api.API_OAUTH_ENTRYPOINT
+ self.grant_type = 'http://oauth.net/grant_type/device/1.0'
+
+ def get_devide_code(self, client_id: str, new_credentials:bool=False) -> dict:
+ """Get authentication data"""
+ JSONResp = self.api.get('/device/code', client_id= client_id, new_credentials=new_credentials)
+ if self.api._convert_special_types:
+ JSONResp['expires_in'] = timedelta(seconds=JSONResp['expires_in'])
+ JSONResp['verification_url'] = URL(JSONResp['verification_url'])
+ return JSONResp
+
+ def get_credentials(self, client_id: str, device_code: str):
+ """Verify authentication data and get credentials"""
+ return self.api.get('/device/credentials', client_id = client_id, code=device_code)
+
+ def get_token(self, client_id: str, client_secret: str, device_code: str):
+ """Get token from credentials"""
+ JSONResp = self.api.post('/token', client_id= client_id, client_secret=client_secret, code=device_code, grant_type = self.grant_type)
+ if self.api._convert_special_types:
+ JSONResp['expires_in'] = timedelta(seconds=JSONResp['expires_in'])
+ self.api.update_token(JSONResp['access_token'])
+ return JSONResp
+
+ def get_new_token(self, client_id: str, client_secret: str, refresh_token: str):
+ """Get a new token"""
+ return self.get_token(client_id= client_id, client_secret=client_secret, device_code=refresh_token)
+
+ def refresh_token(self, client_id: str, client_secret: str, refresh_token: str):
+ """Calls `get_new_token`"""
+ return self.get_new_token(client_id= client_id, client_secret=client_secret, refresh_token=refresh_token)
+
+class System:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def disable_token(self) -> None:
+ '''Disable current access token, returns 204 HTTP code'''
+ return self.api.get('/disable_access_token')
+
+ def time(self):
+ '''Get server time. This request does not require authentication'''
+ date_str = self.api.get('/time')
+ if self.api._convert_special_types:
+ return datetime.strptime(date_str, DATE_FORMAT)
+ return date_str
+
+ def iso_time(self):
+ '''Get server time as ISO (with timezone). This request does not require authentication'''
+ date_str = self.api.get('/time/iso')
+ if self.api._convert_special_types:
+ return datetime.strptime(date_str, DATE_ISO_FORMAT)
+ return date_str
+
+class User:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def get(self) -> dict:
+ '''Returns information about the current user'''
+ JSONResp = self.api.get('/user')
+ if self.api._convert_special_types:
+ JSONResp['avatar'] = URL(JSONResp['avatar'])
+ JSONResp['premium'] = timedelta(seconds=JSONResp['avatar'])
+ JSONResp['avatar'] = URL(JSONResp['avatar'])
+ JSONResp['expiration'] = datetime.strptime(JSONResp['expiration'], "%Y-%m-%dT%H:%M:%S.%fZ")
+ return JSONResp
+
+class Unrestrict:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def check(self, link: URL, password: Optional[str] =None):
+ """Check if a file is downloadable on the concerned hoster. This request does not require authentication"""
+ return self.api.post('/unrestrict/check', link=link, password=password)
+
+ def link(self, link: URL, password: Optional[str] = None, remote: bool=None) -> dict:
+ """Unrestrict a hoster link and get a new unrestricted link"""
+ return self.api.post('/unrestrict/link', link=link, password=password, remote=remote)
+
+ def folder(self, link: URL) -> list:
+ """Unrestrict a hoster folder link and get individual links, returns an empty array if no links found"""
+ return self.api.post('/unrestrict/folder', link=link)
+
+ def container_file(self, filepath: Path) -> dict:
+ """Decrypt a container file (RSDF, CCF, CCF3, DLC)"""
+ return self.api.put('/unrestrict/containerFile', filepath=filepath)
+
+ def container_link(self, link: URL) -> dict:
+ """Decrypt a container file from a link"""
+ return self.api.post('/unrestrict/containerLink', link=link)
+
+class Traffic:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def get(self):
+ """Get traffic informations for limited hosters (limits, current usage, extra packages)"""
+ return self.api.get('/traffic')
+
+ def details(self, start: date = None, end: date=None):
+ """Get traffic details on each hoster used during a defined period"""
+ JSONResp = self.api.get('/traffic/details', start=start, end=end)
+ if self.api._convert_special_types:
+ JSONResp = {datetime.strptime(key, "%Y-%m-%d").date(): value
+ for key, value in JSONResp.items()}
+ return JSONResp
+
+class Streaming:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def transcode(self, id: str):
+ """Get transcoding links for given file, {id} from /downloads or /unrestrict/link"""
+ return self.api.get(f"/streaming/transcode/{id}")
+
+ def media_info(self, id: str):
+ """Get detailed media information for the given file id, {id} from /downloads or /unrestrict/link"""
+ return self.api.get(f"/streaming/mediaInfos/{id}")
+
+class Downloads:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def get(self, offset: int =None, page: int=None, limit:int=None ):
+ """Get user downloads list"""
+ JSONResp = self.api.get('/downloads', offset=offset, page=page, limit=limit)
+ if self.api._convert_special_types:
+ for download in JSONResp:
+ download['generated'] = datetime.strptime(download['generated'], DATE_JSON_FORMAT)
+ return JSONResp
+
+ def delete(self, id: str):
+ """Delete a link from downloads list, returns 204 HTTP code"""
+ return self.api.delete(f'/downloads/delete/{id}')
+
+class Torrents:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+ self.POSIBLE_STATUS = ['magnet_error', 'magnet_conversion', 'waiting_files_selection', 'queued', 'downloading', 'downloaded', 'error', 'virus', 'compressing', 'uploading', 'dead']
+
+ def get(self, offset:int=None, page:int=None, limit:int=None, filter: str=None ):
+ """Get user torrents list"""
+ JSONResp: list[dict] = self.api.get('/torrents', offset=offset, page=page, limit=limit, filter=filter)
+ if self.api._convert_special_types:
+ for torrent in JSONResp:
+ torrent['added'] = datetime.strptime(torrent['added'], DATE_JSON_FORMAT)
+ if torrent.get('ended'):
+ torrent['ended'] = datetime.strptime(torrent['ended'], DATE_JSON_FORMAT)
+
+ return JSONResp
+
+ def info(self, id:str):
+ """Get all informations on the asked torrent"""
+ JSONResp: list[dict] = self.api.get(f'/torrents/info/{id}')
+ if self.api._convert_special_types:
+ for torrent in JSONResp:
+ torrent['added'] = datetime.strptime(torrent['added'], DATE_JSON_FORMAT)
+ if torrent.get('ended'):
+ torrent['ended'] = datetime.strptime(torrent['ended'], DATE_JSON_FORMAT)
+
+ return JSONResp
+
+ def instant_availability(self, *hash: str):
+ """
+ Get list of instantly available file IDs by hoster, {hash} is the SHA1 of the torrent.
+ You can test multiple hashes adding multiple /{hash} at the end of the request"""
+ return self.api.get('/torrents/instantAvailability/' + "/".join(*hash))
+
+ def active_count(self):
+ """Get currently active torrents number and the current maximum limit"""
+ return self.api.get('/torrents/activeCount')
+
+ def available_hosts(self):
+ """Get available hosts to upload the torrent to"""
+ return self.api.get('/torrents/availableHosts')
+
+ def add_file(self, filepath:Path, host:Optional[str]=None):
+ """Add a torrent file to download, return a 201 HTTP code"""
+ return self.api.put('/torrents/addTorrent', filepath=filepath, host=host)
+
+ def add_magnet(self, magnet:str, host:Optional[str]=None):
+ """Add a magnet link to download, returns a 201 HTTP code"""
+ if MAGNET_PREFIX not in magnet:
+ magnet = f"{MAGNET_PREFIX}{magnet}"
+ return self.api.post('/torrents/addMagnet', magnet=magnet, host=host)
+
+ def select_files(self, id:str, *files:str):
+ """Select files of a torrent to start it, returns 204 HTTP code
+
+ files = Selected files IDs (comma separated) or 'all'"""
+ return self.api.post(f'/torrents/selectFiles/{id}', files=','.join(files))
+
+ def delete(self, id:str):
+ """Delete a torrent from torrents list, returns 204 HTTP code"""
+ return self.api.delete(f'/torrents/delete/{id}')
+
+class Hosts:
+ def __init__(self, api:RealDebridApi):
+ self.api = api
+
+ def get(self):
+ """Get supported hosts. This request does not require authentication"""
+ return self.api.get('/hosts')
+
+ def status(self):
+ """Get status of supported hosters or not and their status on competitors"""
+ JSONResp: dict[dict] = self.api.get('/hosts/status')
+ if self.api._convert_special_types:
+ for host in JSONResp:
+ host['check_time'] = datetime.strptime(host['check_time'], DATE_JSON_FORMAT)
+ if host.get('competitor_status'):
+ for competitor in host['competitor_status']:
+ competitor['check_time'] = datetime.strptime(competitor['check_time'], DATE_JSON_FORMAT)
+
+ return JSONResp
+
+ def regex(self) -> list:
+ """Get all supported links Regex, useful to find supported links inside a document. This request does not require authentication"""
+ return self.api.get('/hosts/regex')
+
+ def regex_folder(self) -> list:
+ """Get all supported folder Regex, useful to find supported links inside a document. This request does not require authentication"""
+ return self.api.get('/hosts/regexFolder')
+
+ def domains(self):
+ """Get all hoster domains supported on the service. This request does not require authentication"""
+ return self.api.get('/hosts/domains')
+
+class Settings:
+ def __init__(self, api: RealDebridApi):
+ self.api = api
+
+ def get(self):
+ """Get current user settings with possible values to update"""
+ return self.api.get('/settings')
+
+ def update(self, setting_name: str, setting_value:str):
+ """Update a user setting, returns 204 HTTP code"""
+ return self.api.post('/settings/update', setting_name=setting_name, setting_value=setting_value)
+
+ def convert_points(self):
+ """Convert fidelity points, returns 204 HTTP code"""
+ return self.api.post('/settings/convertPoints')
+
+ def change_password(self):
+ """Send the verification email to change the password, returns 204 HTTP code"""
+ return self.api.post('/settings/changePassword')
+
+ def avatar_file(self, filepath:Path):
+ """Upload a new user avatar image, returns 204 HTTP code"""
+ return self.api.put('/settings/avatarFile', filepath=filepath)
+
+ def avatar_delete(self):
+ """Reset user avatar image to default, returns 204 HTTP code"""
+ return self.api.delete('/settings/avatarDelete')
+
diff --git a/cyberdrop_dl/managers/real_debrid/errors.py b/cyberdrop_dl/managers/real_debrid/errors.py
new file mode 100755
index 000000000..e2c1df46c
--- /dev/null
+++ b/cyberdrop_dl/managers/real_debrid/errors.py
@@ -0,0 +1,62 @@
+from typing import TYPE_CHECKING
+from http import HTTPStatus
+from yarl import URL
+
+if TYPE_CHECKING:
+ from requests import Response
+
+ERROR_CODES = {
+ -1: "Internal error",
+ 1: "Missing parameter",
+ 2: "Bad parameter value",
+ 3: "Unknown method",
+ 4: "Method not allowed",
+ 5: "Slow down",
+ 6: "Ressource unreachable",
+ 7: "Resource not found",
+ 8: "Bad token",
+ 9: "Permission denied",
+ 10: "Two-Factor authentication needed",
+ 11: "Two-Factor authentication pending",
+ 12: "Invalid login",
+ 13: "Invalid password",
+ 14: "Account locked",
+ 15: "Account not activated",
+ 16: "Unsupported hoster",
+ 17: "Hoster in maintenance",
+ 18: "Hoster limit reached",
+ 19: "Hoster temporarily unavailable",
+ 20: "Hoster not available for free users",
+ 21: "Too many active downloads",
+ 22: "IP Address not allowed",
+ 23: "Traffic exhausted",
+ 24: "File unavailable",
+ 25: "Service unavailable",
+ 26: "Upload too big",
+ 27: "Upload error",
+ 28: "File not allowed",
+ 29: "Torrent too big",
+ 30: "Torrent file invalid",
+ 31: "Action already done",
+ 32: "Image resolution error",
+ 33: "Torrent already active",
+ 34: "Too many requests",
+ 35: "Infringing file",
+ 36: "Fair Usage Limit"
+}
+
+class RealDebridError(BaseException):
+ """Base RealDebrid API error"""
+ def __init__(self, response: Response):
+ self.path = URL(response.url).path
+ try:
+ JSONResp: dict = response.json()
+ self.code = JSONResp.get('error_code')
+ self.error = ERROR_CODES.get(self.code, 'Unknown error')
+
+ except AttributeError:
+ self.code = response.status_code
+ self.error = f"{self.code} - {HTTPStatus(self.code).phrase}"
+
+ self.msg = f'{self.code}: {self.error} at {self.path}'
+ super().__init__(self.msg)
diff --git a/cyberdrop_dl/managers/realdebrid_manager.py b/cyberdrop_dl/managers/realdebrid_manager.py
new file mode 100644
index 000000000..f4ebf1ff5
--- /dev/null
+++ b/cyberdrop_dl/managers/realdebrid_manager.py
@@ -0,0 +1,84 @@
+from typing import TYPE_CHECKING, Optional
+
+from cyberdrop_dl.managers.real_debrid.api import RealDebridApi
+from cyberdrop_dl.managers.real_debrid.errors import RealDebridError
+from cyberdrop_dl.utils.utilities import log
+import re
+from dataclasses import field
+
+if TYPE_CHECKING:
+ from yarl import URL
+ from re import Pattern
+ from cyberdrop_dl.managers.manager import Manager
+
+FOLDER_AS_PART = {'folder','folders','dir'}
+FOLDER_AS_QUERY = {'sharekey'}
+
+class RealDebridManager:
+ def __init__(self, manager: 'Manager'):
+ self.manager = manager
+ self.__api_token = self.manager.config_manager.authentication_data['RealDebrid']['realdebrid_api_key']
+ self.enabled = bool(self.__api_token)
+ self.file_regex: Pattern = field(init=False)
+ self.folder_regex: Pattern = field(init=False)
+ self.supported_regex: Pattern = field(init=False)
+ self.api: RealDebridApi = field(init=False)
+ self._folder_guess_functions = [
+ self._guess_folder_by_part,
+ self._guess_folder_by_query
+ ]
+
+ async def startup(self) -> None:
+ """Startup process for Real Debrid manager"""
+ try:
+ self.api = RealDebridApi(self.__api_token, True)
+ file_regex = "|".join(self.api.hosts.regex())
+ folder_regex = "|".join(self.api.hosts.regex_folder())
+ self.supported_regex = re.compile("|".join(folder_regex + file_regex))
+ self.file_regex = re.compile("|".join(file_regex))
+ self.folder_regex = re.compile("|".join(folder_regex))
+ except RealDebridError:
+ await log("Failed RealDebrid setup", 40)
+ self.enabled = False
+
+ async def is_supported_folder(self, url: URL) -> bool:
+ match = self.folder_regex.search(url)
+ return bool(match)
+
+ async def is_supported_file(self, url: URL) -> bool:
+ match = self.file_regex.search(url)
+ return bool(match)
+
+ async def is_supported(self, url: URL) -> bool:
+ match = self.supported_regex.search(url)
+ return bool(match)
+
+ async def unrestrict_link(self, url: URL, password: Optional[str] = None) -> URL:
+ return self.api.unrestrict.link(url, password).get('download')
+
+ async def unrestrict_folder(self, url: URL) -> list [URL]:
+ return self.api.unrestrict.folder(url)
+
+ async def _guess_folder_by_part(self, url: URL):
+ folder = None
+ for word in FOLDER_AS_PART:
+ if word in url.parts:
+ index = url.parts.index(word)
+ if index + 1 < len(url.parts):
+ return url.parts[index + 1]
+ return folder
+
+ async def _guess_folder_by_query(self, url: URL):
+ for word in FOLDER_AS_QUERY:
+ folder = url.query.get(word)
+ if folder:
+ break
+ return folder
+
+ async def guess_folder(self, url:URL ) -> str:
+ for guess_function in self._folder_guess_functions:
+ folder = await guess_function(url)
+ if folder:
+ return folder
+ return url.path
+
diff --git a/cyberdrop_dl/scraper/scraper.py b/cyberdrop_dl/scraper/scraper.py
index 0576be7a9..ab0149edf 100644
--- a/cyberdrop_dl/scraper/scraper.py
+++ b/cyberdrop_dl/scraper/scraper.py
@@ -284,12 +284,18 @@ class ScrapeMapper:
if self.jdownloader.enabled and isinstance(self.jdownloader.jdownloader_agent, Field):
await self.jdownloader.jdownloader_setup()
+ async def start_real_debrid(self) -> None:
+ """Starts RealDebrid"""
+ if self.manager.real_debrid_manager.enabled and isinstance(self.manager.real_debrid_manager.api, Field):
+ await self.manager.real_debrid_manager.startup()
+
async def start(self) -> None:
"""Starts the orchestra"""
self.manager.scrape_mapper = self
await self.start_scrapers()
await self.start_jdownloader()
+ await self.start_real_debrid()
await self.no_crawler_downloader.startup()