Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
fix: handle invalid urls (#641)
NTFSvolume9121f44ceebf
diff --git a/cyberdrop_dl/clients/errors.py b/cyberdrop_dl/clients/errors.pyindex 55daf898a..7a9b18f69 100644--- a/cyberdrop_dl/clients/errors.py+++ b/cyberdrop_dl/clients/errors.py@@ -16,6 +16,7 @@ if TYPE_CHECKING:from cyberdrop_dl.scraper.crawler import ScrapeItemfrom cyberdrop_dl.utils.data_enums_classes.url_objects import MediaItem+# See: https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-5xx-errors/CLOUDFLARE_ERRORS = {520: "Unexpected Response",@@ -42,9 +43,7 @@ class CDLBaseError(Exception):) -> None:self.ui_message = ui_messageself.message = message or ui_message- self.origin = origin- if origin and not isinstance(origin, URL | Path):- self.origin = origin.parents[0] if origin.parents else None+ self.origin = get_origin(origin)super().__init__(self.message)if status:self.status = status@@ -160,8 +159,17 @@ class ScrapeError(CDLBaseError):) -> None:"""This error will be thrown when a scrape fails."""ui_message = create_error_msg(status)- msg = message- super().__init__(ui_message, message=msg, status=status, origin=origin)+ super().__init__(ui_message, message=message, status=status, origin=origin)+++class InvalidURLError(ScrapeError):+ def __init__(+ self, message: str | None = None, origin: ScrapeItem | MediaItem | URL | None = None, url: URL | str = ""+ ) -> None:+ """This error will be thrown when parsed URL is not valid."""+ ui_message = "Invalid URL"+ self.url = url+ super().__init__(ui_message, message=message, origin=origin)class LoginError(CDLBaseError):@@ -198,3 +206,9 @@ def create_error_msg(error: int) -> str:@create_error_msg.registerdef _(error: str) -> str:return error+++def get_origin(origin: ScrapeItem | Path | MediaItem | URL | None = None) -> Path | URL | None:+ if origin and not isinstance(origin, URL | Path):+ return origin.parents[0] if origin.parents else None+ return origindiff --git a/cyberdrop_dl/scraper/crawler.py b/cyberdrop_dl/scraper/crawler.pyindex b73f88f92..d030f993f 100644--- a/cyberdrop_dl/scraper/crawler.py+++ b/cyberdrop_dl/scraper/crawler.py@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Any, ClassVar, Protocolfrom aiolimiter import AsyncLimiterfrom yarl import URL+from cyberdrop_dl.clients.errors import InvalidURLErrorfrom cyberdrop_dl.downloader.downloader import Downloaderfrom cyberdrop_dl.utils.data_enums_classes.url_objects import MediaItem, ScrapeItemfrom cyberdrop_dl.utils.database.tables.history_table import get_db_path@@ -270,12 +271,15 @@ class Crawler(ABC):scrape_item.add_to_parent_title(title)def parse_url(self, link_str: str, relative_to: URL | None = None) -> URL:- assert link_str- assert isinstance(link_str, str)- link_str = clean_link_str(link_str)- encoded = "%" in link_str- base = relative_to or self.primary_base_domain- new_url = URL(link_str, encoded=encoded)+ try:+ assert link_str+ assert isinstance(link_str, str)+ link_str = clean_link_str(link_str)+ encoded = "%" in link_str+ base = relative_to or self.primary_base_domain+ new_url = URL(link_str, encoded=encoded)+ except (AssertionError, AttributeError, ValueError, TypeError) as e:+ raise InvalidURLError(str(e), url=link_str) from eif not new_url.absolute:new_url = base.join(new_url)if not new_url.scheme:diff --git a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.pyindex 2fc864096..a5381fbd6 100644--- a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py+++ b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py@@ -12,7 +12,7 @@ from typing import TYPE_CHECKINGfrom bs4 import BeautifulSoupfrom yarl import URL-from cyberdrop_dl.clients.errors import LoginError, ScrapeError+from cyberdrop_dl.clients.errors import InvalidURLError, LoginError, ScrapeErrorfrom cyberdrop_dl.scraper.crawler import Crawler, create_task_id, remove_trailing_slashfrom cyberdrop_dl.scraper.filters import set_return_valuefrom cyberdrop_dl.utils.data_enums_classes.url_objects import FORUM, FORUM_POST, ScrapeItem@@ -281,17 +281,17 @@ class XenforoCrawler(Crawler):link = self.filter_link(link)if not link:continue- await self.handle_link(scrape_item, link)+ new_scrape_item = self.create_scrape_item(scrape_item, link)+ await self.handle_link(new_scrape_item)scrape_item.add_children()@singledispatchmethoddef is_attachment(self, link: URL) -> bool:if not link:return False- assert link.host, f"{link} has no host"parts = self.attachment_url_partshosts = self.attachment_url_hosts- return any(p in link.parts for p in parts) or any(h in link.host for h in hosts)+ return any(p in link.parts for p in parts) or (link.host and any(h in link.host for h in hosts)) # type: ignore@is_attachment.registerdef _(self, link_str: str) -> bool:@@ -316,18 +316,19 @@ class XenforoCrawler(Crawler):parsed_link = self.parse_url(link_str)return await self.get_absolute_link(parsed_link)- async def handle_link(self, scrape_item: ScrapeItem, link: URL) -> None:- if not link or link == self.primary_base_domain:+ @error_handling_wrapper+ async def handle_link(self, scrape_item: ScrapeItem) -> None:+ if not scrape_item.url or scrape_item.url == self.primary_base_domain:return- assert link.host, f"{link} has no host"- new_scrape_item = self.create_scrape_item(scrape_item, link)- if self.is_attachment(link):- return await self.handle_internal_link(new_scrape_item)- if self.primary_base_domain.host in link.host and self.stop_thread_recursion(new_scrape_item): # type: ignore+ if not scrape_item.url.host:+ raise InvalidURLError("url has no host", origin=scrape_item)+ if self.is_attachment(scrape_item.url):+ return await self.handle_internal_link(scrape_item)+ if self.primary_base_domain.host in scrape_item.url.host and self.stop_thread_recursion(scrape_item): # type: ignoreorigin = scrape_item.parents[0]- return log(f"Skipping nested thread URL {link} found on {origin}", 10)- new_scrape_item.set_type(None, self.manager)- self.handle_external_links(new_scrape_item)+ return log(f"Skipping nested thread URL {scrape_item.url} found on {origin}", 10)+ scrape_item.set_type(None, self.manager)+ self.handle_external_links(scrape_item)@error_handling_wrapperasync def handle_internal_link(self, scrape_item: ScrapeItem) -> None:diff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.pyindex 955bae13a..380a0196d 100644--- a/cyberdrop_dl/utils/utilities.py+++ b/cyberdrop_dl/utils/utilities.py@@ -18,7 +18,13 @@ from rich.text import Textfrom yarl import URLfrom cyberdrop_dl import __version__ as current_version-from cyberdrop_dl.clients.errors import CDLBaseError, InvalidExtensionError, NoExtensionError+from cyberdrop_dl.clients.errors import (+ CDLBaseError,+ InvalidExtensionError,+ InvalidURLError,+ NoExtensionError,+ get_origin,+)from cyberdrop_dl.utils import constantsfrom cyberdrop_dl.utils.logger import log, log_debug, log_spacer, log_with_color@@ -47,7 +53,9 @@ def error_handling_wrapper(func: Callable) -> Callable:except CDLBaseError as e:log_message_short = e_ui_failure = e.ui_messagelog_message = f"{e.ui_message} - {e.message}" if e.ui_message != e.message else e.message- origin = e.origin+ origin = e.origin or get_origin(item)+ if isinstance(e, InvalidURLError):+ link = e.url or linkexcept TimeoutError:log_message_short = log_message = e_ui_failure = "Timeout"except ClientConnectorError as e: