Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: catch `Ctrl + C` while scraping

NTFSvolumeeb1083f73cc3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60b18390e..8578f6cf4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Log HTTP requests made to JDownloader
+- If scraping has already started, sending `SIGINT` (`Ctrl + C`) to the process will stop all scraping but will continue with post-runtime actions
+ (sorting, hashing, send notifications, etc..). Press `Ctrl + C` again to force quit
## [10.5.0] - 2026-08-15
diff --git a/cyberdrop_dl/scrape_mapper.py b/cyberdrop_dl/scrape_mapper.py
index 958edfc74..6c041513b 100644
--- a/cyberdrop_dl/scrape_mapper.py
+++ b/cyberdrop_dl/scrape_mapper.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
import contextlib
import dataclasses
import logging
@@ -174,7 +175,7 @@ class ScrapeMapper:
await self.manager.http_client.load_cookie_files(await self.manager.get_cookie_files())
self.tui.mode = self.manager.config.ui.mode
## IMPORTANT: Order of each context matters!
- with self.tui():
+ with self.__cancel_context():
async with (
self.manager.http_client,
storage.monitor(config.min_free_space),
@@ -185,6 +186,21 @@ class ScrapeMapper:
self.manager.scrape_mapper = self
yield self
+ @contextlib.contextmanager
+ def __cancel_context(self) -> Generator[None]:
+ try:
+ with self.tui():
+ yield
+ except asyncio.CancelledError as e:
+ # This is a KeyboardInterrupt cause we never cancel tasks
+ try:
+ _ = self.manager.scrape_mapper
+ except AttributeError:
+ # self.__call__ did not get a change to finish setup
+ raise e from None
+ else:
+ logger.warning("Scraping aborted (KeyboardInterrupt)")
+
async def __async_init__(self) -> None:
if self._ready:
return