Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add vipergirls support (#1090)

NTFSvolume743b42d5c784
diff --git a/cyberdrop_dl/crawlers/__init__.py b/cyberdrop_dl/crawlers/__init__.py
index 4baaf0637..f8e6a04df 100644
--- a/cyberdrop_dl/crawlers/__init__.py
+++ b/cyberdrop_dl/crawlers/__init__.py
@@ -90,6 +90,7 @@ from .tokyomotion import TokioMotionCrawler
from .toonily import ToonilyCrawler
from .twitter_images import TwimgCrawler
from .twpornstars import TwPornstarsCrawler
+from .vbulletin import ViperGirlsCrawler # TODO: import all vBulletin crawlers as a set
from .vipr_dot_im import ViprImCrawler
from .wetransfer import WeTransferCrawler
from .wordpress import WP_CRAWLERS, WP_GENERIC_CRAWLERS
diff --git a/cyberdrop_dl/crawlers/vbulletin/__init__.py b/cyberdrop_dl/crawlers/vbulletin/__init__.py
new file mode 100644
index 000000000..42498e4db
--- /dev/null
+++ b/cyberdrop_dl/crawlers/vbulletin/__init__.py
@@ -0,0 +1,7 @@
+from ._vbulletin import vBulletinCrawler
+from .vipergirls import ViperGirlsCrawler
+
+VBULLETIN_CRAWLERS: set[type[vBulletinCrawler]] = {ViperGirlsCrawler}
+
+VBULLETIN_CRAWLERS_MAP = {c.__name__: c for c in VBULLETIN_CRAWLERS}
+__all__ = list(VBULLETIN_CRAWLERS_MAP.keys()) # type: ignore[reportUnsupportedDunderAll]
diff --git a/cyberdrop_dl/crawlers/vbulletin/_vbulletin.py b/cyberdrop_dl/crawlers/vbulletin/_vbulletin.py
new file mode 100644
index 000000000..5ff7e8bcf
--- /dev/null
+++ b/cyberdrop_dl/crawlers/vbulletin/_vbulletin.py
@@ -0,0 +1,139 @@
+from __future__ import annotations
+
+import dataclasses
+import itertools
+from typing import TYPE_CHECKING, ClassVar
+from xml.etree import ElementTree
+
+from cyberdrop_dl.crawlers.xenforo.xenforo import Thread, XenforoCrawler
+from cyberdrop_dl.exceptions import MaxChildrenError, ScrapeError
+
+if TYPE_CHECKING:
+ from collections.abc import Iterable
+
+ from cyberdrop_dl.crawlers.crawler import SupportedPaths
+ from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL, ScrapeItem
+
+N_POSTS_PER_PAGE = 15
+
+
[email protected](frozen=True, slots=True, order=True)
+class Post:
+ id: int
+ title: str
+ xml: ElementTree.Element[str]
+
+ @staticmethod
+ def new(element: ElementTree.Element[str]) -> Post:
+ title, id_ = element.attrib["title"], int(element.attrib["id"])
+ return Post(id_, title, element)
+
+ @property
+ def images(self) -> Iterable[str]:
+ return (image.attrib["main_url"] for image in self.xml.iter("image"))
+
+
+# TODO: make a super class of Xenforo an inherit from that instead of Xenforo itself
+class vBulletinCrawler(XenforoCrawler, is_abc=True): # noqa: N801
+ # TODO: Make this crawler more general, potentially scraping the actual html of a page like xenforo
+ # Current limitations
+ # 1. It can't get the content (text) of the actual post. (It's not included in the API response of most sites)
+ # 2. Must vBulletin sites have the API disabled
+ # 3. It has no date information
+ SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
+ "Threads": (
+ "/threads/<thread_name>",
+ "/posts/<post_id>",
+ "/goto/<post_id>",
+ ),
+ }
+ VBULLETIN_LOGIN_COOKIE_NAME: ClassVar = ""
+ VBULLETIN_THREAD_QUERY_PARAM: ClassVar = "t"
+ VBULLETIN_POST_QUERY_PARAM: ClassVar = "p"
+ VBULLETIN_API_ENDPOINT: ClassVar[AbsoluteHttpURL]
+
+ def __init_subclass__(cls, **kwargs) -> None:
+ super().__init_subclass__(**kwargs)
+ REQUIRED_FIELDS = ("VBULLETIN_LOGIN_COOKIE_NAME", "VBULLETIN_API_ENDPOINT")
+ for field_name in REQUIRED_FIELDS:
+ assert getattr(cls, field_name, None), f"Subclass {cls.__name__} must override: {field_name}"
+ # TODO: Use the same name for these classvars across all crawlers
+ cls.XF_USER_COOKIE_NAME = cls.VBULLETIN_LOGIN_COOKIE_NAME
+ cls.XF_PAGE_URL_PART_NAME = "page"
+ cls.XF_POST_URL_PART_NAME = "post"
+
+ async def async_startup(self) -> None:
+ if not self.logged_in:
+ login_url = self.PRIMARY_URL / "login.php"
+ await self.login_setup(login_url)
+
+ self.register_cache_filter(self.PRIMARY_URL, lambda _: True)
+
+ async def check_login_with_request(self, *_) -> tuple[str, bool]:
+ # TODO: Support login
+ return "", False
+
+ async def fetch(self, scrape_item: ScrapeItem) -> None:
+ # TODO: Handle more URLs
+ if self.login_required and not self.logged_in:
+ return
+ await self.fetch_thread(scrape_item)
+
+ async def process_thread(self, scrape_item: ScrapeItem, thread: Thread) -> None:
+ title: str = ""
+ if thread.post_id and self.scrape_single_forum_post:
+ api_url = self.VBULLETIN_API_ENDPOINT.with_query({self.VBULLETIN_POST_QUERY_PARAM: str(thread.post_id)})
+ else:
+ api_url = self.VBULLETIN_API_ENDPOINT.with_query({self.VBULLETIN_THREAD_QUERY_PARAM: str(thread.id)})
+
+ root_xml = await self.get_xml(api_url)
+ if (thread_element := root_xml.find("thread")) is not None:
+ title = self.create_title(thread_element.attrib["title"], thread_id=thread.id)
+ scrape_item.setup_as_forum(title)
+ else:
+ raise ScrapeError(422)
+
+ await self.process_posts(scrape_item, thread, root_xml)
+
+ async def process_posts(self, scrape_item: ScrapeItem, thread: Thread, root_xml: ElementTree.Element[str]) -> None:
+ if thread.page:
+ posts = itertools.islice(root_xml.iter("post"), (thread.page - 1) * N_POSTS_PER_PAGE)
+ else:
+ posts = root_xml.iter("post")
+
+ last_post_id = thread.post_id
+ for element in posts:
+ post = Post.new(element)
+ if thread.post_id and thread.post_id > post.id:
+ continue
+ new_scrape_item = scrape_item.create_child(
+ thread.url.update_query({self.VBULLETIN_POST_QUERY_PARAM: str(post.id)})
+ )
+ await self.post(new_scrape_item, post)
+ last_post_id = post.id
+ try:
+ scrape_item.add_children()
+ except MaxChildrenError:
+ break
+
+ if last_post_id:
+ last_post_url = thread.url.update_query({self.VBULLETIN_POST_QUERY_PARAM: str(last_post_id)})
+ await self.write_last_forum_post(thread.url, last_post_url)
+
+ async def post(self, scrape_item: ScrapeItem, post: Post) -> None:
+ title = self.create_separate_post_title(post.title, str(post.id), None)
+ scrape_item.setup_as_post(title)
+ for image in post.images:
+ new_scrap_item = scrape_item.create_child(self.parse_url(image))
+ self.handle_external_links(new_scrap_item)
+ scrape_item.add_children()
+
+ async def get_xml(self, url: AbsoluteHttpURL) -> ElementTree.Element[str]:
+ async with self.request_limiter:
+ text = await self.client.get_text(self.DOMAIN, url)
+ root_xml = ElementTree.XML(text)
+ if error := root_xml.find("error"):
+ details = error.attrib["details"]
+ error_code = 403 if error.attrib["type"] == "permissions" and "unknown" not in details.casefold() else 422
+ raise ScrapeError(error_code, message=details)
+ return root_xml
diff --git a/cyberdrop_dl/crawlers/vbulletin/vipergirls.py b/cyberdrop_dl/crawlers/vbulletin/vipergirls.py
new file mode 100644
index 000000000..d5819590b
--- /dev/null
+++ b/cyberdrop_dl/crawlers/vbulletin/vipergirls.py
@@ -0,0 +1,22 @@
+from __future__ import annotations
+
+from typing import ClassVar
+
+from aiolimiter import AsyncLimiter
+
+from cyberdrop_dl.crawlers.vbulletin._vbulletin import vBulletinCrawler
+from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
+
+
+class ViperGirlsCrawler(vBulletinCrawler):
+ login_required = False
+ VBULLETIN_LOGIN_COOKIE_NAME = "vg_password"
+ PRIMARY_URL = AbsoluteHttpURL("https://vipergirls.to")
+ DOMAIN = "vipergirls.to"
+ FOLDER_DOMAIN = "ViperGirls"
+ SUPPORTED_DOMAINS: ClassVar = "viper.click", "vipergirls.to"
+ VBULLETIN_API_ENDPOINT: ClassVar = AbsoluteHttpURL("https://viper.click/vr.php")
+
+ def __post_init__(self) -> None:
+ super().__post_init__()
+ self.request_limiter = AsyncLimiter(4, 1)
diff --git a/cyberdrop_dl/crawlers/xenforo/xenforo.py b/cyberdrop_dl/crawlers/xenforo/xenforo.py
index 02a12bc03..871d18ecf 100644
--- a/cyberdrop_dl/crawlers/xenforo/xenforo.py
+++ b/cyberdrop_dl/crawlers/xenforo/xenforo.py
@@ -150,6 +150,9 @@ class XenforoCrawler(ForumCrawler, is_abc=True):
if is_confirmation_link(scrape_item.url):
return await self.follow_confirmation_link(scrape_item)
+ await self.fetch_thread(scrape_item)
+
+ async def fetch_thread(self, scrape_item: ScrapeItem) -> None:
thread_part_index = len(self.PRIMARY_URL.parts)
match scrape_item.url.parts[thread_part_index:]:
case [thread_part, thread_name_and_id, *_] if thread_part in KNOWN_THREAD_PART_NAMES:
@@ -200,8 +203,11 @@ class XenforoCrawler(ForumCrawler, is_abc=True):
msg = "`--scrape-single-forum-post` is `True`, but the provided URL has no post id"
raise ScrapeError("User Error", msg)
- title: str = ""
self.scraped_threads.add(thread.url)
+ await self.process_thread(scrape_item, thread)
+
+ async def process_thread(self, scrape_item: ScrapeItem, thread: Thread) -> None:
+ title: str = ""
last_post_url = thread.url
async for soup in self.thread_pager(scrape_item):
if not title:
diff --git a/tests/crawlers/test_vbulletin.py b/tests/crawlers/test_vbulletin.py
new file mode 100644
index 000000000..7a8fa7eb1
--- /dev/null
+++ b/tests/crawlers/test_vbulletin.py
@@ -0,0 +1,40 @@
+from __future__ import annotations
+
+import pytest
+
+from cyberdrop_dl.crawlers.xenforo import xenforo
+from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
+
+
+ ("url", "thread_name_and_id", "result", "canonical_url"),
+ [
+ [
+ "https://vipergirls.to/threads/2783401-Jodie-Comer/page4",
+ "2783401-Jodie-Comer",
+ (
+ 2783401,
+ "Jodie-Comer",
+ 4,
+ None,
+ ),
+ "https://vipergirls.to/threads/2783401-Jodie-Comer",
+ ],
+ [
+ "https://vipergirls.to/threads/9046167-Lacey-Evans/page11?p=222377716&viewfull=1#post222377716",
+ "9046167-Lacey-Evans",
+ (
+ 9046167,
+ "Lacey-Evans",
+ 11,
+ 222377716,
+ ),
+ "https://vipergirls.to/threads/9046167-Lacey-Evans",
+ ],
+ ],
+)
+def test_parse_thread(url: str, thread_name_and_id: str, result: tuple[int, str, int, int], canonical_url: str) -> None:
+ url_, canonical_url_ = AbsoluteHttpURL(url), AbsoluteHttpURL(canonical_url)
+ result_ = xenforo.Thread(*result, canonical_url_)
+ parsed = xenforo.parse_thread(url_, thread_name_and_id, "page", "post")
+ assert result_ == parsed