Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: handle exceptions within crawler taskgroup

NTFSvolume52196b801ba0
diff --git a/cyberdrop_dl/constants.py b/cyberdrop_dl/constants.py
index f375dd466..7cfb8d3fa 100644
--- a/cyberdrop_dl/constants.py
+++ b/cyberdrop_dl/constants.py
@@ -2,11 +2,10 @@ from __future__ import annotations
import datetime
from contextvars import ContextVar
-from enum import StrEnum, auto
+from enum import Enum, StrEnum, auto
from typing import TYPE_CHECKING, Literal, final
from cyclopts import Parameter
-from typing_extensions import Sentinel
from cyberdrop_dl import __version__
@@ -19,7 +18,9 @@ LOGS_DATETIME_FORMAT = "%Y%m%d_%H%M%S"
LOGS_DATE_FORMAT = "%Y_%m_%d"
STARTUP_TIME_STR = datetime.datetime.now().strftime(LOGS_DATETIME_FORMAT) # noqa: DTZ005
CDL_USER_AGENT = f"cyberdrop-dl/{__version__}"
-MISSING = Sentinel("MISSING")
+
+# TODO: Switch to Sentinel from typing extension when basedpyright supports it
+MISSING = Enum("_Sentinel", ["MISSING", "MISSING"]).MISSING
MAIN_LOG_FILE: ContextVar[Path] = ContextVar("MAIN_LOG_FILE")
diff --git a/cyberdrop_dl/crawlers/_forum.py b/cyberdrop_dl/crawlers/_forum.py
index 2e94434fa..071d6be68 100644
--- a/cyberdrop_dl/crawlers/_forum.py
+++ b/cyberdrop_dl/crawlers/_forum.py
@@ -444,7 +444,7 @@ class HTMLMessageBoardCrawler(MessageBoardCrawler, is_abc=True):
scrape_item.append_folders(post_title)
stats: dict[str, int] = {}
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for scraper in (
self._attachments,
self._images,
diff --git a/cyberdrop_dl/crawlers/clonr.py b/cyberdrop_dl/crawlers/clonr.py
index 19188feec..ebe56ad09 100644
--- a/cyberdrop_dl/crawlers/clonr.py
+++ b/cyberdrop_dl/crawlers/clonr.py
@@ -57,7 +57,7 @@ class ClonrCrawler(Crawler):
return
sleep = aio.periodic_sleep(10)
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for file in files:
tg.create_eager_task(self._file(scrape_item, file))
scrape_item.add_children()
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index 5b815622f..ca4d9c961 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -892,10 +892,36 @@ class Crawler(HTTPMixin, HLSMixin, ABC):
@final
@contextlib.asynccontextmanager
- async def new_task_group(self, scrape_item: ScrapeItem | AbsoluteHttpURL) -> AsyncGenerator[aio.EagerTaskGroup]:
- async with aio.EagerTaskGroup() as tg:
- with self.catch_errors(scrape_item):
- yield tg
+ async def new_task_group(self) -> AsyncGenerator[aio.EagerTaskGroup]:
+ """A taskgroup that does not cancel its tasks if an exception is raised within its `async with` context
+
+ Exceptions raised within a task will still cancel all tasks
+
+ This is mostly just to catch `MaxChildrenError`"""
+
+ context_exc = None
+ task_exc = None
+
+ try:
+ async with aio.EagerTaskGroup() as tg:
+ try:
+ yield tg
+ except Exception as e: # noqa: BLE001
+ context_exc = e
+ except ExceptionGroup as eg:
+ if context_exc is None:
+ raise
+ task_exc = eg
+
+ try:
+ if task_exc is not None and context_exc is not None:
+ raise ExceptionGroup(task_exc.message, (*task_exc.exceptions, context_exc)) from None
+ if task_exc is not None:
+ raise task_exc from None
+ if context_exc is not None:
+ raise context_exc from None
+ finally:
+ context_exc = task_exc = None
@final
@classmethod
diff --git a/cyberdrop_dl/crawlers/goonbox.py b/cyberdrop_dl/crawlers/goonbox.py
index af97f4ab6..ef87c238c 100644
--- a/cyberdrop_dl/crawlers/goonbox.py
+++ b/cyberdrop_dl/crawlers/goonbox.py
@@ -103,7 +103,7 @@ class GoonBoxCrawler(Crawler):
scrape_item.setup_as_profile(self.create_title(f"{user} [user]"))
async for albums in self.api.user_albums(user, sort=scrape_item.url.query.get("sort")):
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for album in albums:
url = self.PRIMARY_URL / "a" / album.encoded_id
new_item = scrape_item.create_child(url)
diff --git a/cyberdrop_dl/crawlers/koofr.py b/cyberdrop_dl/crawlers/koofr.py
index de9c6f7c1..684e49c04 100644
--- a/cyberdrop_dl/crawlers/koofr.py
+++ b/cyberdrop_dl/crawlers/koofr.py
@@ -63,7 +63,7 @@ class KooFrCrawler(Crawler):
@error_handling_wrapper
async def _walk_folder(self, scrape_item: ScrapeItem, content_id: str, path: str) -> None:
children = await self.api.get_children(content_id, path, scrape_item.password)
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for node in children:
if node.type == "file":
tg.create_task(self._file(scrape_item, node))
diff --git a/cyberdrop_dl/crawlers/multporn.py b/cyberdrop_dl/crawlers/multporn.py
index d3ab7ba89..fd9f55af1 100644
--- a/cyberdrop_dl/crawlers/multporn.py
+++ b/cyberdrop_dl/crawlers/multporn.py
@@ -43,7 +43,7 @@ class MultPornCrawler(Crawler):
name, date = _extract_info(soup)
scrape_item.uploaded_at = self.parse_iso_date(date)
scrape_item.setup_as_album(self.create_title(name))
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for img in _extract_images(soup):
tg.create_task(self.direct_file(scrape_item, img))
scrape_item.add_children()
diff --git a/cyberdrop_dl/crawlers/spankbang.py b/cyberdrop_dl/crawlers/spankbang.py
index 1d98454f4..1d553a00b 100644
--- a/cyberdrop_dl/crawlers/spankbang.py
+++ b/cyberdrop_dl/crawlers/spankbang.py
@@ -151,7 +151,7 @@ class SpankBangCrawler(Crawler):
await self._iter_videos(scrape_item, soup)
async def _iter_videos(self, scrape_item: ScrapeItem, soup: BeautifulSoup) -> None:
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for new_item in self.iter_children(scrape_item, soup, Selector.VIDEOS):
tg.create_task(self.run(new_item, check_referer=True))
diff --git a/cyberdrop_dl/crawlers/yurivan.py b/cyberdrop_dl/crawlers/yurivan.py
index 11acec178..f5ef2f2bf 100644
--- a/cyberdrop_dl/crawlers/yurivan.py
+++ b/cyberdrop_dl/crawlers/yurivan.py
@@ -80,7 +80,7 @@ class YuriVanCrawler(Crawler):
chapter = story.chapters[chapter_idx]
scrape_item.append_folders(self.create_title(chapter.title))
- async with self.new_task_group(scrape_item) as tg:
+ async with self.new_task_group() as tg:
for page in chapter.pages:
tg.create_task(self.direct_file(scrape_item, page.url))
scrape_item.add_children()