Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use new syntax (eporner) (#921)

NTFSvolumeffba21bf68e8
diff --git a/cyberdrop_dl/crawlers/eporner.py b/cyberdrop_dl/crawlers/eporner.py
index c7a3c6264..1a8e2a285 100644
--- a/cyberdrop_dl/crawlers/eporner.py
+++ b/cyberdrop_dl/crawlers/eporner.py
@@ -1,18 +1,13 @@
from __future__ import annotations
-import calendar
-from datetime import datetime
-from functools import cached_property
from typing import TYPE_CHECKING, NamedTuple
-from pydantic import ByteSize
from yarl import URL
from cyberdrop_dl.clients.errors import ScrapeError
from cyberdrop_dl.crawlers.crawler import Crawler, create_task_id
from cyberdrop_dl.utils import javascript
from cyberdrop_dl.utils.data_enums_classes.url_objects import ScrapeItem
-from cyberdrop_dl.utils.logger import log_debug
from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
@@ -22,26 +17,27 @@ if TYPE_CHECKING:
from cyberdrop_dl.utils.data_enums_classes.url_objects import ScrapeItem
-RESOLUTIONS = ["4k", "2160p", "1440p", "1080p", "720p", "480p", "360p", "240p"] # best to worst
-ALLOW_AV1 = True
-
+class Selectors:
+ DOWNLOADS = "div#hd-porn-dload > div.dloaddivcol"
+ PHOTO = "div#gridphoto > a.photohref"
+ VIDEO = "div[id^='vf'] div.mbcontent a"
+ NEXT_PAGE = "div.numlist2 a.nmnext"
+ H264 = "span.download-h264 > a"
+ AV1 = "span.download-av1 > a"
+ PROFILE_GALLERY = "div[id^='pf'] a"
+ PROFILE_PLAYLIST = "div.streameventsday.showAll > div#pl > a"
+ DATE_JS = "main script:contains('uploadDate')"
+ GALLERY_TITLE = "div#galleryheader > h1"
-DOWNLOADS_SELECTOR = "div#hd-porn-dload > div.dloaddivcol"
-PHOTO_SELECTOR = "div#gridphoto > a.photohref"
-VIDEO_SELECTOR = "div[id^='vf'] div.mbcontent a"
-NEXT_PAGE_SELECTOR = "div.numlist2 a.nmnext"
-H264_SELECTOR = "span.download-h264 > a"
-AV1_SELECTOR = "span.download-av1 > a"
-PROFILE_GALLERY_SELECTOR = "div[id^='pf'] a"
-PROFILE_PLAYLIST_SELECTOR = "div.streameventsday.showAll > div#pl > a"
-DATE_JS_SELECTOR = "main script:contains('uploadDate')"
-GALLERY_TITLE_SELECTOR = "div#galleryheader > h1"
+_SELECTORS = Selectors()
+RESOLUTIONS = ["4k", "2160p", "1440p", "1080p", "720p", "480p", "360p", "240p"] # best to worst
+ALLOW_AV1 = True
PROFILE_URL_PARTS = {
- "pics": ("uploaded-pics", PROFILE_GALLERY_SELECTOR),
- "videos": ("uploaded-videos", VIDEO_SELECTOR),
- "playlists": ("playlists", PROFILE_PLAYLIST_SELECTOR),
+ "pics": ("uploaded-pics", _SELECTORS.PROFILE_GALLERY),
+ "videos": ("uploaded-videos", _SELECTORS.VIDEO),
+ "playlists": ("playlists", _SELECTORS.PROFILE_PLAYLIST),
}
@@ -51,16 +47,11 @@ class VideoInfo(NamedTuple):
size: str
link_str: str
- @cached_property
- def byte_size(self) -> ByteSize:
- return ByteSize(self.size)
-
@classmethod
def from_tag(cls, tag: Tag) -> VideoInfo:
- link_str: str = tag.get("href") # type: ignore
- name = tag.get_text()
- name_string = name.removeprefix("Download").strip()
- details = name_string.split("(", 1)[1].removesuffix(")").split(",")
+ link_str: str = tag["href"] # type: ignore
+ name = tag.get_text(strip=True).removeprefix("Download")
+ details = name.split("(", 1)[1].removesuffix(")").split(",")
res, codec, size = tuple([d.strip() for d in details])
codec = codec.lower()
return cls(codec, res, size, link_str)
@@ -68,7 +59,7 @@ class VideoInfo(NamedTuple):
class EpornerCrawler(Crawler):
primary_base_domain = URL("https://www.eporner.com/")
- next_page_selector = NEXT_PAGE_SELECTOR
+ next_page_selector = _SELECTORS.NEXT_PAGE
def __init__(self, manager: Manager) -> None:
super().__init__(manager, "eporner", "ePorner")
@@ -117,19 +108,17 @@ class EpornerCrawler(Crawler):
@error_handling_wrapper
async def playlist(self, scrape_item: ScrapeItem, from_profile: bool = False) -> None:
- added_title = False
-
+ title: str = ""
async for soup in self.web_pager(scrape_item.url):
- if not added_title and not from_profile:
+ if not title and not from_profile:
title = soup.title.text # type: ignore
title_trash = "Porn Star Videos", "Porn Videos", "Videos -", "EPORNER"
for trash in title_trash:
title = title.rsplit(trash)[0].strip()
title = self.create_title(title)
scrape_item.setup_as_album(title)
- added_title = True
- for _, new_scrape_item in self.iter_children(scrape_item, soup, VIDEO_SELECTOR):
+ for _, new_scrape_item in self.iter_children(scrape_item, soup, _SELECTORS.VIDEO):
self.manager.task_group.create_task(self.run(new_scrape_item))
@error_handling_wrapper
@@ -137,11 +126,11 @@ class EpornerCrawler(Crawler):
title: str = ""
async for soup in self.web_pager(scrape_item.url):
if not title:
- title = soup.select_one(GALLERY_TITLE_SELECTOR).get_text(strip=True) # type: ignore
+ title = soup.select_one(_SELECTORS.GALLERY_TITLE).get_text(strip=True) # type: ignore
title = self.create_title(title)
scrape_item.setup_as_album(title)
- for thumb, new_scrape_item in self.iter_children(scrape_item, soup, PROFILE_GALLERY_SELECTOR):
+ for thumb, new_scrape_item in self.iter_children(scrape_item, soup, _SELECTORS.PROFILE_GALLERY):
assert thumb
filename = thumb.name.rsplit("-", 1)[0]
filename, ext = self.get_filename_and_ext(f"{filename}{thumb.suffix}")
@@ -159,10 +148,10 @@ class EpornerCrawler(Crawler):
soup: BeautifulSoup = await self.client.get_soup(self.domain, scrape_item.url)
scrape_item.url = canonical_url
- img = soup.select_one(PHOTO_SELECTOR)
+ img = soup.select_one(_SELECTORS.PHOTO)
if not img:
raise ScrapeError(422)
- link_str: str = img.get("href") # type: ignore
+ link_str: str = img["href"] # type: ignore
link = self.parse_url(link_str)
filename, ext = self.get_filename_and_ext(link.name)
await self.handle_file(link, scrape_item, filename, ext)
@@ -191,7 +180,7 @@ class EpornerCrawler(Crawler):
raise ScrapeError(422)
link = self.parse_url(link_str)
- scrape_item.possible_datetime = parse_datetime(info_dict["uploadDate"])
+ scrape_item.possible_datetime = self.parse_date(info_dict["uploadDate"])
filename, ext = self.get_filename_and_ext(link.name)
if ext == ".m3u8":
raise ScrapeError(422)
@@ -200,11 +189,11 @@ class EpornerCrawler(Crawler):
def get_available_resolutions(soup: BeautifulSoup) -> list[VideoInfo]:
- downloads = soup.select_one(DOWNLOADS_SELECTOR)
+ downloads = soup.select_one(_SELECTORS.DOWNLOADS)
assert downloads
- formats = downloads.select(H264_SELECTOR)
+ formats = downloads.select(_SELECTORS.H264)
if ALLOW_AV1:
- formats.extend(downloads.select(AV1_SELECTOR))
+ formats.extend(downloads.select(_SELECTORS.AV1))
return [VideoInfo.from_tag(tag) for tag in formats]
@@ -218,7 +207,6 @@ def get_best_quality(soup: BeautifulSoup) -> tuple[str, str]:
for res in RESOLUTIONS:
formats_dict[res] = sorted(f for f in formats if f.resolution == res)
- log_debug(formats_dict)
for res in RESOLUTIONS:
available_formats = formats_dict.get(res)
if available_formats:
@@ -229,10 +217,9 @@ def get_best_quality(soup: BeautifulSoup) -> tuple[str, str]:
def get_info_dict(soup: BeautifulSoup) -> dict:
- info_js_script = soup.select_one(DATE_JS_SELECTOR)
+ info_js_script = soup.select_one(_SELECTORS.DATE_JS)
info_dict: dict = javascript.parse_json_to_dict(info_js_script.text, use_regex=False) # type: ignore
javascript.clean_dict(info_dict)
- log_debug(info_dict)
return info_dict
@@ -242,9 +229,3 @@ def get_video_id(url: URL) -> str:
if any(p in url.parts for p in ("hd-porn", "embed")) and len(url.parts) > 2:
return url.parts[2]
return ""
-
-
-def parse_datetime(date: str) -> int:
- """Parses a datetime string into a unix timestamp."""
- parsed_date = datetime.fromisoformat(date)
- return calendar.timegm(parsed_date.timetuple())