Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
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.pyindex 7fa4cb7aa..c00b8b694 100644--- a/cyberdrop_dl/crawlers/_hls.py+++ b/cyberdrop_dl/crawlers/_hls.py@@ -3,11 +3,14 @@ from __future__ import annotationsimport asyncioimport dataclassesimport logging+import osfrom abc import ABC, abstractmethodfrom typing import TYPE_CHECKING, Literalfrom typing_extensions import override+from cyberdrop_dl import ffmpeg+from cyberdrop_dl.exceptions import DownloadErrorfrom cyberdrop_dl.utils import m3u8if 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.pyindex 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)@classmethoddiff --git a/cyberdrop_dl/downloader/http.py b/cyberdrop_dl/downloader/http.pyindex 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.pyindex 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