Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: always use GET for redirects (#1323)

NTFSvolume75ec6dcf0c7d
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 0c49e1c94..279525db5 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -35,7 +35,7 @@ class ScraperClient:
self._max_html_stem_len = 245 - min_html_file_path_len
@contextlib.asynccontextmanager
- async def _limiter(self, domain: str):
+ async def _limiter(self, domain: str) -> AsyncGenerator[None]:
with self.client_manager.request_context(domain):
domain_limiter = self.client_manager.get_rate_limiter(domain)
async with self.client_manager.global_rate_limiter, domain_limiter:
diff --git a/cyberdrop_dl/crawlers/_forum.py b/cyberdrop_dl/crawlers/_forum.py
index 18b5b6006..97b94ad2d 100644
--- a/cyberdrop_dl/crawlers/_forum.py
+++ b/cyberdrop_dl/crawlers/_forum.py
@@ -243,7 +243,7 @@ class MessageBoardCrawler(Crawler, is_abc=True):
return await self.thread(scrape_item, thread)
case ["goto" | "posts", _, *_]:
self.check_thread_recursion(scrape_item)
- return await self.follow_redirect_w_get(scrape_item)
+ return await self.follow_redirect(scrape_item)
case _:
raise ValueError
@@ -595,7 +595,7 @@ class HTMLMessageBoardCrawler(MessageBoardCrawler, is_abc=True):
link = link or scrape_item.url
slug = link.name or link.parent.name
if slug.isdigit():
- return await self.follow_redirect_w_head(scrape_item.create_new(link))
+ return await self.follow_redirect(scrape_item.create_new(link))
await super().handle_internal_link(scrape_item, link)
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index d42f68576..b4ad6d660 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -704,26 +704,17 @@ class Crawler(ABC):
log(msg, bug=True)
- async def _get_redirect_url(self, url: AbsoluteHttpURL):
- async with self.request(url, method="HEAD") as head:
- return head.location or url
+ async def _get_redirect_url(self, url: AbsoluteHttpURL) -> AbsoluteHttpURL:
+ async with self.request(url) as resp:
+ return resp.url
@final
@error_handling_wrapper
- async def follow_redirect_w_get(self, scrape_item: ScrapeItem) -> None:
- async with self.request(scrape_item.url) as resp:
- if resp.url == scrape_item.url:
- raise ScrapeError(422)
- scrape_item.url = resp.url
- self.create_task(self.run(scrape_item))
-
- @final
- @error_handling_wrapper
- async def follow_redirect_w_head(self, scrape_item: ScrapeItem) -> None:
- location = await self._get_redirect_url(scrape_item.url)
- if location == scrape_item.url:
- raise ScrapeError(422)
- scrape_item.url = location
+ async def follow_redirect(self, scrape_item: ScrapeItem) -> None:
+ redirect = await self._get_redirect_url(scrape_item.url)
+ if scrape_item.url == redirect:
+ raise ScrapeError(422, "Infinite redirect")
+ scrape_item.url = redirect
self.create_task(self.run(scrape_item))
@staticmethod