Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add support for cyberdrop direct links

NTFSvolumea664f6aace3c
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 2379245ba..d6d34dab8 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -65,7 +65,7 @@ class ScraperClient:
@limiter
async def flaresolverr(self, domain: str, url: URL, client_session: ClientSession,
- origin: Optional[ScrapeItem | URL] = None) -> str:
+ origin: Optional[ScrapeItem | URL] = None, with_response_url: bool = False) -> str:
"""Returns the resolved URL from the given URL"""
if not self.client_manager.flaresolverr:
raise DDOSGuardFailure(message="FlareSolverr is not configured", origin=origin)
@@ -81,40 +81,41 @@ class ScraperClient:
if status != "ok":
raise DDOSGuardFailure(message="Failed to resolve URL with flaresolverr", origin=origin)
- return json_obj.get("solution").get("response")
+ response = json_obj.get("solution").get("response")
+ response_url = json_obj.get("solution").get("url")
+ if with_response_url:
+ return response , URL(response_url)
+ return response
+
@limiter
async def get_BS4(self, domain: str, url: URL, client_session: ClientSession,
- origin: Optional[ScrapeItem | URL] = None) -> BeautifulSoup:
+ origin: Optional[ScrapeItem | URL] = None, with_response_url: bool = False) -> BeautifulSoup:
"""Returns a BeautifulSoup object from the given URL"""
async with client_session.get(url, headers=self._headers, ssl=self.client_manager.ssl_context,
proxy=self.client_manager.proxy) as response:
try:
await self.client_manager.check_http_status(response, origin=origin)
except DDOSGuardFailure:
- response_text = await self.flaresolverr(domain, url, origin=origin)
- return BeautifulSoup(response_text, 'html.parser')
+ response = await self.flaresolverr(domain, url, origin=origin, with_response_url=with_response_url)
+ if with_response_url:
+ return BeautifulSoup(response[0], 'html.parser'), response[1]
+ return BeautifulSoup(response, 'html.parser')
+
content_type = response.headers.get('Content-Type')
assert content_type is not None
if not any(s in content_type.lower() for s in ("html", "text")):
raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting text", origin=origin)
text = await response.text()
+ if with_response_url:
+ return BeautifulSoup(text, 'html.parser'), URL(response.url)
return BeautifulSoup(text, 'html.parser')
- @limiter
- async def get_BS4_and_return_URL(self, domain: str, url: URL, client_session: ClientSession,
+ async def get_BS4_and_return_URL(self, domain: str, url: URL,
origin: Optional[ScrapeItem | URL] = None) -> tuple[
BeautifulSoup, URL]:
"""Returns a BeautifulSoup object and response URL from the given URL"""
- async with client_session.get(url, headers=self._headers, ssl=self.client_manager.ssl_context,
- proxy=self.client_manager.proxy) as response:
- await self.client_manager.check_http_status(response, origin=origin)
- content_type = response.headers.get('Content-Type')
- assert content_type is not None
- if not any(s in content_type.lower() for s in ("html", "text")):
- raise InvalidContentTypeFailure(message=f"Received {content_type}, was expecting text", origin=origin)
- text = await response.text()
- return BeautifulSoup(text, 'html.parser'), URL(response.url)
+ return await self.get_BS4(domain, url, origin = origin, with_response_url = True)
@limiter
async def get_json(self, domain: str, url: URL, params: Optional[Dict] = None, headers_inc: Optional[Dict] = None,
diff --git a/cyberdrop_dl/scraper/crawlers/cyberdrop_crawler.py b/cyberdrop_dl/scraper/crawlers/cyberdrop_crawler.py
index 8cff18cff..94b807765 100644
--- a/cyberdrop_dl/scraper/crawlers/cyberdrop_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/cyberdrop_crawler.py
@@ -2,25 +2,31 @@ from __future__ import annotations
import calendar
import datetime
+import re
from typing import TYPE_CHECKING
from aiolimiter import AsyncLimiter
+import aiofiles
from yarl import URL
+from bs4 import BeautifulSoup
from cyberdrop_dl.scraper.crawler import Crawler
from cyberdrop_dl.utils.dataclasses.url_objects import ScrapeItem
-from cyberdrop_dl.utils.utilities import get_filename_and_ext, error_handling_wrapper
+from cyberdrop_dl.utils.utilities import get_filename_and_ext, error_handling_wrapper, log
+from cyberdrop_dl.clients.errors import ScrapeFailure, DDOSGuardFailure
if TYPE_CHECKING:
from cyberdrop_dl.managers.manager import Manager
from bs4 import BeautifulSoup
+CDN_POSSIBILITIES = re.compile(
+ r"^(?:(?:k1)[0-9]{0,2})(?:redir)?\.cyberdrop?\.[a-z]{2,3}$")
class CyberdropCrawler(Crawler):
def __init__(self, manager: Manager):
super().__init__(manager, "cyberdrop", "Cyberdrop")
self.api_url = URL("https://api.cyberdrop.me/api/")
- self.primary_base_url = URL("https://cyberdrop.me/")
+ self.primary_base_domain = URL("https://cyberdrop.me/")
self.request_limiter = AsyncLimiter(1.0, 2.0)
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
@@ -45,15 +51,22 @@ class CyberdropCrawler(Crawler):
scrape_item.album_id = scrape_item.url.parts[2]
scrape_item.part_of_album = True
+ date = title = None
+
+ try:
+ title = await self.create_title(soup.select_one("h1[id=title]").text, scrape_item.album_id, None)
+ except AttributeError:
+ raise ScrapeFailure (404, message = 'No album information found in response content', origin = scrape_item)
- title = await self.create_title(soup.select_one("h1[id=title]").text, scrape_item.album_id, None)
- date = await self.parse_datetime(soup.select("p[class=title]")[-1].text)
+ date = soup.select("p[class=title]")
+ if date:
+ date = await self.parse_datetime(date[-1].text)
links = soup.select("div[class*=image-container] a[class=image]")
for link in links:
link = link.get('href')
if link.startswith("/"):
- link = self.primary_base_url.with_path(link)
+ link = self.primary_base_domain.with_path(link)
link = URL(link)
new_scrape_item = await self.create_scrape_item(scrape_item, link, title, True, None, date,
@@ -63,6 +76,8 @@ class CyberdropCrawler(Crawler):
@error_handling_wrapper
async def file(self, scrape_item: ScrapeItem) -> None:
"""Scrapes a file"""
+ scrape_item.url = await self.get_stream_link(scrape_item.url)
+
if await self.check_complete_from_referer(scrape_item):
return
@@ -83,6 +98,28 @@ class CyberdropCrawler(Crawler):
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
+ def is_cdn(self, url: URL) -> bool:
+ """Checks if a given URL is from a CDN"""
+ return bool(re.match(CDN_POSSIBILITIES, url.host))
+
+ async def get_stream_link(self, url: URL) -> URL:
+ """Gets the stream link for a given URL
+
+ NOTE: This makes a request to get the final URL (if necessary). Calling function must use `@error_handling_wrapper`
+
+ """
+
+ if any([part in url.parts for part in ('a','f')]):
+ return url
+
+ if self.is_cdn(url):
+ return self.primary_base_domain / "f" / url.parts[-1]
+
+ _ , streaming_url = await self.client.get_BS4_and_return_URL(self.domain, url)
+
+ return streaming_url
+
+
async def parse_datetime(self, date: str) -> int:
"""Parses a datetime string into a unix timestamp"""
date = datetime.datetime.strptime(date, "%d.%m.%Y")