Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: xhamster crawler support for xhamster45.desi (#166)

Barbarella66666665516e0a45d02
diff --git a/cyberdrop_dl/crawlers/xhamster.py b/cyberdrop_dl/crawlers/xhamster.py
index 8a5242ab1..fc44e64b6 100644
--- a/cyberdrop_dl/crawlers/xhamster.py
+++ b/cyberdrop_dl/crawlers/xhamster.py
@@ -210,19 +210,29 @@ class XhamsterCrawler(Crawler):
initials = await self._get_window_initials(scrape_item.url)
video = _parse_video(initials)
scrape_item.uploaded_at = video.created
+ m3u8 = debrid_link = None
+
+ if best_format := video.best_mp4:
+ debrid_link = video.best_mp4.url
+ else:
+ best_format = video.best_hls
+ m3u8, _ = await self.request_m3u8_playlist(video.best_hls.url)
+
custom_filename = self.create_custom_filename(
video.title,
- ".mp4",
+ ext := ".mp4",
file_id=video.id,
- video_codec=video.best_mp4.codec.name.lower(),
- resolution=video.best_mp4.resolution,
+ video_codec=best_format.codec.name.lower(),
+ resolution=best_format.resolution,
)
+
await self.handle_file(
scrape_item.url,
scrape_item,
- filename=video.id + ".mp4",
+ filename=video.id + ext,
custom_filename=custom_filename,
- debrid_link=video.best_mp4.url,
+ m3u8=m3u8,
+ debrid_link=debrid_link,
)
async def _get_window_initials(self, url: AbsoluteHttpURL) -> dict[str, Any]:
@@ -250,8 +260,8 @@ class Video:
id: str
title: str
created: int
- best_hls: Format | None
- best_mp4: Format
+ best_hls: Format
+ best_mp4: Format | None
def _parse_video(initials: dict[str, Any]) -> Video:
@@ -260,23 +270,28 @@ def _parse_video(initials: dict[str, Any]) -> Video:
hls_sources: list[Format] = []
mp4_sources: list[Format] = []
- for src in _parse_xplayer_sources(initials):
- if src.url.suffix == ".m3u8":
- hls_sources.append(src)
- else:
- mp4_sources.append(src)
+ for name, allow_mp4 in [
+ ("xplayerSettings2", True),
+ ("xplayerSettings", False),
+ ]:
+ xplayer_sources: dict[str, Any] = initials.get(name, {}).get("sources", {}) or {}
+
+ for src in _parse_xplayer_sources(xplayer_sources):
+ if src.url.suffix == ".m3u8":
+ hls_sources.append(src)
+ elif allow_mp4:
+ mp4_sources.append(src)
return Video(
id=video.get("idHashSlug") or video["videoIdHashSlug"],
title=video["title"],
created=video.get("created") or video["addTime"],
- best_hls=max(hls_sources, default=None),
- best_mp4=max(mp4_sources),
+ best_hls=max(hls_sources),
+ best_mp4=max(mp4_sources, default=None),
)
-def _parse_xplayer_sources(initials: dict[str, Any]) -> Iterable[Format]:
- xplayer_sources: dict[str, Any] = initials.get("xplayerSettings2", {}).get("sources", {})
+def _parse_xplayer_sources(xplayer_sources: dict[str, Any]) -> Iterable[Format]:
if not xplayer_sources:
return
diff --git a/tests/crawlers/test_cases/xhamster.py b/tests/crawlers/test_cases/xhamster.py
index f0523629a..15684f292 100644
--- a/tests/crawlers/test_cases/xhamster.py
+++ b/tests/crawlers/test_cases/xhamster.py
@@ -7,12 +7,27 @@ TEST_CASES = [
"url": "https://ge.xhamster.desi/videos/horny-asian-slut-fucking-with-big-toy-and-cumming-a-lot-xhW9s1z",
"filename": "Horny Asian slut fucking with Big Toy and CUMMING a lot [xhW9s1z][h264][720p].mp4",
"original_filename": "xhW9s1z.mp4",
+ "debrid_link": None,
"referer": "https://ge.xhamster.desi/videos/horny-asian-slut-fucking-with-big-toy-and-cumming-a-lot-xhW9s1z",
"album_id": None,
"datetime": 1609776316,
}
],
),
+ (
+ "https://xhamster45.desi/videos/eating-her-ass-xhdR056",
+ [
+ {
+ "url": "https://xhamster45.desi/videos/eating-her-ass-xhdR056",
+ "filename": "Eating her ass [xhdR056][h264][720p].mp4",
+ "original_filename": "xhdR056.mp4",
+ "debrid_link": None, # Should download an HLS stream
+ "referer": "https://xhamster45.desi/videos/eating-her-ass-xhdR056",
+ "album_id": None,
+ "datetime": 1764724818,
+ }
+ ],
+ ),
(
"https://ge.xhamster.com/photos/gallery/15860946/511409507",
[
@@ -51,7 +66,7 @@ TEST_CASES = [
"filename": "18yo Alexandra student BDSM real casting [2023793][h264][240p].mp4",
"original_filename": "2023793.mp4",
"referer": "https://xhamster.com/videos/18yo-alexandra-student-bdsm-real-casting-2023793",
- "download_folder": r"re:ombreto \[user\] \(xHamster\)/videos",
+ "download_folder": r"re:ombreto \[user\] \(xHamster\)[\\/]videos",
"album_id": None,
"datetime": 1372456260,
}
@@ -66,12 +81,12 @@ TEST_CASES = [
"download_folder": r"re:DanielaGomezx \[user\] \(xHamster\)",
},
],
- range(50, 60),
+ range(50, 70),
),
(
"https://xhamster.com/creators/danielagomezz/exclusive",
[],
- range(50, 60),
+ range(50, 70),
),
(
"https://xhamster.com/creators/danielagomezz/photos",
@@ -81,7 +96,7 @@ TEST_CASES = [
"filename": "001 - 698_1000 [517308698].jpg",
"original_filename": "698_1000.jpg",
"referer": "https://xhamster.com/photos/gallery/16252066/517308698",
- "download_folder": r"re:DanielaGomezx \[user\] \(xHamster\)/galleries/New image change",
+ "download_folder": r"re:DanielaGomezx \[user\] \(xHamster\)[\\/]galleries[\\/]New image change",
"album_id": "16252066",
"datetime": 1738917721,
},
@@ -90,7 +105,7 @@ TEST_CASES = [
"filename": "002 - 699_1000 [517308699].jpg",
"original_filename": "699_1000.jpg",
"referer": "https://xhamster.com/photos/gallery/16252066/517308699",
- "download_folder": r"re:DanielaGomezx \[user\] \(xHamster\)/galleries/New image change",
+ "download_folder": r"re:DanielaGomezx \[user\] \(xHamster\)[\\/]galleries[\\/]New image change",
"album_id": "16252066",
"datetime": 1738917721,
},