Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: handle attachments with trailing slash (xenforo) (#576)

NTFSvolume7638b39fc905
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 7f0f82965..30f84d52b 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -236,7 +236,9 @@ class ScraperClient:
return {}
@limiter
- async def get_head(self, domain: str, url: URL, client_session: CachedSession) -> CIMultiDictProxy[str]:
+ async def get_head(
+ self, domain: str, url: URL, client_session: CachedSession, *, origin: ScrapeItem | URL | None = None
+ ) -> CIMultiDictProxy[str]:
"""Returns the headers from the given URL."""
async with client_session.head(
url,
@@ -244,6 +246,7 @@ class ScraperClient:
ssl=self.client_manager.ssl_context,
proxy=self.client_manager.proxy,
) as response:
+ await self.client_manager.check_http_status(response, origin=origin)
return response.headers
def get_soup_from_file(self, path_as_str: str) -> BeautifulSoup:
diff --git a/cyberdrop_dl/scraper/crawler.py b/cyberdrop_dl/scraper/crawler.py
index f75e9906e..cc37dbd94 100644
--- a/cyberdrop_dl/scraper/crawler.py
+++ b/cyberdrop_dl/scraper/crawler.py
@@ -280,7 +280,7 @@ class Crawler(ABC):
new_url = base.join(new_url)
if not new_url.scheme:
new_url = new_url.with_scheme(base.scheme or "https")
- return new_url
+ return remove_trailing_slash(new_url)
def update_cookies(self, cookies: dict, url: URL | None = None) -> None:
"""Update cookies for the provided URL
@@ -291,6 +291,12 @@ class Crawler(ABC):
self.client.client_manager.cookies.update_cookies(cookies, response_url)
+def remove_trailing_slash(url: URL) -> URL:
+ if url.name:
+ return url
+ return url.parent.with_fragment(url.fragment).with_query(url.query)
+
+
def create_task_id(func: Callable) -> Callable:
"""Wrapper handles task_id creation and removal for ScrapeItems"""
diff --git a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
index a41265668..2fc864096 100644
--- a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
@@ -12,8 +12,8 @@ from typing import TYPE_CHECKING
from bs4 import BeautifulSoup
from yarl import URL
-from cyberdrop_dl.clients.errors import LoginError
-from cyberdrop_dl.scraper.crawler import Crawler, create_task_id
+from cyberdrop_dl.clients.errors import LoginError, ScrapeError
+from cyberdrop_dl.scraper.crawler import Crawler, create_task_id, remove_trailing_slash
from cyberdrop_dl.scraper.filters import set_return_value
from cyberdrop_dl.utils.data_enums_classes.url_objects import FORUM, FORUM_POST, ScrapeItem
from cyberdrop_dl.utils.logger import log
@@ -332,6 +332,17 @@ class XenforoCrawler(Crawler):
@error_handling_wrapper
async def handle_internal_link(self, scrape_item: ScrapeItem) -> None:
"""Handles internal links."""
+ scrape_item.url = remove_trailing_slash(scrape_item.url)
+
+ if scrape_item.url.name.isdigit():
+ head = await self.client.get_head(self.domain, scrape_item.url, origin=scrape_item) # type: ignore
+ redirect = head.get("location")
+ if not redirect:
+ raise ScrapeError(422, origin=scrape_item)
+ scrape_item.url = self.parse_url(redirect)
+ self.manager.task_group.create_task(self.run(scrape_item))
+ return
+
filename, ext = get_filename_and_ext(scrape_item.url.name, forum=True)
scrape_item.add_to_parent_title("Attachments")
scrape_item.part_of_album = True
diff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.py
index 81ed1c3c8..955bae13a 100644
--- a/cyberdrop_dl/utils/utilities.py
+++ b/cyberdrop_dl/utils/utilities.py
@@ -118,9 +118,10 @@ def get_filename_and_ext(filename: str, forum: bool = False) -> tuple[str, str]:
filename_as_path = Path(filename)
if not filename_as_path.suffix:
raise NoExtensionError
- if filename_as_path.suffix[1:].isnumeric() and forum:
+ ext_no_dot = filename_as_path.suffix.split(".")[1]
+ if ext_no_dot.isdigit() and forum:
name, ext = filename_as_path.name.rsplit("-", 1)
- ext = ext.replace(filename_as_path.suffix, "")
+ ext = ext.rsplit(".")[0]
filename_as_path = Path(f"{name}.{ext}")
if len(filename_as_path.suffix) > 5:
raise InvalidExtensionError