Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use for cyberdrop

NTFSvolume250c2d1d88d4
diff --git a/cyberdrop_dl/aio.py b/cyberdrop_dl/aio.py
index 06a5086fc..159b90d1d 100644
--- a/cyberdrop_dl/aio.py
+++ b/cyberdrop_dl/aio.py
@@ -29,6 +29,7 @@ if TYPE_CHECKING:
Coroutine,
Iterable,
Iterator,
+ Sequence,
)
from types import CoroutineType
@@ -249,26 +250,31 @@ async def gather(*coros: Awaitable[_T]) -> list[_T]:
@overload
-async def deterministic_gather(*coros: Awaitable[_T]) -> list[_T]: ...
+async def safe_gather(coro: Awaitable[_T1], /) -> tuple[_T1]: ...
@overload
-async def deterministic_gather(coro: Awaitable[_T], /) -> tuple[_T]: ...
+async def safe_gather(coro_1: Awaitable[_T1], coro_2: Awaitable[_T2], /) -> tuple[_T1, _T2]: ...
@overload
-async def deterministic_gather(coro_1: Awaitable[_T1], coro_2: Awaitable[_T2], /) -> tuple[_T1, _T2]: ...
-@overload
-async def deterministic_gather(
- coro_1: Awaitable[_T1], coro_2: Awaitable[_T2], coro_3: Awaitable[_T3], /
+async def safe_gather(
+ coro_1: Awaitable[_T1],
+ coro_2: Awaitable[_T2],
+ coro_3: Awaitable[_T3],
+ /,
) -> tuple[_T1, _T2, _T3]: ...
-async def deterministic_gather(*coros: Awaitable[_T]) -> list[_T]: # pyright: ignore[reportInconsistentOverload]
- async def try_call(coro: Awaitable[_T]):
- try:
- return await coro
- except Exception as e: # noqa: BLE001
- return e
+async def safe_gather(
+ coro_1: Awaitable[_T1],
+ coro_2: Awaitable[_T2] | None = None,
+ coro_3: Awaitable[_T3] | None = None,
+ /,
+) -> Sequence[_T]:
+ """Like `asyncio.gather(*coros, return_exceptions=True)`, but all exceptions are re-raised as an ExceptionGroup
+
+ This makes errors deterministic"""
- results = await gather(*(try_call(c) for c in coros))
- errors = tuple(r for r in results if isinstance(r, Exception))
+ coros = filter(None, (coro_1, coro_2, coro_3))
+ results = await asyncio.gather(*coros, return_exceptions=True)
+ errors = tuple(r for r in results if isinstance(r, BaseException))
if errors:
raise BaseExceptionGroup("", errors)
return cast("list[_T]", results)
diff --git a/cyberdrop_dl/crawlers/cyberdrop.py b/cyberdrop_dl/crawlers/cyberdrop.py
index 5f4601723..d28dde9b8 100644
--- a/cyberdrop_dl/crawlers/cyberdrop.py
+++ b/cyberdrop_dl/crawlers/cyberdrop.py
@@ -1,18 +1,15 @@
from __future__ import annotations
-import asyncio
from typing import TYPE_CHECKING, ClassVar
-from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths
+from cyberdrop_dl import aio
+from cyberdrop_dl.crawlers.crawler import API, Crawler, RateLimit, SupportedDomains, SupportedPaths
from cyberdrop_dl.url_objects import AbsoluteHttpURL
from cyberdrop_dl.utils import css, error_handling_wrapper
if TYPE_CHECKING:
from cyberdrop_dl.url_objects import ScrapeItem
-_API_ENTRYPOINT = AbsoluteHttpURL("https://api.cyberdrop.cr/api/")
-_PRIMARY_URL = AbsoluteHttpURL("https://cyberdrop.cr/")
-
class Selector:
ALBUM_TITLE = "#title"
@@ -30,10 +27,13 @@ class CyberdropCrawler(Crawler):
),
"Direct links": "/api/file/d/<file_id>",
}
- PRIMARY_URL: ClassVar[AbsoluteHttpURL] = _PRIMARY_URL
+ PRIMARY_URL: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://cyberdrop.cr/")
DOMAIN: ClassVar[str] = "cyberdrop"
- OLD_DOMAINS = ("cyberdrop.me", "cyberdrop.to")
- _RATE_LIMIT: ClassVar[tuple[float, float]] = 5, 1
+ OLD_DOMAINS: ClassVar[tuple[str, ...]] = ("cyberdrop.me", "cyberdrop.to")
+ _RATE_LIMIT: ClassVar[RateLimit] = 5, 1
+
+ def __post_init__(self) -> None:
+ self.api: CyberdropAPI = CyberdropAPI.from_crawler(self)
async def fetch(self, scrape_item: ScrapeItem) -> None:
match scrape_item.url.parts[1:]:
@@ -68,18 +68,21 @@ class CyberdropCrawler(Crawler):
if await self.check_complete_from_referer(scrape_item):
return
- info, auth = await asyncio.gather(
- self.request_json(_API_ENTRYPOINT / "file" / "info" / file_id),
- self.request_json(_API_ENTRYPOINT / "file" / "auth" / file_id),
- return_exceptions=True,
- )
- if isinstance(info, BaseException):
- raise info
-
- if isinstance(auth, BaseException):
- raise auth
+ info, auth = await aio.safe_gather(self.api.file_info(file_id), self.api.file_auth(file_id))
name: str = info["name"]
filename, ext = self.get_filename_and_ext(name)
link = self.parse_url(auth["url"])
await self.handle_file(link, scrape_item, name, ext, custom_filename=filename)
+
+
+class CyberdropAPI(API):
+ ENTRYPOINT: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://api.cyberdrop.cr/api")
+
+ async def file_info(self, file_id: str) -> dict[str, str]:
+ api_url = self.ENTRYPOINT / "file/info" / file_id
+ return await self.request_json(api_url)
+
+ async def file_auth(self, file_id: str) -> dict[str, str]:
+ api_url = self.ENTRYPOINT / "file/auth" / file_id
+ return await self.request_json(api_url)