Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: handle old and new URL formats (imagebam) (#1133)

NTFSvolumebf566420c477
diff --git a/cyberdrop_dl/crawlers/imagebam.py b/cyberdrop_dl/crawlers/imagebam.py
index deaf2f69d..1f55efcc6 100644
--- a/cyberdrop_dl/crawlers/imagebam.py
+++ b/cyberdrop_dl/crawlers/imagebam.py
@@ -1,10 +1,10 @@
from __future__ import annotations
+from pathlib import Path
from typing import TYPE_CHECKING, ClassVar
from cyberdrop_dl.crawlers.crawler import Crawler, SupportedPaths
from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.utils import css
from cyberdrop_dl.utils.utilities import error_handling_wrapper
@@ -14,79 +14,111 @@ if TYPE_CHECKING:
from cyberdrop_dl.data_structures.url_objects import ScrapeItem
-GALLERY_TITLE_SELECTOR = "a#gallery-name"
-IMAGES_SELECTOR = "ul.images a.thumbnail"
-IMAGE_SELECTOR = "img.main-image"
-GALLERY_INFO_SELECTOR = "div.view-navigation a:has(i.fas.fa-reply)"
+class Selectors:
+ GALLERY_TITLE = "a#gallery-name"
+ THUMBNAILS = "ul.images a.thumbnail"
+ IMAGE = "img.main-image"
+ GALLERY_INFO = "div.view-navigation a:has(i.fas.fa-reply)"
+ IS_GALLERY = ".card-header:contains('Share this gallery')"
+ NEXT_PAGE = "a.page-link[rel=next]"
+
+
+_SELECTORS = Selectors()
PRIMARY_URL = AbsoluteHttpURL("https://www.imagebam.com/")
class ImageBamCrawler(Crawler):
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
- "Album": "/view/...",
- "Image": "/view/...",
- "Direct links": "",
+ "Gallery": "/gallery/...",
+ "Image": "/image/...",
+ "Gallery or Image": "/view/...",
+ "Thumbnails": "thumbs<x>.imagebam.com/...",
}
PRIMARY_URL: ClassVar[AbsoluteHttpURL] = PRIMARY_URL
DOMAIN: ClassVar[str] = "imagebam"
FOLDER_DOMAIN: ClassVar[str] = "ImageBam"
+ NEXT_PAGE_SELECTOR: ClassVar[str] = _SELECTORS.NEXT_PAGE
async def async_startup(self) -> None:
+ # This skips the "Continue to image" pages.
self.update_cookies({"nsfw_inter": "1"})
async def fetch(self, scrape_item: ScrapeItem) -> None:
- if is_cdn(scrape_item.url):
- scrape_item.url = get_view_url(scrape_item.url)
- if any(part in scrape_item.url.parts for part in ("gallery", "image", "view")):
- return await self.view(scrape_item)
- raise ValueError
+ if "thumbs" in scrape_item.url.host:
+ scrape_item.url = thumbnail_to_img(scrape_item.url)
+ # this is to update the URL in the UI
+ # TODO: Add URL convertion methods as a pre_check step, before assigning them a task_id
+ # Several crawlers do something like this
+ self.manager.task_group.create_task(self.run(scrape_item))
+ return
+
+ match scrape_item.url.parts[1:]:
+ case ["gallery", _]:
+ return await self.gallery(scrape_item)
+ case ["image", _]:
+ return await self.image(scrape_item)
+ case ["view", _]:
+ return await self.view(scrape_item)
+ case _:
+ raise ValueError
@error_handling_wrapper
async def view(self, scrape_item: ScrapeItem) -> None:
+ # view URLs can be either a gallery or a single image.
async with self.request_limiter:
- soup: BeautifulSoup = await self.client.get_soup(self.DOMAIN, scrape_item.url)
- if "Share this gallery" in soup.text:
+ soup = await self.client.get_soup(self.DOMAIN, scrape_item.url)
+ if soup.select_one(_SELECTORS.IS_GALLERY):
return await self.gallery(scrape_item, soup)
await self.image(scrape_item, soup)
@error_handling_wrapper
- async def gallery(self, scrape_item: ScrapeItem, soup: BeautifulSoup) -> None:
- gallery_name = css.select_one_get_text(soup, GALLERY_TITLE_SELECTOR)
+ async def gallery(self, scrape_item: ScrapeItem, soup: BeautifulSoup | None = None) -> None:
+ if not soup:
+ async with self.request_limiter:
+ soup = await self.client.get_soup(self.DOMAIN, scrape_item.url)
+
+ gallery_name = css.select_one_get_text(soup, _SELECTORS.GALLERY_TITLE)
gallery_id = scrape_item.url.name
title = self.create_title(gallery_name, gallery_id)
scrape_item.setup_as_album(title, album_id=gallery_id)
results = await self.get_album_results(gallery_id)
- for _, new_scrape_item in self.iter_children(scrape_item, soup, IMAGES_SELECTOR):
- if not self.check_album_results(new_scrape_item.url, results):
- self.manager.task_group.create_task(self.image(new_scrape_item, soup))
+ def process_gallery_images(soup: BeautifulSoup) -> None:
+ for _, new_scrape_item in self.iter_children(scrape_item, soup, _SELECTORS.THUMBNAILS):
+ if not self.check_album_results(new_scrape_item.url, results):
+ self.manager.task_group.create_task(self.image(new_scrape_item))
+
+ process_gallery_images(soup)
+
+ while next_page := css.select_one_get_attr_or_none(soup, _SELECTORS.NEXT_PAGE, "href"):
+ async with self.request_limiter:
+ soup = await self.client.get_soup(self.DOMAIN, self.parse_url(next_page))
+ process_gallery_images(soup)
@error_handling_wrapper
- async def image(self, scrape_item: ScrapeItem, soup: BeautifulSoup) -> None:
- if await self.check_complete_from_referer(scrape_item):
- return
+ async def image(self, scrape_item: ScrapeItem, soup: BeautifulSoup | None = None) -> None:
+ if not soup:
+ if await self.check_complete_from_referer(scrape_item):
+ return
- image_tag = soup.select_one(IMAGE_SELECTOR)
- if not image_tag:
- raise ScrapeError(422)
+ async with self.request_limiter:
+ soup = await self.client.get_soup(self.DOMAIN, scrape_item.url)
- gallery_info = soup.select_one(GALLERY_INFO_SELECTOR)
- if gallery_info and not scrape_item.album_id:
- gallery_url = self.parse_url(css.get_attr(gallery_info, "href"))
- gallery_id = gallery_url.name
+ image_tag = css.select_one(soup, _SELECTORS.IMAGE)
+ if not scrape_item.album_id and (gallery_info := soup.select_one(_SELECTORS.GALLERY_INFO)):
+ gallery_id = css.get_attr(gallery_info, "href").rsplit("/", 1)[-1]
scrape_item.album_id = gallery_id
- title: str = css.get_attr(image_tag, "alt")
+ title = css.get_attr(image_tag, "alt")
link = self.parse_url(css.get_attr(image_tag, "src"))
filename, ext = self.get_filename_and_ext(link.name)
custom_filename = self.create_custom_filename(title, ext)
await self.handle_file(link, scrape_item, filename, ext, custom_filename=custom_filename)
-def is_cdn(url: AbsoluteHttpURL) -> bool:
- return "imagebam" in url.host.split(".") and "." in url.host.rstrip(".com")
-
-
-def get_view_url(url: AbsoluteHttpURL) -> AbsoluteHttpURL:
- view_id = url.name.rsplit("_", 1)[0]
- return PRIMARY_URL / "view" / view_id
+def thumbnail_to_img(url: AbsoluteHttpURL) -> AbsoluteHttpURL:
+ stem = Path(url.name).stem
+ image_id = stem.removesuffix("_t")
+ if image_id != stem:
+ return PRIMARY_URL / "view" / image_id
+ return PRIMARY_URL / "image" / image_id