Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
scrape high-quality image URLs from data-object in CheveretoCrawler
das10lmf7b8e5e5a236
diff --git a/cyberdrop_dl/crawlers/_chevereto.py b/cyberdrop_dl/crawlers/_chevereto.pyindex f74645ec6..6909c9693 100644--- a/cyberdrop_dl/crawlers/_chevereto.py+++ b/cyberdrop_dl/crawlers/_chevereto.py@@ -1,15 +1,18 @@from __future__ import annotationsimport base64+import urllib.parsefrom typing import TYPE_CHECKING, ClassVar, finalfrom cyberdrop_dl.crawlers.crawler import Crawlerfrom cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURLfrom cyberdrop_dl.exceptions import PasswordProtectedError-from cyberdrop_dl.utils import css, open_graph+from cyberdrop_dl.utils import css, json, open_graphfrom cyberdrop_dl.utils.utilities import error_handling_wrapper, xor_decryptif TYPE_CHECKING:+ from collections.abc import Generator+from bs4 import BeautifulSoupfrom cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL, ScrapeItem@@ -75,7 +78,7 @@ class CheveretoCrawler(Crawler, is_generic=True):case ["images", _, *_]:return await self.direct_file(scrape_item)case [_, "albums"]:- return await self.profile(scrape_item)+ return await self.profile(scrape_item, albums=True)case [_]:return await self.profile(scrape_item)case _:@@ -89,12 +92,6 @@ class CheveretoCrawler(Crawler, is_generic=True):new_name = new_name.replace(trash, ".")return url.with_name(new_name)- @classmethod- def _match_img(cls, url: AbsoluteHttpURL) -> AbsoluteHttpURL | None:- match url.parts[1:]:- case ["img" | "image" as part, image_slug, *_]:- return url.origin() / part / _id(image_slug)-@classmethoddef transform_url(cls, url: AbsoluteHttpURL) -> AbsoluteHttpURL:url = super().transform_url(url)@@ -107,12 +104,19 @@ class CheveretoCrawler(Crawler, is_generic=True):return url@error_handling_wrapper- async def profile(self, scrape_item: ScrapeItem) -> None:+ async def profile(self, scrape_item: ScrapeItem, *, albums: bool = False) -> None:title: str = ""async for soup in self.web_pager(_sort_by_new(scrape_item.url), trim=False):if not title:title = self.create_title(open_graph.title(soup))scrape_item.setup_as_profile(title)++ if albums:+ for _, sub_album in self.iter_children(scrape_item, soup, Selector.ITEM):+ self.create_task(self.run(sub_album))++ return+self._process_page(scrape_item, soup)async def _get_final_album_url(self, url: AbsoluteHttpURL) -> AbsoluteHttpURL:@@ -155,21 +159,14 @@ class CheveretoCrawler(Crawler, is_generic=True):def _process_page(self, scrape_item: ScrapeItem, soup: BeautifulSoup, results: dict[str, int] | None = None) -> None:- for thumb, new_scrape_item in self.iter_children(scrape_item, soup, Selector.ITEM):- if image_url := self._match_img(new_scrape_item.url):- new_scrape_item.url = image_url-- if thumb:- # for images, we can download the file from the thumbnail, skipping an additional request per img- # cons: we won't get the upload date- source = self._thumbnail_to_src(thumb)- if results and self.check_album_results(source, results):- continue+ results = results or {}+ for web_url, src_url in self._get_album_files(soup):+ if self.check_album_results(web_url, results):+ continue- self.create_task(self.direct_file(new_scrape_item, source))- continue-- self.create_task(self.run(new_scrape_item))+ new_scrape_item = scrape_item.create_child(web_url)+ self.create_task(self.direct_file(new_scrape_item, src_url))+ scrape_item.add_children()async def _unlock_password_protected_album(self, scrape_item: ScrapeItem) -> None:if not scrape_item.password:@@ -213,6 +210,14 @@ class CheveretoCrawler(Crawler, is_generic=True):link_str = xor_decrypt(encrypted_url, _DECRYPTION_KEY)return super().parse_url(link_str, relative_to, trim=trim)+ def _get_album_files(self, soup: BeautifulSoup) -> Generator[tuple[AbsoluteHttpURL, AbsoluteHttpURL]]:+ for item in soup.select(".list-item[data-object]"):+ web_url = self.parse_url(css.select(item, "a.image-container", "href"))+ encoded_data = css.get_attr(item, "data-object")+ data = json.loads(urllib.parse.unquote(encoded_data))+ src_url = self.parse_url(data["image"]["url"])+ yield self.transform_url(web_url), src_url+def _is_password_protected(soup: BeautifulSoup) -> bool:return "This content is password protected" in soup.textdiff --git a/cyberdrop_dl/crawlers/imgbb.py b/cyberdrop_dl/crawlers/imgbb.pyindex 1412948f2..80f224c33 100644--- a/cyberdrop_dl/crawlers/imgbb.py+++ b/cyberdrop_dl/crawlers/imgbb.py@@ -40,9 +40,3 @@ class ImgBBCrawler(CheveretoCrawler):return await self.media(scrape_item)case _:raise ValueError-- @classmethod- def _match_img(cls, url: AbsoluteHttpURL) -> AbsoluteHttpURL | None:- match url.parts[1:]:- case [_]:- return urldiff --git a/tests/crawlers/test_cases/imgbb.py b/tests/crawlers/test_cases/imgbb.pyindex 1ffda7c10..17f2c6079 100644--- a/tests/crawlers/test_cases/imgbb.py+++ b/tests/crawlers/test_cases/imgbb.py@@ -36,4 +36,70 @@ TEST_CASES = [],1537,),+ (+ "https://ibb.co/album/DDcTkZ",+ [+ {+ "url": "https://i.ibb.co/xSSVZ3TF/FOISTUDIOS-arna-SET2-16.jpg",+ "filename": "FOISTUDIOS-arna-SET2-16.jpg",+ "referrer": "https://ibb.co/WWWSCsJV",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/3ytgckZy/FOISTUDIOS-arna-SET2-14.jpg",+ "filename": "FOISTUDIOS-arna-SET2-14.jpg",+ "referrer": "https://ibb.co/wND54YmN",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/gF3fc73F/FOISTUDIOS-arna-SET2-3.jpg",+ "filename": "FOISTUDIOS-arna-SET2-3.jpg",+ "referrer": "https://ibb.co/93N1dvN3",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/tPzBWLjm/FOISTUDIOS-arna-SET2-10.jpg",+ "filename": "FOISTUDIOS-arna-SET2-10.jpg",+ "referrer": "https://ibb.co/Z1Sdk24L",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/tTgcj5k9/FOISTUDIOS-arna-SET2-6.jpg",+ "filename": "FOISTUDIOS-arna-SET2-6.jpg",+ "referrer": "https://ibb.co/5gmvpVtP",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/sdMsb0YW/FOISTUDIOS-arna-SET2-7.jpg",+ "filename": "FOISTUDIOS-arna-SET2-7.jpg",+ "referrer": "https://ibb.co/Mx3PZY0B",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/sd5z3Mj5/FOISTUDIOS-arna-SET2-12.jpg",+ "filename": "FOISTUDIOS-arna-SET2-12.jpg",+ "referrer": "https://ibb.co/PvT0CfxT",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/nqMZF2ts/FOISTUDIOS-arna-SET2-17.jpg",+ "filename": "FOISTUDIOS-arna-SET2-17.jpg",+ "referrer": "https://ibb.co/7xt8qcLJ",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/G428GCFC/FOISTUDIOS-arna-SET2-13.jpg",+ "filename": "FOISTUDIOS-arna-SET2-13.jpg",+ "referrer": "https://ibb.co/wFWP8pYp",+ "album_id": "DDcTkZ",+ },+ {+ "url": "https://i.ibb.co/JF0FWGXz/FOISTUDIOS-arna-SET2-9.jpg",+ "filename": "FOISTUDIOS-arna-SET2-9.jpg",+ "referrer": "https://ibb.co/zHjHWBKS",+ "album_id": "DDcTkZ",+ },+ ],+ 10,+ ),]