Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: remove duplicated open graph code

NTFSvolume70290a03a875
diff --git a/cyberdrop_dl/crawlers/archivebate.py b/cyberdrop_dl/crawlers/archivebate.py
index 48d1bd529..b558f1a7d 100644
--- a/cyberdrop_dl/crawlers/archivebate.py
+++ b/cyberdrop_dl/crawlers/archivebate.py
@@ -7,8 +7,8 @@ from aiolimiter import AsyncLimiter
from cyberdrop_dl.crawlers.mixdrop import MixDropCrawler
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, get_og_properties, get_text_between
+from cyberdrop_dl.utils import css, open_graph
+from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_text_between
if TYPE_CHECKING:
from bs4 import BeautifulSoup
@@ -54,10 +54,6 @@ class ArchiveBateCrawler(MixDropCrawler):
# Not supported, video entries are dynamically generated with javascript
# They have an API to request them but it also returns javascript
raise ValueError
- scrape_item.setup_as_profile("")
- async for soup in self.web_pager(scrape_item.url):
- for _, new_scrape_item in self.iter_children(scrape_item, soup, _SELECTORS.PROFILE_VIDEOS):
- self.manager.task_group.create_task(self.run(new_scrape_item))
@error_handling_wrapper
async def video(self, scrape_item: ScrapeItem) -> None:
@@ -78,16 +74,15 @@ class ArchiveBateCrawler(MixDropCrawler):
if "This video has been deleted" in soup.text:
raise ScrapeError(410)
- og_props = get_og_properties(soup)
- date_str: str = get_text_between(og_props.description, "show on", " - ").strip()
- user_name: str = css.select_one_get_text(soup, _SELECTORS.USER_NAME)
- site_name: str = css.select_one_get_text(soup, _SELECTORS.SITE_NAME)
- video_src: str = css.select_one_get_attr(soup, _SELECTORS.VIDEO, "src")
+ description = open_graph.get("description", soup)
+ assert description
+ date_str = get_text_between(description, "show on", " - ").strip()
+ user_name = css.select_one_get_text(soup, _SELECTORS.USER_NAME)
+ site_name = css.select_one_get_text(soup, _SELECTORS.SITE_NAME)
+ video_src = css.select_one_get_attr(soup, _SELECTORS.VIDEO, "src")
title = self.create_title(f"{user_name} [{site_name}]")
scrape_item.setup_as_profile(title)
scrape_item.possible_datetime = self.parse_date(date_str)
- show_title = f"Show on {date_str}"
-
mixdrop_url = self.get_embed_url(self.parse_url(video_src)) # Override domain
if await self.check_complete_from_referer(mixdrop_url):
@@ -98,6 +93,6 @@ class ArchiveBateCrawler(MixDropCrawler):
link = self.create_download_link(soup)
filename, ext = self.get_filename_and_ext(link.name)
- custom_filename = self.create_custom_filename(show_title, ext)
+ custom_filename = self.create_custom_filename(f"Show on {date_str}", ext)
scrape_item.url = mixdrop_url
await self.handle_file(url, scrape_item, filename, ext, custom_filename=custom_filename, debrid_link=link)
diff --git a/cyberdrop_dl/crawlers/bunkrr.py b/cyberdrop_dl/crawlers/bunkrr.py
index 975c7a7f1..b21939979 100644
--- a/cyberdrop_dl/crawlers/bunkrr.py
+++ b/cyberdrop_dl/crawlers/bunkrr.py
@@ -17,10 +17,9 @@ from cyberdrop_dl.constants import FILE_FORMATS
from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths
from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
from cyberdrop_dl.exceptions import DDOSGuardError, NoExtensionError, ScrapeError
-from cyberdrop_dl.utils import css
+from cyberdrop_dl.utils import css, open_graph
from cyberdrop_dl.utils.utilities import (
error_handling_wrapper,
- get_og_properties,
get_text_between,
parse_url,
with_suffix_encoded,
@@ -235,7 +234,8 @@ class BunkrrCrawler(Crawler):
if not scrape_item.possible_datetime and (date_str := soup.select_one(_SELECTORS.ITEM_DATE)):
scrape_item.possible_datetime = self.parse_date(date_str.text.strip())
- title: str = get_og_properties(soup).title # See: https://github.com/jbsparrow/CyberDropDownloader/issues/929
+ title = open_graph.get_title(soup) # See: https://github.com/jbsparrow/CyberDropDownloader/issues/929
+ assert title
await self.handle_direct_link(scrape_item, link, fallback_filename=title)
@error_handling_wrapper
diff --git a/cyberdrop_dl/crawlers/camwhores_dot_tv.py b/cyberdrop_dl/crawlers/camwhores_dot_tv.py
index 136f2b965..17989c433 100644
--- a/cyberdrop_dl/crawlers/camwhores_dot_tv.py
+++ b/cyberdrop_dl/crawlers/camwhores_dot_tv.py
@@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, ClassVar
from cyberdrop_dl.crawlers._kvs import KernelVideoSharingCrawler, Video
from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL, ScrapeItem
-from cyberdrop_dl.utils.utilities import get_og_properties
+from cyberdrop_dl.utils import open_graph
if TYPE_CHECKING:
from bs4 import BeautifulSoup
@@ -35,7 +35,9 @@ class CamwhoresTVCrawler(KernelVideoSharingCrawler):
def get_video_info(self, soup: BeautifulSoup) -> Video:
video = super().get_video_info(soup)
- return video._replace(title=get_og_properties(soup).title)
+ title = open_graph.get_title(soup)
+ assert title
+ return video._replace(title=title)
def parse_url(self, link_str: str, relative_to: AbsoluteHttpURL | None = None, *_) -> AbsoluteHttpURL:
return super().parse_url(link_str, relative_to, trim=False)
diff --git a/cyberdrop_dl/crawlers/missav.py b/cyberdrop_dl/crawlers/missav.py
index a801dc1ae..0f74e3e0f 100644
--- a/cyberdrop_dl/crawlers/missav.py
+++ b/cyberdrop_dl/crawlers/missav.py
@@ -4,8 +4,8 @@ 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.utils import css
-from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_og_properties
+from cyberdrop_dl.utils import css, open_graph
+from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
from bs4 import BeautifulSoup, Tag
@@ -38,13 +38,14 @@ class MissAVCrawler(Crawler):
async with self.request_limiter:
soup: BeautifulSoup = await self.client.get_soup_cffi(self.DOMAIN, scrape_item.url)
- og_props = get_og_properties(soup)
- title = og_props.title.strip()
+ title, date_str = open_graph.get_title(soup), open_graph.get("video_release_date", soup)
+ assert title
+ title = title.strip()
if dvd_code_tag := soup.select_one(DVD_CODE_SELECTOR):
title = fix_title(title, dvd_code_tag)
- if date_str := og_props.get("video_release_date"):
- scrape_item.possible_datetime = self.parse_date(date_str, "%Y-%m-%d")
+ if date_str:
+ scrape_item.possible_datetime = self.parse_iso_date(date_str)
elif date_tag := soup.select_one(DATE_SELECTOR):
scrape_item.possible_datetime = self.parse_date(css.get_attr(date_tag, "datetime"))
else:
diff --git a/cyberdrop_dl/crawlers/pixeldrain.py b/cyberdrop_dl/crawlers/pixeldrain.py
index e36f5c444..3f8868b47 100644
--- a/cyberdrop_dl/crawlers/pixeldrain.py
+++ b/cyberdrop_dl/crawlers/pixeldrain.py
@@ -6,9 +6,9 @@ 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 DownloadError, NoExtensionError, ScrapeError
-from cyberdrop_dl.utils import css
+from cyberdrop_dl.utils import css, open_graph
from cyberdrop_dl.utils.logger import log_debug
-from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_og_properties, get_text_between
+from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_text_between
if TYPE_CHECKING:
from bs4 import BeautifulSoup
@@ -111,21 +111,20 @@ class PixelDrainCrawler(Crawler):
async with self.request_limiter:
soup: BeautifulSoup = await self.client.get_soup(self.DOMAIN, scrape_item.url)
- meta = get_og_properties(soup)
- filename: str = meta.title
- link_str: str = ""
- if "video" in meta.type:
- link_str = meta.video
- elif "image" in meta.type:
- link_str = meta.image
+ og_props = open_graph.parse(soup)
+ title, type_ = og_props.title, og_props.type
+ assert title and type_
+ filename = title
+ link_str: str | None = ""
+ if "video" in type_:
+ link_str = og_props.video
+ elif "image" in type_:
+ link_str = og_props.image
if not link_str or "filesystem" not in link_str:
raise ScrapeError(422)
- js_text: str = css.select_one(soup, JS_SELECTOR).text
- if not js_text:
- raise ScrapeError(422)
-
+ js_text = css.select_one_get_text(soup, JS_SELECTOR)
json_str = get_text_between(js_text, "window.initial_node =", "window.user = ").removesuffix(";")
json_data = json.loads(json_str)
log_debug(json_data)
diff --git a/cyberdrop_dl/utils/open_graph.py b/cyberdrop_dl/utils/open_graph.py
index 1f73c3bbb..2e678de24 100644
--- a/cyberdrop_dl/utils/open_graph.py
+++ b/cyberdrop_dl/utils/open_graph.py
@@ -6,13 +6,13 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
import bs4
+_required_attrs = ("title", "type", "image", "url", "description")
+
@dataclasses.dataclass(frozen=True, slots=True)
class OpenGraph:
"""Open Graph properties. Each attribute corresponds to an OG property."""
- required_attrs = ("title", "type", "image", "url", "description")
-
audio: str | None = None
description: str | None = None
determiner: str | None = None
@@ -27,7 +27,7 @@ class OpenGraph:
video: str | None = None
def is_valid(self) -> bool:
- return all(getattr(self, attr, False) for attr in self.required_attrs)
+ return all(getattr(self, attr, False) for attr in _required_attrs)
_og_fields = dataclasses.fields(OpenGraph)
diff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.py
index 8252d98a4..3a24ffa43 100644
--- a/cyberdrop_dl/utils/utilities.py
+++ b/cyberdrop_dl/utils/utilities.py
@@ -37,7 +37,6 @@ from cyberdrop_dl.exceptions import (
create_error_msg,
get_origin,
)
-from cyberdrop_dl.utils import css
from cyberdrop_dl.utils.logger import log, log_debug, log_spacer, log_with_color
if TYPE_CHECKING:
@@ -70,24 +69,6 @@ class Dataclass(Protocol):
__dataclass_fields__: ClassVar[dict]
-class OGProperties(dict[str, str]):
- """Open Graph properties. Each attribute corresponds to an OG property."""
-
- title: str
- description: str
- image: str
- url: str
- type: str
- site_name: str
- locale: str
- determiner: str
- audio: str
- video: str
-
- def __getattr__(self, name) -> str | None:
- return self.get(name, None)
-
-
def error_handling_wrapper(
func: Callable[Concatenate[CrawerOrDownloader, Origin, P], R | Coroutine[None, None, R]],
) -> Callable[Concatenate[CrawerOrDownloader, Origin, P], Coroutine[None, None, R | None]]:
@@ -533,17 +514,6 @@ async def get_soup_no_error(response: CurlResponse | AnyResponse) -> BeautifulSo
return BeautifulSoup(content, "html.parser")
-def get_og_properties(soup: BeautifulSoup) -> OGProperties:
- """Extracts Open Graph properties (og properties) from soup."""
- og_properties = OGProperties()
-
- for meta in soup.select('meta[property^="og:"]'):
- property_name = css.get_attr(meta, "property").replace("og:", "").replace(":", "_")
- og_properties[property_name] = css.get_attr(meta, "content")
-
- return og_properties
-
-
def get_size_or_none(path: Path) -> int | None:
"""Checks if this is a file and returns its size with a single system call.