Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: delete api, integrate into crawler
NTFSvolume6da389a298c1
diff --git a/cyberdrop_dl/crawlers/realdebrid.py b/cyberdrop_dl/crawlers/realdebrid.pyindex 3d12cff81..081e87c08 100644--- a/cyberdrop_dl/crawlers/realdebrid.py+++ b/cyberdrop_dl/crawlers/realdebrid.py@@ -1,13 +1,25 @@+"""Real-Debrid API Implementation. All methods return their JSON response (if any).++For details, visit: https://api.real-debrid.com++All API methods require authentication except the regexes++The API is limited to 250 requests per minute.+"""+from __future__ import annotationsimport asyncioimport refrom re import Pattern-from typing import TYPE_CHECKING, ClassVar+from typing import TYPE_CHECKING, Any, ClassVar++import aiohttp+from aiolimiter import AsyncLimiterfrom cyberdrop_dl.crawlers.crawler import Crawlerfrom cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL-from cyberdrop_dl.managers.real_debrid.api import RealDebridAPI+from cyberdrop_dl.exceptions import RealDebridErrorfrom cyberdrop_dl.utils.logger import logfrom cyberdrop_dl.utils.utilities import error_handling_wrapper@@ -15,9 +27,47 @@ if TYPE_CHECKING:from cyberdrop_dl.data_structures.url_objects import ScrapeItemPRIMARY_URL = AbsoluteHttpURL("https://real-debrid.com")-_FOLDER_AS_PART = {"folder", "folders", "dir"}-_FOLDER_AS_QUERY = {"sharekey"}_DB_FLATTEN_URL_KEYS = "parts", "query", "frag"+_API_ENTRYPOINT = AbsoluteHttpURL("https://api.real-debrid.com/rest/1.0")+_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 RealDebridCrawler(Crawler):@@ -26,17 +76,19 @@ class RealDebridCrawler(Crawler):FOLDER_DOMAIN: ClassVar[str] = "RealDebrid"def __post_init__(self) -> None:- self._headers = {}- self._api_token = self.manager.auth_config.realdebrid.api_key- self._file_regex: Pattern+ self._api_token = token = self.manager.auth_config.realdebrid.api_keyself._supported_folder_url_regex: Patternself._supported_url_regex: Pattern- self._api: RealDebridAPI- self.disabled = not bool(self._api_token)+ self.request_limiter = AsyncLimiter(250, 60)+ self.disabled = not bool(token)+ self._headers = {"Authorization": f"Bearer {token}", "User-Agent": "CyberDrop-DL"}++ def is_supported(self, url: AbsoluteHttpURL) -> bool:+ match = self._supported_url_regex.search(str(url))+ return bool(match) or "real-debrid" in url.hostasync def async_startup(self) -> None:- self._api = RealDebridAPI(self._api_token, self.client._session)- await self._get_supported_urls_regexes()+ await self._get_regexes()async def fetch(self, scrape_item: ScrapeItem) -> None:scrape_item.url = _reconstruct_original_url(scrape_item.url)@@ -44,64 +96,72 @@ class RealDebridCrawler(Crawler):return await self.folder(scrape_item)await self.file(scrape_item)- async def _get_supported_urls_regexes(self) -> None:+ async def _get_regexes(self) -> None:if self.disabled:returntry:- files_r, folders_r = await asyncio.gather(self._api.hosts.regex(), self._api.hosts.regex_folder())- file_regex = [pattern[1:-1] for pattern in files_r]- folder_regex = [pattern[1:-1] for pattern in folders_r]- file_or_folder_regex = "|".join(file_regex + folder_regex)- folder_regex = "|".join(folder_regex)- self._supported_url_regex = re.compile(file_or_folder_regex)- self._supported_folder_url_regex = re.compile(folder_regex)+ files_r, folders_r = await asyncio.gather(+ self._api_request("hosts/regex"), self._api_request("hosts/regexFolder")+ )+ file_regex: list[str] = [pattern[1:-1] for pattern in files_r]+ folder_regex: list[str] = [pattern[1:-1] for pattern in folders_r]+ self._supported_url_regex = re.compile("|".join(file_regex + folder_regex))+ self._supported_folder_url_regex = re.compile("|".join(folder_regex))except Exception as e:- log(f"Failed RealDebrid setup: {e}", 40)+ log(f"Failed RealDebrid setup: {e!r}", 40)self.disabled = Truedef _is_supported_folder(self, url: AbsoluteHttpURL) -> bool:match = self._supported_folder_url_regex.search(str(url))return bool(match)- def is_supported(self, url: AbsoluteHttpURL) -> bool:- match = self._supported_url_regex.search(str(url))- return bool(match) or "real-debrid" in url.host.lower()-@error_handling_wrapperasync def folder(self, scrape_item: ScrapeItem) -> None:- self.log(f"Scraping folder with RealDebrid: {scrape_item.url}", 20)+ log(f"Scraping folder with RealDebrid: {scrape_item.url}", 20)folder_id = _guess_folder(scrape_item.url)- title = self.create_title(f"{folder_id} [{scrape_item.url.host.lower()}]", folder_id)+ title = self.create_title(f"{folder_id} [{scrape_item.url.host}]", folder_id)scrape_item.setup_as_album(title, album_id=folder_id)- links = await self._api.unrestrict.folder(scrape_item.url)+ links: list[str] = await self._api_request("unrestrict/folder", link=str(scrape_item.url))for link in links:- new_scrape_item = scrape_item.create_child(link)+ new_scrape_item = scrape_item.create_child(self.parse_url(link))self.create_task(self.file(new_scrape_item))scrape_item.add_children()@error_handling_wrapperasync def file(self, scrape_item: ScrapeItem) -> None:original_url = scrape_item.url- password = original_url.query.get("password", "")if await self.check_complete_from_referer(original_url):return- if _is_self_hosted_download(scrape_item.url):- database_url = debrid_url = scrape_item.url+ if _is_self_hosted_download(original_url):+ database_url = debrid_url = original_urlelse:- host = original_url.host.lower()+ host = original_url.hosttitle = self.create_title(f"files [{host}]")scrape_item.setup_as_album(title)- debrid_url = await self._api.unrestrict.link(original_url, password)- database_url = _flatten_url_for_db(original_url, host)+ password = original_url.query.get("password", "")+ json_resp: dict[str, Any] = await self._api_request(+ "unrestrict/link", link=str(original_url), password=password, remote=False+ )+ debrid_url = self.parse_url(json_resp["download"])+ database_url = _flatten_url(original_url, host)if await self.check_complete_from_referer(debrid_url):return- self.log(f"Real Debrid:\n Original URL: {original_url}\n Debrid URL: {debrid_url}", 10)+ log(f"Real Debrid:\n Original URL: {original_url}\n Debrid URL: {debrid_url}", 10)filename, ext = self.get_filename_and_ext(debrid_url.name)await self.handle_file(database_url, scrape_item, filename, ext, debrid_link=debrid_url)+ async def _api_request(self, path: str, /, **data: Any) -> Any:+ method = "POST" if data else "GET"+ data_ = data or None+ async with self.request_limiter, self.client._session.disabled():+ response = await self.client._session.request(+ method, _API_ENTRYPOINT / path, headers=self._headers, data=data_+ )+ return _handle_api_response(response)+def _guess_folder(url: AbsoluteHttpURL) -> str:for guess_function in (_guess_folder_by_part, _guess_folder_by_query):@@ -111,27 +171,31 @@ def _guess_folder(url: AbsoluteHttpURL) -> str:def _guess_folder_by_part(url: AbsoluteHttpURL) -> str | 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]+ for word in ("folder", "folders", "dir"):+ try:+ return url.parts[url.parts.index(word) + 1]+ except (IndexError, ValueError):+ continuedef _guess_folder_by_query(url: AbsoluteHttpURL) -> str | None:- for word in _FOLDER_AS_QUERY:- folder = url.query.get(word)- if folder:+ for word in ("sharekey",):+ if folder := url.query.get(word):return folderdef _is_self_hosted_download(url: AbsoluteHttpURL) -> bool:- return any(subdomain in url.host for subdomain in ("download.", "my.")) and "real-debrid" in url.host+ return url.host in ("download.real-debrid.com", "my.real-debrid.com")def _reconstruct_original_url(url: AbsoluteHttpURL) -> AbsoluteHttpURL:"""Reconstructs an URL that might have been flatten for the database."""- if len(url.parts) < 3 or url.host != PRIMARY_URL.host or url.parts[1].count(".") == 0:+ if (+ len(url.parts) < 3+ or url.host != PRIMARY_URL.host+ or url.parts[1].count(".") == 0+ or _is_self_hosted_download(url)+ ):parsed_url = urlelse:@@ -155,7 +219,7 @@ def _reconstruct_original_url(url: AbsoluteHttpURL) -> AbsoluteHttpURL:return parsed_url-def _flatten_url_for_db(original_url: AbsoluteHttpURL, host: str) -> AbsoluteHttpURL:+def _flatten_url(original_url: AbsoluteHttpURL, host: str) -> AbsoluteHttpURL:"""Some hosts use query params or fragment as id or password (ex: mega.nz)This function flattens the query and fragment as parts of the URL path because database lookups only use the url path"""flatten_url = PRIMARY_URL / host / original_url.path[1:]@@ -167,3 +231,22 @@ def _flatten_url_for_db(original_url: AbsoluteHttpURL, host: str) -> AbsoluteHttflatten_url = flatten_url / "frag" / original_url.fragmentreturn flatten_url+++async def _handle_api_response(response: aiohttp.ClientResponse) -> Any:+ try:+ json_resp: dict[str, Any] = await response.json()+ try:+ response.raise_for_status()+ except aiohttp.ClientResponseError:+ code = json_resp.get("error_code")+ if not code:+ raise+ code = 7 if code == 16 else code+ msg = _ERROR_CODES.get(code, "Unknown error")+ raise RealDebridError(response.url, code, msg) from None+ else:+ return json_resp+ except AttributeError:+ response.raise_for_status()+ return await response.text()diff --git a/cyberdrop_dl/managers/real_debrid/__init__.py b/cyberdrop_dl/managers/real_debrid/__init__.pydeleted file mode 100644index e69de29bb..000000000diff --git a/cyberdrop_dl/managers/real_debrid/api.py b/cyberdrop_dl/managers/real_debrid/api.pydeleted file mode 100644index f3e09c52a..000000000--- a/cyberdrop_dl/managers/real_debrid/api.py+++ /dev/null@@ -1,147 +0,0 @@-"""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` context 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--"""--from __future__ import annotations--from typing import Any--import aiohttp-from aiolimiter import AsyncLimiter--from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL-from cyberdrop_dl.exceptions import RealDebridError--RATE_LIMIT = 250-API_ENTRYPOINT = AbsoluteHttpURL("https://api.real-debrid.com/rest/1.0")-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 RealDebridAPI:- def __init__(self, api_token: str | None, session: aiohttp.ClientSession) -> None:- self._headers: dict[str, str] = {}- self._session = session- self.update_token(api_token or "")- self.unrestrict = Unrestrict(self)- self.hosts = Hosts(self)- self.request_limiter = AsyncLimiter(RATE_LIMIT, 60)-- async def _get(self, path: str, /) -> Any:- async with self.request_limiter:- response = await self._session.get(API_ENTRYPOINT / path, headers=self._headers)- return _handle_response(response)-- async def _post(self, path: str, /, **data: Any) -> Any:- async with self.request_limiter:- response = await self._session.post(API_ENTRYPOINT / path, headers=self._headers, data=data)- return _handle_response(response)-- def update_token(self, new_token: str) -> None:- self._api_token = new_token- self._headers["Authorization"] = f"Bearer {new_token}"---class Unrestrict:- def __init__(self, api: RealDebridAPI) -> None:- self.api = api-- async def check(self, link: AbsoluteHttpURL, password: str | None = None) -> dict[str, Any]:- """Check if a file is downloadable on the concerned hoster. This request does not require authentication."""- return await self.api._post("unrestrict/check", link=link, password=password)-- async def link(self, link: AbsoluteHttpURL, password: str | None = None, remote: bool = False) -> AbsoluteHttpURL:- """Unrestrict a hoster link and get a new unrestricted link."""- json_resp: dict[str, Any] = await self.api._post("unrestrict/link", link=link, password=password, remote=remote)- return AbsoluteHttpURL(json_resp["download"])-- async def folder(self, link: AbsoluteHttpURL) -> list[AbsoluteHttpURL]:- """Unrestrict a hoster folder link and get individual links, returns an empty array if no links found."""- links: list[str] = await self.api._post("unrestrict/folder", link=link)- return [AbsoluteHttpURL(link) for link in links]---class Hosts:- def __init__(self, api: RealDebridAPI) -> None:- self.api = api-- async def get(self) -> list[str]:- """Get supported hosts. This request does not require authentication."""- return await self.api._get("hosts")-- async def regex(self) -> list[str]:- """Get all supported links Regex, useful to find supported links inside a document. This request does not require authentication."""- return await self.api._get("hosts/regex")-- async def regex_folder(self) -> list[str]:- """Get all supported folder Regex, useful to find supported links inside a document. This request does not require authentication."""- return await self.api._get("hosts/regexFolder")-- async def domains(self) -> list[str]:- """Get all hoster domains supported on the service. This request does not require authentication."""- return await self.api._get("hosts/domains")---async def _handle_response(response: aiohttp.ClientResponse) -> Any:- try:- json_resp: dict[str, Any] = await response.json()- try:- response.raise_for_status()- except aiohttp.ClientResponseError:- code = json_resp.get("error_code")- if not code or code not in ERROR_CODES:- raise- code = 7 if code == 16 else code- msg = ERROR_CODES.get(code, "Unknown error")- raise RealDebridError(response.url, code, msg) from None- else:- return json_resp- except AttributeError:- response.raise_for_status()- return await response.text()