Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
fix: albums downloads (pixhost) (#1188)
NTFSvolume76057ee45a4a
diff --git a/cyberdrop_dl/crawlers/pixhost.py b/cyberdrop_dl/crawlers/pixhost.pyindex e58c09ede..a6d9214fa 100644--- a/cyberdrop_dl/crawlers/pixhost.py+++ b/cyberdrop_dl/crawlers/pixhost.py@@ -8,54 +8,60 @@ from cyberdrop_dl.utils import cssfrom cyberdrop_dl.utils.utilities import error_handling_wrapperif TYPE_CHECKING:- from bs4 import BeautifulSoup-from cyberdrop_dl.data_structures.url_objects import ScrapeItem+class Selectors:+ GALLERY_TITLE = "a.link h2"+ GALLERY_IMAGES = "div.images a"+ IMAGE = "img.image-img"+++_SELECTORS = Selectors()PRIMARY_URL = AbsoluteHttpURL("https://pixhost.to/")-GALLERY_TITLE_SELECTOR = "a[class=link] h2"-IMAGES_SELECTOR = "div[class=images] a"-IMAGE_SELECTOR = "img[class=image-img]"class PixHostCrawler(Crawler):- SUPPORTED_PATHS: ClassVar[SupportedPaths] = {"Gallery": "/gallery/...", "Image": "/show/..."}+ SUPPORTED_PATHS: ClassVar[SupportedPaths] = {+ "Gallery": "/gallery/<gallery_id>",+ "Image": "/show/<image_id>",+ "Thumbnail": "/thumbs/..",+ }PRIMARY_URL: ClassVar[AbsoluteHttpURL] = PRIMARY_URLUPDATE_UNSUPPORTED: ClassVar[bool] = TrueDOMAIN: ClassVar[str] = "pixhost"FOLDER_DOMAIN: ClassVar[str] = "PixHost"async def fetch(self, scrape_item: ScrapeItem) -> None:- if is_cdn(scrape_item.url):- link = scrape_item.url- scrape_item.url = get_canonical_url(link)- return await self.handle_direct_link(scrape_item, link)- if "gallery" in scrape_item.url.parts:- return await self.gallery(scrape_item)- if "show" in scrape_item.url.parts:- return await self.image(scrape_item)- raise ValueError+ if self.is_thumbnail(scrape_item.url):+ src = _thumbnail_to_src(scrape_item.url)+ scrape_item.url = _thumbnail_to_web_url(scrape_item.url)+ return await self.direct_file(scrape_item, src)++ match scrape_item.url.parts[1:]:+ case ["gallery", gallery_id]:+ return await self.gallery(scrape_item, gallery_id)+ case ["show", _]:+ return await self.image(scrape_item)+ case _:+ raise ValueError@error_handling_wrapper- async def gallery(self, scrape_item: ScrapeItem) -> None:+ async def gallery(self, scrape_item: ScrapeItem, album_id: str) -> None:async with self.request_limiter:- soup: BeautifulSoup = await self.client.get_soup(self.DOMAIN, scrape_item.url)+ soup = await self.client.get_soup(self.DOMAIN, scrape_item.url)- album_id = scrape_item.url.name- title = css.select_one_get_text(soup, GALLERY_TITLE_SELECTOR)+ title = css.select_one_get_text(soup, _SELECTORS.GALLERY_TITLE)title = self.create_title(title, album_id)scrape_item.setup_as_album(title, album_id=album_id)results = await self.get_album_results(album_id)- for thumb, link in self.iter_tags(soup, IMAGES_SELECTOR):- if not thumb:- continue- link = get_canonical_url(link)- src = thumbnail_to_img(thumb)+ for thumb, web_url in self.iter_tags(soup, _SELECTORS.GALLERY_IMAGES):+ assert thumb+ src = _thumbnail_to_src(thumb)if not self.check_album_results(src, results):- new_scrape_item = scrape_item.create_child(link)- await self.handle_direct_link(new_scrape_item, src)+ new_scrape_item = scrape_item.create_child(web_url)+ self.create_task(self.direct_file(new_scrape_item, src))scrape_item.add_children()@error_handling_wrapper@@ -64,41 +70,25 @@ class PixHostCrawler(Crawler):returnasync with self.request_limiter:- soup: BeautifulSoup = await self.client.get_soup(self.DOMAIN, scrape_item.url)+ soup = await self.client.get_soup(self.DOMAIN, scrape_item.url)- link_str: str = css.select_one_get_attr(soup, IMAGE_SELECTOR, "src")+ link_str = css.select_one_get_attr(soup, _SELECTORS.IMAGE, "src")link = self.parse_url(link_str)- await self.handle_direct_link(scrape_item, link)-- @error_handling_wrapper- async def handle_direct_link(self, scrape_item: ScrapeItem, url: AbsoluteHttpURL | None = None) -> None:- link = url or scrape_item.url- if is_thumbnail(link):- link = thumbnail_to_img(link)await self.direct_file(scrape_item, link)--def thumbnail_to_img(url: AbsoluteHttpURL) -> AbsoluteHttpURL:- thumb_server_id: str = url.host.split(".", 1)[0].split("t")[-1]- img_host = f"img{thumb_server_id}.{PRIMARY_URL.host}"- img_url = replace_first_part(url, "images")- return img_url.with_host(img_host)---def replace_first_part(url: AbsoluteHttpURL, new_part: str) -> AbsoluteHttpURL:- new_parts = new_part, *url.parts[1:]- new_path = "/".join(new_parts)- return url.with_path(new_path)+ @classmethod+ def is_thumbnail(cls, url: AbsoluteHttpURL):+ return "thumbs" in url.parts and cls.is_subdomain(url)-def is_thumbnail(url: AbsoluteHttpURL) -> bool:- return "thumbs" in url.parts and is_cdn(url)---def is_cdn(url: AbsoluteHttpURL) -> bool:- return len(url.host.split(".")) > 2+def _thumbnail_to_src(url: AbsoluteHttpURL) -> AbsoluteHttpURL:+ # https://t100.pixhost.to/thumbs/491/538303440_005.jpg -> https://img100.pixhost.to/images/491/538303440_005.jpg+ thumb_server_id = url.host.split(".", 1)[0].split("t")[-1]+ img_host = f"img{thumb_server_id}.{PRIMARY_URL.host}"+ new_path = url.path.replace("/thumbs/", "/images/")+ return url.with_host(img_host).with_path(new_path)-def get_canonical_url(url: AbsoluteHttpURL) -> AbsoluteHttpURL:- show_url = replace_first_part(url, "show")- return show_url.with_host(PRIMARY_URL.host)+def _thumbnail_to_web_url(url: AbsoluteHttpURL) -> AbsoluteHttpURL:+ new_path = url.path.replace("/thumbs/", "/show/")+ return url.with_host(PRIMARY_URL.host).with_path(new_path)