Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
feat: improve hotpic crawler (#679)
NTFSvolumed1c4718a7cc8
diff --git a/cyberdrop_dl/scraper/crawlers/hotpic_crawler.py b/cyberdrop_dl/scraper/crawlers/hotpic_crawler.pyindex d90702c98..b5b00cf0b 100644--- a/cyberdrop_dl/scraper/crawlers/hotpic_crawler.py+++ b/cyberdrop_dl/scraper/crawlers/hotpic_crawler.py@@ -1,9 +1,11 @@from __future__ import annotations-from typing import TYPE_CHECKING+from pathlib import Path+from typing import TYPE_CHECKING, ClassVarfrom yarl import URL+from cyberdrop_dl.clients.errors import ScrapeErrorfrom cyberdrop_dl.scraper.crawler import Crawler, create_task_idfrom cyberdrop_dl.utils.data_enums_classes.url_objects import FILE_HOST_ALBUM, ScrapeItemfrom cyberdrop_dl.utils.utilities import error_handling_wrapper, get_filename_and_ext@@ -13,12 +15,18 @@ if TYPE_CHECKING:from cyberdrop_dl.managers.manager import Manager+IMAGE_SELECTOR = "img[id*=main-image]"+VIDEO_SELECTOR = "video > source"+ALBUM_ITEM_SELECTOR = "a[class*=spotlight]"+class HotPicCrawler(Crawler):+ SUPPORTED_SITES: ClassVar[dict[str, list]] = {"hotpic": ["hotpic", "2385290.xyz"]}primary_base_domain = URL("https://hotpic.cc")+ update_unsupported = True- def __init__(self, manager: Manager) -> None:- super().__init__(manager, "hotpic", "HotPic")+ def __init__(self, manager: Manager, site: str) -> None:+ super().__init__(manager, site, "HotPic")"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""@@ -26,11 +34,12 @@ class HotPicCrawler(Crawler):async def fetch(self, scrape_item: ScrapeItem) -> None:"""Determines where to send the scrape item based on the url."""if "album" in scrape_item.url.parts:- await self.album(scrape_item)+ return await self.album(scrape_item)elif "i" in scrape_item.url.parts:- await self.image(scrape_item)- else:- raise ValueError+ return await self.file(scrape_item)+ elif any(p in scrape_item.url.parts for p in ("uploads", "reddit")):+ return await self.handle_direct_link(scrape_item)+ raise ValueError@error_handling_wrapperasync def album(self, scrape_item: ScrapeItem) -> None:@@ -39,29 +48,61 @@ class HotPicCrawler(Crawler):soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url, origin=scrape_item)scrape_item.album_id = scrape_item.url.parts[2]- title = self.create_title(soup.select_one("title").text.rsplit(" - ")[0], scrape_item.album_id)+ title = self.create_title(soup.title.text.rsplit(" - ")[0], scrape_item.album_id) # type: ignorescrape_item.add_to_parent_title(title)scrape_item.part_of_album = Truescrape_item.set_type(FILE_HOST_ALBUM, self.manager)- files = soup.select("a[class*=spotlight]")+ files = soup.select(ALBUM_ITEM_SELECTOR)for file in files:- link_str: str = file.get("href")+ link_str: str = file.get("href") # type: ignorelink = self.parse_url(link_str)+ canonical_url = get_canonical_url(link)filename, ext = get_filename_and_ext(link.name)- await self.handle_file(link, scrape_item, filename, ext)+ await self.handle_file(canonical_url, scrape_item, filename, ext, debrid_link=link)scrape_item.add_children()@error_handling_wrapper- async def image(self, scrape_item: ScrapeItem) -> None:- """Scrapes an image."""+ async def file(self, scrape_item: ScrapeItem) -> None:+ """Scrapes a file."""if await self.check_complete_from_referer(scrape_item):returnasync with self.request_limiter:soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url, origin=scrape_item)- link_str: str = soup.select_one("img[id*=main-image]").get("src")+ file = soup.select_one(VIDEO_SELECTOR) or soup.select_one(IMAGE_SELECTOR)+ if not file:+ raise ScrapeError(422)+ link_str: str = file.get("src") # type: ignorelink = self.parse_url(link_str)+ canonical_url = get_canonical_url(link)filename, ext = get_filename_and_ext(link.name)- await self.handle_file(link, scrape_item, filename, ext)+ await self.handle_file(canonical_url, scrape_item, filename, ext, debrid_link=link)++ @error_handling_wrapper+ async def handle_direct_link(self, scrape_item: ScrapeItem) -> None:+ link = thumbnail_to_img(scrape_item.url)+ canonical_url = get_canonical_url(link)+ filename, ext = get_filename_and_ext(canonical_url.name)+ await self.handle_file(canonical_url, scrape_item, filename, ext, debrid_link=link)+++def thumbnail_to_img(url: URL) -> URL:+ if "thumb" not in url.parts:+ return url+ if (new_ext := ".mp4") != url.suffix:+ new_ext = ".jpg"+ url = with_suffix_encoded(url, new_ext)+ new_parts = [p for p in url.parts if p not in ("/", "thumb")]+ new_path = "/".join(new_parts)+ return url.with_path(new_path)+++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)+++def get_canonical_url(url: URL) -> URL:+ return url.with_host("hotpic.cc")