Skip to content

Bulk asynchronous downloader for multiple file hosts

Scraper error handling wrapper

Jules-WinnfieldXfbd74ed9f9da
diff --git a/cyberdrop_dl/scraper/crawlers/bunkr_crawler.py b/cyberdrop_dl/scraper/crawlers/bunkr_crawler.py
index fe63cb76a..a934c69dd 100644
--- a/cyberdrop_dl/scraper/crawlers/bunkr_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/bunkr_crawler.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import re
from dataclasses import field
+from functools import wraps
from typing import TYPE_CHECKING
from aiolimiter import AsyncLimiter
@@ -9,7 +10,7 @@ from yarl import URL
from cyberdrop_dl.clients.errors import NoExtensionFailure
from cyberdrop_dl.utils.dataclasses.url_objects import MediaItem
-from cyberdrop_dl.utils.utilities import FILE_FORMATS, get_filename_and_ext, sanitize_folder
+from cyberdrop_dl.utils.utilities import FILE_FORMATS, get_filename_and_ext, sanitize_folder, error_handling_wrapper
if TYPE_CHECKING:
from asyncio import Queue
@@ -75,17 +76,18 @@ class BunkrCrawler:
if scrape_item.url.path.startswith("/a/"):
self._current_is_album = True
- await self.album(scrape_item)
+ await self.album(self, scrape_item)
return
elif scrape_item.url.path.startswith("/v/"):
- await self.video(scrape_item)
+ await self.video(self, scrape_item)
else:
- await self.other(scrape_item)
+ await self.other(self, scrape_item)
await self.scraping_progress.remove_task(task_id)
+ @error_handling_wrapper
async def album(self, scrape_item: ScrapeItem) -> None:
"""Scrapes an album"""
async with self.request_limiter:
@@ -105,6 +107,7 @@ class BunkrCrawler:
link = await self.get_stream_link(link)
await self.fetch(ScrapeItem(link, title))
+ @error_handling_wrapper
async def video(self, scrape_item: ScrapeItem) -> None:
"""Scrapes a video"""
async with self.request_limiter:
@@ -119,6 +122,7 @@ class BunkrCrawler:
await self.handle_file(link, scrape_item.url, scrape_item.parent_title, filename, ext)
+ @error_handling_wrapper
async def other(self, scrape_item: ScrapeItem) -> None:
"""Scrapes an image/other file"""
async with self.request_limiter:
diff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.py
index b7c83ec3b..652f95002 100644
--- a/cyberdrop_dl/utils/utilities.py
+++ b/cyberdrop_dl/utils/utilities.py
@@ -2,13 +2,18 @@ from __future__ import annotations
import logging
import re
+from functools import wraps
from typing import TYPE_CHECKING
+from yarl import URL
+
from cyberdrop_dl.clients.errors import NoExtensionFailure
if TYPE_CHECKING:
from typing import Tuple
+ from cyberdrop_dl.managers.manager import Manager
+
logger = logging.getLogger(__name__)
MAX_NAME_LENGTHS = {"FILE": 95, "FOLDER": 60}
@@ -37,6 +42,17 @@ FILE_FORMATS = {
}
+def error_handling_wrapper(func):
+ """Wrapper handles errors for url scraping"""
+ @wraps(func)
+ async def wrapper(self, *args, **kwargs):
+ try:
+ await func(self, *args, **kwargs)
+ except Exception as e:
+ await handle_scrape_error(self.manager, args[0].url, e)
+ return wrapper
+
+
async def sanitize(name: str) -> str:
"""Simple sanitization to remove illegal characters"""
return re.sub(r'[<>:"/\\|?*\']', "", name).strip()
@@ -65,3 +81,16 @@ async def get_filename_and_ext(filename: str, forum: bool = False) -> Tuple[str,
filename = filename.rstrip(".")
filename = await sanitize(filename + ext)
return filename, ext
+
+
+async def handle_scrape_error(manager: Manager, url: URL, error: Exception) -> None:
+ """Handles logging scrape errors"""
+ if hasattr(error, 'status'):
+ if hasattr(error, 'message'):
+ logger.debug(f"Scrape Error: {url} ({error.status} - {error.message})")
+ else:
+ logger.debug(f"Scrape Error: {url} ({error.status})")
+ await manager.progress_manager.scrape_stats_progress.add_failure(error.status)
+ else:
+ logger.debug(f"Scrape Error: {url} ({error})")
+ await manager.progress_manager.scrape_stats_progress.add_failure("Unknown")