Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: bunkr crawler (#591)

NTFSvolumeead52ccd7db0
diff --git a/cyberdrop_dl/scraper/crawler.py b/cyberdrop_dl/scraper/crawler.py
index b176a63b5..22a5b7a32 100644
--- a/cyberdrop_dl/scraper/crawler.py
+++ b/cyberdrop_dl/scraper/crawler.py
@@ -294,6 +294,7 @@ def create_task_id(func: Callable) -> Callable:
scrape_item: ScrapeItem = args[0]
task_id = self.scraping_progress.add_task(scrape_item.url)
try:
+ pre_check_scrape_item(scrape_item)
return await func(self, *args, **kwargs)
except ValueError:
log(f"Scrape Failed: {UNKNOWN_URL_PATH_MSG}: {scrape_item.url}", 40)
@@ -303,3 +304,8 @@ def create_task_id(func: Callable) -> Callable:
self.scraping_progress.remove_task(task_id)
return wrapper
+
+
+def pre_check_scrape_item(scrape_item: ScrapeItem) -> None:
+ if scrape_item.url.path == "/":
+ raise ValueError
diff --git a/cyberdrop_dl/scraper/crawlers/bunkrr_crawler.py b/cyberdrop_dl/scraper/crawlers/bunkrr_crawler.py
index c8a1434f9..e5bad3831 100644
--- a/cyberdrop_dl/scraper/crawlers/bunkrr_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/bunkrr_crawler.py
@@ -3,6 +3,9 @@ from __future__ import annotations
import calendar
import datetime
import re
+from dataclasses import dataclass
+from functools import partial
+from pathlib import Path
from typing import TYPE_CHECKING, ClassVar
from yarl import URL
@@ -14,11 +17,14 @@ from cyberdrop_dl.utils.data_enums_classes.url_objects import FILE_HOST_ALBUM, S
from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
+ from collections.abc import Callable
+
from bs4 import BeautifulSoup, Tag
from cyberdrop_dl.managers.manager import Manager
BASE_CDNS = [
+ "bacon",
"big-taco",
"burger",
"c",
@@ -34,6 +40,7 @@ BASE_CDNS = [
"soup",
"taquito",
"wiener",
+ "wings",
r"mlk-bk\.cdn\.gigachad-cdn",
]
@@ -43,6 +50,43 @@ CDNS = BASE_CDNS + EXTENDED_CDNS + IMAGE_CDNS
CDN_REGEX_STR = r"^(?:(?:(" + "|".join(CDNS) + r")[0-9]{0,2}(?:redir)?))\.bunkr?\.[a-z]{2,3}$"
CDN_POSSIBILITIES = re.compile(CDN_REGEX_STR)
+ALBUM_ITEM_SELECTOR = "div[class*='relative group/item theItem']"
+ITEM_NAME_SELECTOR = "p[class*='theName']"
+ITEM_DATE_SELECTOR = 'span[class*="theDate"]'
+DOWNLOAD_BUTTON_SELECTOR = "a.btn.ic-download-01"
+IMAGE_PREVIEW_SELECTOR = "img.max-h-full.w-auto.object-cover.relative"
+VIDEO_AND_IMAGE_EXTS = FILE_FORMATS["Images"] | FILE_FORMATS["Videos"]
+
+
+@dataclass(frozen=True)
+class AlbumItem:
+ name: str
+ thumbnail: str
+ date: int
+ url: URL
+
+ @classmethod
+ def from_tag(cls, tag: Tag, parse_url: Callable[..., URL]) -> AlbumItem:
+ name = tag.select_one(ITEM_NAME_SELECTOR).text # type: ignore
+ thumbnail: str = tag.select_one("img").get("src") # type: ignore
+ date_str = tag.select_one(ITEM_DATE_SELECTOR).text.strip() # type: ignore
+ date = parse_datetime(date_str)
+ link_str: str = tag.find("a").get("href") # type: ignore
+ link = parse_url(link_str)
+ return cls(name, thumbnail, date, link)
+
+ def get_src(self, parse_url: Callable[..., URL]) -> URL:
+ src_str = self.thumbnail.replace("/thumbs/", "/")
+ src = parse_url(src_str)
+ src = with_suffix_encoded(src, self.suffix).with_query(None)
+ if src.suffix.lower() not in FILE_FORMATS["Images"]:
+ src = src.with_host(src.host.replace("i-", "")) # type: ignore
+ return override_cdn(src)
+
+ @property
+ def suffix(self) -> str:
+ return Path(self.name).suffix
+
class BunkrrCrawler(Crawler):
SUPPORTED_SITES: ClassVar[dict[str, list]] = {"bunkrr": ["bunkrr", "bunkr"]}
@@ -56,17 +100,13 @@ class BunkrrCrawler(Crawler):
@create_task_id
async def fetch(self, scrape_item: ScrapeItem) -> None:
"""Determines where to send the scrape item based on the url."""
- if scrape_item.url.path == "/":
- raise ValueError
-
- if self.is_reinforced_link(scrape_item.url):
- scrape_item.url = await self.handle_reinforced_link(scrape_item.url, scrape_item)
- if not scrape_item.url:
- return
+ scrape_item.url = await self.get_final_url(scrape_item)
+ if not scrape_item.url:
+ return
if "a" in scrape_item.url.parts:
await self.album(scrape_item)
- elif self.is_cdn(scrape_item.url) and not self.is_stream_redirect(scrape_item.url):
+ elif is_cdn(scrape_item.url) and not is_stream_redirect(scrape_item.url):
await self.handle_direct_link(scrape_item)
else:
await self.file(scrape_item)
@@ -74,68 +114,45 @@ class BunkrrCrawler(Crawler):
@error_handling_wrapper
async def album(self, scrape_item: ScrapeItem) -> None:
"""Scrapes an album."""
- if not scrape_item.url:
- return
-
scrape_item.url = self.primary_base_domain.with_path(scrape_item.url.path)
- album_id = scrape_item.url.parts[2]
- scrape_item.album_id = album_id
- scrape_item.part_of_album = True
- results = await self.get_album_results(album_id)
- scrape_item.set_type(FILE_HOST_ALBUM, self.manager)
async with self.request_limiter:
soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url, origin=scrape_item)
- title = soup.select_one("title").text.rsplit(" | Bunkr")[0].strip()
- title = self.create_title(title, album_id)
+ scrape_item.album_id = scrape_item.url.parts[2]
+ scrape_item.part_of_album = True
+ scrape_item.set_type(FILE_HOST_ALBUM, self.manager)
+ title = soup.select_one("title").text.rsplit(" | Bunkr")[0].strip() # type: ignore
+ title = self.create_title(title, scrape_item.album_id)
scrape_item.add_to_parent_title(title)
+ results = await self.get_album_results(scrape_item.album_id)
- card_listings: list[Tag] = soup.select('div[class*="relative group/item theItem"]')
- for card_listing in card_listings:
- filename = card_listing.select_one('p[class*="theName"]').text
- file_ext = "." + filename.split(".")[-1]
- thumbnail: str = card_listing.select_one("img").get("src")
- date_str = card_listing.select_one('span[class*="theDate"]').text.strip()
- date = self.parse_datetime(date_str)
- link_str: str = card_listing.find("a").get("href")
- link = self.parse_url(link_str, scrape_item.url.with_path("/"))
- new_scrape_item = self.create_scrape_item(
- scrape_item,
- link,
- album_id=album_id,
- possible_datetime=date,
- add_parent=scrape_item.url,
- )
-
- valid_extensions = FILE_FORMATS["Images"] | FILE_FORMATS["Videos"]
- src_str = thumbnail.replace("/thumbs/", "/")
- src = self.parse_url(src_str)
- src = src.with_suffix(file_ext).with_query(None)
- if file_ext.lower() not in FILE_FORMATS["Images"]:
- src = src.with_host(src.host.replace("i-", ""))
-
- src = self.override_cdn(src)
- # Scrape new URL if unable to get final URL from thumbnail
- if file_ext.lower() not in valid_extensions or "no-image" in src.name or self.deep_scrape(src):
- self.manager.task_group.create_task(self.run(new_scrape_item))
-
- else:
- src_filename, ext = self.get_filename_and_ext(src.name, assume_ext=".mp4")
- filename, _ = self.get_filename_and_ext(filename, assume_ext=".mp4")
- if not self.check_album_results(src, results):
- await self.handle_file(src, new_scrape_item, src_filename, ext, custom_filename=filename)
+ item_tags: list[Tag] = soup.select(ALBUM_ITEM_SELECTOR)
+ parse_url = partial(self.parse_url, relative_to=scrape_item.url.with_path("/"))
+ create = partial(self.create_scrape_item, scrape_item, add_parent=scrape_item.url)
+ for tag in item_tags:
+ item = AlbumItem.from_tag(tag, parse_url)
+ new_scrape_item = create(item.url, possible_datetime=item.date)
+ await self.process_album_item(new_scrape_item, item, results)
scrape_item.add_children()
+ async def process_album_item(self, scrape_item: ScrapeItem, item: AlbumItem, results: dict):
+ src = item.get_src(self.parse_url)
+ if src.suffix.lower() not in VIDEO_AND_IMAGE_EXTS or "no-image" in src.name or self.deep_scrape(src):
+ return self.manager.task_group.create_task(self.run(scrape_item))
+
+ filename, ext = self.get_filename_and_ext(src.name, assume_ext=".mp4")
+ custom_name, _ = self.get_filename_and_ext(item.name, assume_ext=".mp4")
+ if not self.check_album_results(src, results):
+ await self.handle_file(src, scrape_item, filename, ext, custom_filename=custom_name)
+
@error_handling_wrapper
async def file(self, scrape_item: ScrapeItem) -> None:
"""Scrapes a file."""
- if not scrape_item.url:
- return
- soup = link_container = None
+ soup = link_container = date = None # type: ignore
src_selector = "src"
- if self.is_stream_redirect(scrape_item.url):
+ if is_stream_redirect(scrape_item.url):
soup, scrape_item.url = await self.client.get_soup_and_return_url(self.domain, scrape_item.url)
scrape_item.url = self.primary_base_domain.with_path(scrape_item.url.path)
@@ -152,25 +169,24 @@ class BunkrrCrawler(Crawler):
# try image
if not (link_container or self.manager.config_manager.deep_scrape):
- link_container = soup.select_one("img.max-h-full.w-auto.object-cover.relative")
+ link_container = soup.select_one(IMAGE_PREVIEW_SELECTOR)
# fallback for everything else
if not link_container:
- link_container = soup.select_one("a.btn.ic-download-01")
+ link_container = soup.select_one(DOWNLOAD_BUTTON_SELECTOR)
src_selector = "href"
- link_str: str = link_container.get(src_selector) if link_container else None
+ link_str: str = link_container.get(src_selector) if link_container else None # type: ignore
if not link_str:
raise ScrapeError(422, "Couldn't find source", origin=scrape_item)
link = self.parse_url(link_str)
- date = None
- date_str = soup.select_one('span[class*="theDate"]')
+ date_str = soup.select_one(ITEM_DATE_SELECTOR)
if date_str:
- date = self.parse_datetime(date_str.text.strip())
+ date = parse_datetime(date_str.text.strip())
new_scrape_item = self.create_scrape_item(scrape_item, scrape_item.url, possible_datetime=date)
- await self.handle_direct_link(new_scrape_item, link, fallback_filename=soup.select_one("h1").text)
+ await self.handle_direct_link(new_scrape_item, link, fallback_filename=soup.select_one("h1").text) # type: ignore
async def handle_direct_link(
self, scrape_item: ScrapeItem, url: URL | None = None, fallback_filename: str | None = None
@@ -180,8 +196,9 @@ class BunkrrCrawler(Crawler):
If `link` is not supplied, `scrape_item.url` will be used by default
`fallback_filename` will only be used if the link has not `n` query parameter"""
+
link = url or scrape_item.url
- if self.is_reinforced_link(link):
+ if is_reinforced_link(link):
link: URL = await self.handle_reinforced_link(link, scrape_item)
if not link:
@@ -197,8 +214,7 @@ class BunkrrCrawler(Crawler):
@error_handling_wrapper
async def handle_reinforced_link(self, url: URL, scrape_item: ScrapeItem) -> URL | None:
- """Gets the download link for a given reinforced URL."""
- """get.bunkr.su"""
+ """Gets the download link for a given reinforced URL (get.bunkr.su)."""
async with self.request_limiter:
soup: BeautifulSoup = await self.client.get_soup(self.domain, url, origin=scrape_item)
@@ -206,36 +222,50 @@ class BunkrrCrawler(Crawler):
link_container = soup.select('a[download*=""]')[-1]
except IndexError:
link_container = soup.select("a[class*=download]")[-1]
- link_str: str = link_container.get("href")
+ link_str: str = link_container.get("href") # type: ignore
return self.parse_url(link_str)
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
def deep_scrape(self, url: URL) -> bool:
+ assert url.host
return any(part in url.host.split(".") for part in ("burger",)) or self.manager.config_manager.deep_scrape
- @staticmethod
- def is_reinforced_link(url: URL) -> bool:
- return any(part in url.host.split(".") for part in ("get",))
-
- @staticmethod
- def is_stream_redirect(url: URL) -> bool:
- return any(part in url.host for part in ("cdn12", "cdn-")) or url.host == "cdn.bunkr.ru"
-
- @staticmethod
- def is_cdn(url: URL) -> bool:
- """Checks if a given URL is from a CDN."""
- return bool(CDN_POSSIBILITIES.match(url.host))
-
- @staticmethod
- def parse_datetime(date: str) -> int:
- """Parses a datetime string into a unix timestamp."""
- parsed_date = datetime.datetime.strptime(date, "%H:%M:%S %d/%m/%Y")
- return calendar.timegm(parsed_date.timetuple())
-
- @staticmethod
- def override_cdn(link: URL) -> URL:
- new_link = link
- if "milkshake" in link.host:
- new_link = link.with_host("mlk-bk.cdn.gigachad-cdn.ru")
- return new_link
+ async def get_final_url(self, scrape_item: ScrapeItem) -> URL:
+ if not is_reinforced_link(scrape_item.url):
+ return scrape_item.url
+ return await self.handle_reinforced_link(scrape_item.url, scrape_item)
+
+
+def is_stream_redirect(url: URL) -> bool:
+ assert url.host
+ return any(part in url.host for part in ("cdn12", "cdn-")) or url.host == "cdn.bunkr.ru"
+
+
+def is_cdn(url: URL) -> bool:
+ """Checks if a given URL is from a CDN."""
+ assert url.host
+ return bool(CDN_POSSIBILITIES.match(url.host))
+
+
+def override_cdn(url: URL) -> URL:
+ assert url.host
+ if "milkshake" in url.host:
+ return url.with_host("mlk-bk.cdn.gigachad-cdn.ru")
+ return url
+
+
+def is_reinforced_link(url: URL) -> bool:
+ assert url.host
+ return any(part in url.host.split(".") for part in ("get",))
+
+
+def parse_datetime(date: str) -> int:
+ """Parses a datetime string into a unix timestamp."""
+ parsed_date = datetime.datetime.strptime(date, "%H:%M:%S %d/%m/%Y")
+ return calendar.timegm(parsed_date.timetuple())
+
+
+def with_suffix_encoded(url: URL, suffix: str) -> URL:
+ name = Path(url.raw_name).with_suffix(suffix)
+ return url.parent.joinpath(str(name), encoded=True).with_query(url.query).with_fragment(url.fragment)