Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add photos and galleries support to dirtyship (#1025)

Barbarella66666661d6fadb61510
diff --git a/cyberdrop_dl/crawlers/dirtyship.py b/cyberdrop_dl/crawlers/dirtyship.py
index 9b0dfd186..a6d1cb0b5 100644
--- a/cyberdrop_dl/crawlers/dirtyship.py
+++ b/cyberdrop_dl/crawlers/dirtyship.py
@@ -16,9 +16,19 @@ if TYPE_CHECKING:
from cyberdrop_dl.managers.manager import Manager
-VIDEO_SELECTOR = "video#fp-video-0 > source"
-FLOWPLAYER_VIDEO_SELECTOR = "div.freedomplayer"
-PLAYLIST_ITEM_SELECTOR = "li.thumi > a"
+class Selectors:
+ VIDEO = "video#fp-video-0 > source"
+ FLOWPLAYER_VIDEO = "div.freedomplayer"
+ PLAYLIST_ITEM = "li.thumi > a"
+ GALLERY_TITLE = "div#album p[style='text-align: center;']"
+ GALLERY_ALTERNATIVE_TITLE = "h1.singletitle"
+ GALLERY_THUMBNAILS = "div.gallery_grid img.gallery-img"
+ GALLERY_ALTERNATIVE_THUMBNAILS = "div#gallery-1 img"
+ GALLERY_DECODING_ASYNC = "div#album img[decoding='async']"
+ SINGLE_PHOTO = "div.resolutions a"
+
+
+_SELECTORS = Selectors()
class Format(NamedTuple):
@@ -40,8 +50,54 @@ class DirtyShipCrawler(Crawler):
"""Determines where to send the scrape item based on the url."""
if any(p in scrape_item.url.parts for p in ("tag", "category")):
return await self.playlist(scrape_item)
+ if "gallery" in scrape_item.url.parts:
+ if len(scrape_item.url.parts) >= 4:
+ return await self.photo(scrape_item)
+ else:
+ return await self.gallery(scrape_item)
return await self.video(scrape_item)
+ @error_handling_wrapper
+ async def photo(self, scrape_item: ScrapeItem) -> None:
+ if await self.check_complete_from_referer(scrape_item):
+ return
+ if not scrape_item.url.suffix == ".jpg":
+ async with self.request_limiter:
+ soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url)
+ url = next(
+ self.parse_url(a["href"]) for a in soup.select(_SELECTORS.SINGLE_PHOTO) if "full" in a.get_text()
+ )
+ else:
+ url = scrape_item.url
+ filename, ext = self.get_filename_and_ext(url.name)
+ await self.handle_file(url, scrape_item, filename, ext)
+
+ @error_handling_wrapper
+ async def gallery(self, scrape_item: ScrapeItem) -> None:
+ title: str = ""
+ async for soup in self.web_pager(scrape_item.url):
+ if not title:
+ title_tag = soup.select_one(_SELECTORS.GALLERY_TITLE) or soup.select_one(
+ _SELECTORS.GALLERY_ALTERNATIVE_TITLE
+ )
+ title: str = title_tag.get_text(strip=True)
+ title = self.create_title(title)
+ scrape_item.setup_as_album(title)
+
+ thumbnails = (
+ soup.select(_SELECTORS.GALLERY_THUMBNAILS)
+ or soup.select(_SELECTORS.GALLERY_ALTERNATIVE_THUMBNAILS)
+ or soup.select(_SELECTORS.GALLERY_DECODING_ASYNC)
+ )
+
+ for img in thumbnails:
+ url = img["src"] if img.get("decoding") == "async" else get_highest_resolution_picture(img["srcset"])
+ if not url:
+ raise ScrapeError(404)
+ url = self.parse_url(url)
+ filename, ext = self.get_filename_and_ext(url.name)
+ await self.handle_file(url, scrape_item, filename, ext)
+
@error_handling_wrapper
async def playlist(self, scrape_item: ScrapeItem) -> None:
title: str = ""
@@ -52,7 +108,7 @@ class DirtyShipCrawler(Crawler):
title = self.create_title(title)
scrape_item.setup_as_album(title)
- for _, new_scrape_item in self.iter_children(scrape_item, soup, PLAYLIST_ITEM_SELECTOR):
+ for _, new_scrape_item in self.iter_children(scrape_item, soup, _SELECTORS.PLAYLIST_ITEM):
self.manager.task_group.create_task(self.run(new_scrape_item))
@error_handling_wrapper
@@ -62,7 +118,7 @@ class DirtyShipCrawler(Crawler):
title: str = soup.select_one("title").text # type: ignore
title = title.split(" - DirtyShip")[0]
- videos = soup.select(VIDEO_SELECTOR)
+ videos = soup.select(_SELECTORS.VIDEO)
def get_formats():
for video in videos:
@@ -88,7 +144,7 @@ class DirtyShipCrawler(Crawler):
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
def get_flowplayer_sources(self, soup: BeautifulSoup) -> set[Format]:
- flow_player = soup.select_one(FLOWPLAYER_VIDEO_SELECTOR)
+ flow_player = soup.select_one(_SELECTORS.FLOWPLAYER_VIDEO)
data_item: str = flow_player.get("data-item") if flow_player else None # type: ignore
if not data_item:
return set()
@@ -96,3 +152,23 @@ class DirtyShipCrawler(Crawler):
json_data = json.loads(data_item)
sources = json_data["sources"]
return {Format(None, self.parse_url(s["src"])) for s in sources}
+
+
+"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
+
+
+def get_highest_resolution_picture(srcset: str) -> str | None:
+ """
+ Parses a srcset string and returns the URL with the highest resolution (width).
+ """
+ candidates = []
+ for item in srcset.split(","):
+ parts = item.strip().split()
+ if len(parts) == 2:
+ url, width = parts
+ try:
+ width = int(width.rstrip("w"))
+ candidates.append((width, url))
+ except ValueError:
+ continue
+ return max(candidates)[1] if candidates else None