Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: allow a crawler to check complete of another crawler

NTFSvolumea713a5da52ac
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index e4276d1ad..ed9d34fa9 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -45,6 +45,7 @@ P = ParamSpec("P")
R = TypeVar("R")
T = TypeVar("T")
_T_co = TypeVar("_T_co", covariant=True)
+_CRAWLER_DOMAIN: Any = object()
if TYPE_CHECKING:
@@ -348,10 +349,19 @@ class Crawler(ABC):
return False
- async def check_complete_from_referer(self, scrape_item: ScrapeItem | URL) -> bool:
- """Checks if the scrape item has already been scraped."""
+ async def check_complete_from_referer(
+ self, scrape_item: ScrapeItem | URL, domain: str | None = _CRAWLER_DOMAIN
+ ) -> bool:
+ """Checks if the scrape item has already been scraped.
+
+ by default, it only checks database entries from this crawler. Pass a custom value in domain to check referers from other crawlers
+
+ if domain is `None`, checks database entries for all crawlers and returns `True` if at least 1 of them has marked it as completed
+ """
url = scrape_item if isinstance(scrape_item, URL) else scrape_item.url
- downloaded = await self.manager.db_manager.history_table.check_complete_by_referer(self.DOMAIN, url)
+ if domain is _CRAWLER_DOMAIN:
+ domain = self.DOMAIN
+ downloaded = await self.manager.db_manager.history_table.check_complete_by_referer(domain, url)
if downloaded:
log(f"Skipping {url} as it has already been downloaded", 10)
self.manager.progress_manager.download_progress.add_previously_completed()
diff --git a/cyberdrop_dl/utils/database/tables/history_table.py b/cyberdrop_dl/utils/database/tables/history_table.py
index aae5409b4..bc3441613 100644
--- a/cyberdrop_dl/utils/database/tables/history_table.py
+++ b/cyberdrop_dl/utils/database/tables/history_table.py
@@ -126,18 +126,23 @@ class HistoryTable:
)
await self.db_conn.commit()
- async def check_complete_by_referer(self, domain: str, referer: URL) -> bool:
+ async def check_complete_by_referer(self, domain: str | None, referer: URL) -> bool:
"""Checks whether an individual file has completed given its domain and url path."""
if self.ignore_history:
return False
+ if domain is None:
+ query, *params = "SELECT completed FROM media WHERE referer = ?", str(referer)
+ else:
+ query, *params = "SELECT completed FROM media WHERE referer = ? and domain = ?", str(referer), domain
cursor = await self.db_conn.cursor()
- result = await cursor.execute(
- """SELECT completed FROM media WHERE domain = ? and referer = ?""",
- (domain, str(referer)),
- )
- sql_file_check = await result.fetchone()
- return bool(sql_file_check and sql_file_check[0] != 0)
+ result = await cursor.execute(query, params)
+ if domain is None:
+ results = await result.fetchall()
+ else:
+ row = await result.fetchone()
+ results = [row] if row is not None else []
+ return bool(results and any(row[0] != 0 for row in results))
async def insert_incompleted(self, domain: str, media_item: MediaItem) -> None:
"""Inserts an uncompleted file into the database."""