Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
feat: add google photos support (#1589)
NTFSvolume3c5ab5b17749
diff --git a/cyberdrop_dl/crawlers/__init__.py b/cyberdrop_dl/crawlers/__init__.pyindex d362284f8..a0dd75dcb 100644--- a/cyberdrop_dl/crawlers/__init__.py+++ b/cyberdrop_dl/crawlers/__init__.py@@ -43,6 +43,7 @@ from .generic import GenericCrawlerfrom .girlsreleased import GirlsReleasedCrawlerfrom .gofile import GoFileCrawlerfrom .google_drive import GoogleDriveCrawler+from .google_photos import GooglePhotosCrawlerfrom .hclips import HClipsCrawlerfrom .hdzog import HDZogCrawlerfrom .hianime import HiAnimeCrawlerdiff --git a/cyberdrop_dl/crawlers/google_photos.py b/cyberdrop_dl/crawlers/google_photos.pynew file mode 100644index 000000000..fcb86d77b--- /dev/null+++ b/cyberdrop_dl/crawlers/google_photos.py@@ -0,0 +1,113 @@+from __future__ import annotations++import json+import mimetypes+from dataclasses import dataclass+from typing import TYPE_CHECKING, Any, ClassVar++from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths, auto_task_id+from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL+from cyberdrop_dl.utils import css+from cyberdrop_dl.utils.utilities import error_handling_wrapper++if TYPE_CHECKING:+ from collections.abc import Generator++ from bs4 import BeautifulSoup++ from cyberdrop_dl.data_structures.url_objects import ScrapeItem+++@dataclass(slots=True, frozen=True)+class Image:+ base_url: str+ width: int+ height: int+ display_id: str+ id: str+ date: int++ @staticmethod+ def parse(protobuf: list[Any]) -> Image:+ return Image(+ *protobuf[1][0:3],+ display_id=protobuf[0],+ date=protobuf[2] // 1000,+ id=protobuf[3],+ )+++class GooglePhotosCrawler(Crawler):+ SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = "photos.app.goo.gl", "photos.google.com"+ SUPPORTED_PATHS: ClassVar[SupportedPaths] = {+ "Album": "/share/<album_id>",+ "Photo": "/album/<album_id>/photo/<photo_id>",+ "**NOTE**": "Only downloads 'optimized' images, NOT original quality",+ "**NOTE**2": "Can NOT download videos",+ }+ PRIMARY_URL: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://photos.google.com")+ DOMAIN: ClassVar[str] = "photos.google"+ FOLDER_DOMAIN: ClassVar[str] = "GooglePhotos"++ async def fetch(self, scrape_item: ScrapeItem) -> None:+ if scrape_item.url.host == "photos.app.goo.gl":+ return await self.follow_redirect(scrape_item)++ match scrape_item.url.parts[1:]:+ case ["album" | "share" as type_, album_id, "photo", photo_id, *_]:+ return await self.album(scrape_item, type_, album_id, photo_id)+ case ["album" | "share" as type_, album_id]:+ return await self.album(scrape_item, type_, album_id)+ case _:+ raise ValueError++ @error_handling_wrapper+ async def album(self, scrape_item: ScrapeItem, type_: str, album_id: str, photo_id: str | None = None) -> None:+ album_url = (self.PRIMARY_URL / type_ / album_id).with_query(scrape_item.url.query)+ soup = await self.request_soup(album_url)+ album_name, images = _parse_album(soup)+ if album_name:+ title = self.create_title(album_name, album_id)+ scrape_item.setup_as_album(title, album_id=album_id)++ for idx, image in enumerate(images, 1):+ if photo_id and photo_id != image.display_id:+ continue++ web_url = self.PRIMARY_URL / type_ / album_id / "photo" / image.display_id+ new_scrape_item = scrape_item.create_child(web_url)+ self.create_task(self._image(new_scrape_item, image, idx))+ scrape_item.add_children()+ if photo_id:+ break++ @auto_task_id+ @error_handling_wrapper+ async def _image(self, scrape_item: ScrapeItem, image: Image, idx: int) -> None:+ link = self.parse_url(image.base_url + f"=w{image.width}-h{image.height}")+ scrape_item.possible_datetime = image.date++ async with self.request(link) as resp:+ ext = mimetypes.guess_extension(resp.content_type)++ assert ext+ filename, ext = self.get_filename_and_ext(image.id + ext)+ custom_filename = f"{str(idx).zfill(3)} - {filename}"+ await self.handle_file(+ scrape_item.url,+ scrape_item,+ filename,+ ext,+ metadata=image,+ custom_filename=custom_filename,+ debrid_link=link,+ )+++def _parse_album(soup: BeautifulSoup) -> tuple[str | None, Generator[Image]]:+ data = css.select_text(soup, "script[class='ds:1']:-soup-contains(AF_initDataCallback)")+ start, end = data.find("["), data.rfind("]") + 1+ protobuf: list[Any] = json.loads(data[start:end])+ name: str | None = protobuf[3][1] or None+ images = (Image.parse(img) for img in protobuf[1])+ return name, imagesdiff --git a/tests/crawlers/test_cases/google_photos.py b/tests/crawlers/test_cases/google_photos.pynew file mode 100644index 000000000..de82719db--- /dev/null+++ b/tests/crawlers/test_cases/google_photos.py@@ -0,0 +1,28 @@+DOMAIN = "photos.google"+TEST_CASES = [+ (+ "https://photos.google.com/share/AF1QipPc_n2CZWqhYO2A9CJ4hYySaQZ5tCyMxWKC9r7ZtVrt-X87KJNe97-SQ4i23Jx4FA?key=b1RTMlhnRU51c0xPQzNBcDRxbXVGQ2RQZ0hKMzlR",+ [+ {+ "url": "https://photos.google.com/share/AF1QipPc_n2CZWqhYO2A9CJ4hYySaQZ5tCyMxWKC9r7ZtVrt-X87KJNe97-SQ4i23Jx4FA/photo/AF1QipPc1xpdMsAEsTCZD7InU5evF61tLLdRJrV_vzqr",+ "filename": "001 - XeRNOAQHmS7jAXvseEuuMIczOWY.jpg",+ "original_filename": "XeRNOAQHmS7jAXvseEuuMIczOWY.jpg",+ "download_folder": "re:Loose Files (GooglePhotos)",+ "referer": "https://photos.google.com/share/AF1QipPc_n2CZWqhYO2A9CJ4hYySaQZ5tCyMxWKC9r7ZtVrt-X87KJNe97-SQ4i23Jx4FA/photo/AF1QipPc1xpdMsAEsTCZD7InU5evF61tLLdRJrV_vzqr",+ "album_id": None,+ "datetime": 1737761657,+ }+ ],+ ),+ (+ "https://photos.google.com/share/AF1QipO-vRh4nYTvRcUSi-KF30ErgYMSpzsDgb_BTAp8VqZnrA2egJVHq--6i4sqZ1Ek7w?key=UDJnQk80bHRqeEZ4MHZRTUVNWFFXeExqRTY2dXBR",+ [+ {+ "url": "ANY",+ "download_folder": "re:OPR August 2025 Painting Contest - Solo (GooglePhotos)",+ "album_id": "AF1QipO-vRh4nYTvRcUSi-KF30ErgYMSpzsDgb_BTAp8VqZnrA2egJVHq--6i4sqZ1Ek7w",+ }+ ],+ 12,+ ),+]