Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add support indirect links (routed via api) (saint) (#459)

NTFSvolume37f27caad786
diff --git a/cyberdrop_dl/scraper/crawlers/saint_crawler.py b/cyberdrop_dl/scraper/crawlers/saint_crawler.py
index 12dec2713..2e85223e6 100644
--- a/cyberdrop_dl/scraper/crawlers/saint_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/saint_crawler.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import base64
import re
from typing import TYPE_CHECKING
@@ -7,7 +8,8 @@ from aiolimiter import AsyncLimiter
from yarl import URL
from cyberdrop_dl.clients.errors import ScrapeError
-from cyberdrop_dl.scraper.crawler import Crawler
+from cyberdrop_dl.scraper.crawler import Crawler, create_task_id
+from cyberdrop_dl.utils.data_enums_classes.url_objects import FILE_HOST_ALBUM, ScrapeItem
from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_filename_and_ext
if TYPE_CHECKING:
@@ -16,11 +18,10 @@ if TYPE_CHECKING:
from bs4 import BeautifulSoup
from cyberdrop_dl.managers.manager import Manager
- from cyberdrop_dl.utils.data_enums_classes.url_objects import ScrapeItem
class SaintCrawler(Crawler):
- primary_base_domain = URL("https://saint2.su")
+ primary_base_domain = URL("https://saint2.su/")
def __init__(self, manager: Manager) -> None:
super().__init__(manager, "saint", "Saint")
@@ -28,18 +29,18 @@ class SaintCrawler(Crawler):
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
+ @create_task_id
async def fetch(self, scrape_item: ScrapeItem) -> None:
"""Determines where to send the scrape item based on the url."""
- task_id = self.scraping_progress.add_task(scrape_item.url)
scrape_item.url = self.primary_base_domain.with_path(scrape_item.url.path)
if "a" in scrape_item.url.parts:
await self.album(scrape_item)
+ elif "embed" in scrape_item.url.parts:
+ await self.embed(scrape_item)
else:
await self.video(scrape_item)
- self.scraping_progress.remove_task(task_id)
-
@error_handling_wrapper
async def album(self, scrape_item: ScrapeItem) -> None:
"""Scrapes an album."""
@@ -47,6 +48,7 @@ class SaintCrawler(Crawler):
results = await self.get_album_results(album_id)
scrape_item.album_id = album_id
scrape_item.part_of_album = True
+ scrape_item.set_type(FILE_HOST_ALBUM, self.manager)
async with self.request_limiter:
soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url, origin=scrape_item)
@@ -65,10 +67,11 @@ class SaintCrawler(Crawler):
filename, ext = get_filename_and_ext(link.name)
if not self.check_album_results(link, results):
await self.handle_file(link, scrape_item, filename, ext)
+ scrape_item.add_children()
@error_handling_wrapper
- async def video(self, scrape_item: ScrapeItem) -> None:
- """Scrapes a video page."""
+ async def embed(self, scrape_item: ScrapeItem) -> None:
+ """Scrapes an embeded video page."""
if await self.check_complete_from_referer(scrape_item):
return
@@ -80,3 +83,22 @@ class SaintCrawler(Crawler):
raise ScrapeError(422, "Couldn't find video source", origin=scrape_item) from None
filename, ext = get_filename_and_ext(link.name)
await self.handle_file(link, scrape_item, filename, ext)
+
+ @error_handling_wrapper
+ async def video(self, scrape_item: ScrapeItem) -> None:
+ """Scrapes a video page."""
+ async with self.request_limiter:
+ soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url, origin=scrape_item)
+ try:
+ api_link = URL(soup.select_one("a:contains('Download Video')").get("href"))
+ link = get_url_from_base64(api_link)
+ except AttributeError:
+ raise ScrapeError(422, "Couldn't find video source", origin=scrape_item) from None
+ filename, ext = get_filename_and_ext(link.name)
+ await self.handle_file(link, scrape_item, filename, ext)
+
+
+def get_url_from_base64(link: URL) -> URL:
+ base64_str = link.query.get("file")
+ filename_decoded = base64.b64decode(base64_str).decode("utf-8")
+ return URL("https://some_cdn.saint2.cr/videos").with_host(link.host) / filename_decoded