Skip to content

Bulk asynchronous downloader for multiple file hosts

perf: do not make any HLS request if ffmpeg is not installed (#1934)

NTFSvolume0027afc83733
diff --git a/cyberdrop_dl/crawlers/_hls.py b/cyberdrop_dl/crawlers/_hls.py
index 7fa4cb7aa..c00b8b694 100644
--- a/cyberdrop_dl/crawlers/_hls.py
+++ b/cyberdrop_dl/crawlers/_hls.py
@@ -3,11 +3,14 @@ from __future__ import annotations
import asyncio
import dataclasses
import logging
+import os
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Literal
from typing_extensions import override
+from cyberdrop_dl import ffmpeg
+from cyberdrop_dl.exceptions import DownloadError
from cyberdrop_dl.utils import m3u8
if TYPE_CHECKING:
@@ -20,6 +23,16 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
+def check_ffmpeg_is_installed() -> None:
+ if ffmpeg.is_installed():
+ return
+ msg = "ffmpeg is not installed and it is required for HLS downloads"
+ if os.name == "nt":
+ msg += ". Get it from: https://www.gyan.dev/ffmpeg/builds/"
+
+ raise DownloadError("FFmpeg Not Installed", msg)
+
+
class HLSMixin(ABC):
"""Class to fetch and parse HTTP live streams
@@ -80,6 +93,7 @@ class HLSMixin(ABC):
headers: Mapping[str, str] | None = None,
media_type: Literal["video", "audio", "subtitle"] | None = None,
) -> m3u8.M3U8:
+ check_ffmpeg_is_installed()
content = await self.request_text(url, headers=headers)
return m3u8.M3U8(content, url.parent, media_type)
diff --git a/cyberdrop_dl/ddos_guard.py b/cyberdrop_dl/ddos_guard.py
index b31818fc6..065eb2b4b 100644
--- a/cyberdrop_dl/ddos_guard.py
+++ b/cyberdrop_dl/ddos_guard.py
@@ -38,16 +38,19 @@ async def check_resp(resp: _Response, /) -> None:
if "html" not in resp.content_type:
return
- posibilities = [cls for cls in (DDosGuard, CloudFlareTurnstile, Anubis) if cls.may_be_challenge(resp)]
- if not posibilities:
+ mitigations: list[type[DDosGuard]] = []
+ for cls in (DDosGuard, CloudFlareTurnstile, Anubis):
+ if not cls.may_be_challenge(resp):
+ continue
+ if cls._is_challenge(resp):
+ raise DDOSGuardError(f"{cls.__name__} anti-bot protection detected")
+ mitigations.append(cls)
+
+ if not mitigations:
return
- for protection in posibilities:
- if protection._is_challenge(resp): # pyright: ignore[reportPrivateUsage]
- raise DDOSGuardError(f"{protection.__name__} anti-bot protection detected")
-
soup = _soup(await resp.text())
- check_soup(soup, posibilities)
+ check_soup(soup, mitigations)
def check_html(html: str) -> None:
@@ -80,7 +83,7 @@ class DDosGuard:
@final
@classmethod
- def is_challenge(cls, resp: _Response) -> bool:
+ def is_confirmed_challenge(cls, resp: _Response) -> bool:
return cls.may_be_challenge(resp) and cls._is_challenge(resp)
@classmethod
diff --git a/cyberdrop_dl/downloader/http.py b/cyberdrop_dl/downloader/http.py
index 226419ec5..160871779 100644
--- a/cyberdrop_dl/downloader/http.py
+++ b/cyberdrop_dl/downloader/http.py
@@ -167,13 +167,7 @@ class Downloader:
if media_item.url.path in self._processed_items and not self._ignore_history:
return
- if not ffmpeg.is_installed():
- msg = "ffmpeg is not installed. (Required for HLS downloads)"
- if os.name == "nt":
- msg += ". Get it from: https://www.gyan.dev/ffmpeg/builds/"
-
- raise DownloadError("FFmpeg Error", msg, media_item)
-
+ assert ffmpeg.is_installed()
async with self._download_context(media_item):
await self._start_hls_download(media_item, m3u8_group)
diff --git a/tests/test_ddos_guard.py b/tests/test_ddos_guard.py
index 4583d7e7a..7214b2dbf 100644
--- a/tests/test_ddos_guard.py
+++ b/tests/test_ddos_guard.py
@@ -122,9 +122,9 @@ def test_may_by_challenge(
({"server": "ddos-guard", "cf-mitigated": "challenge"}, 200, ddos_guard.CloudFlareTurnstile, False),
],
)
-def test_is_challenge(
+def test_is_confirmed_challenge(
headers: dict[str, str], status_code: int, cls: type[ddos_guard.DDosGuard], *, expected: bool
) -> None:
resp = DummyResponse(prepare_headers(headers), status_code)
- assert cls.is_challenge(resp) is expected
+ assert cls.is_confirmed_challenge(resp) is expected