Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: add ALLOW_MISSING_CONTENT_LENGTH envvar
NTFSvolume51d15b2578a7
diff --git a/cyberdrop_dl/clients/downloads.py b/cyberdrop_dl/clients/downloads.pyindex 91b9f3cef..b4b873852 100644--- a/cyberdrop_dl/clients/downloads.py+++ b/cyberdrop_dl/clients/downloads.py@@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any, finalfrom aiohttp import hdrs-from cyberdrop_dl import aio, constants, ffmpeg, storage+from cyberdrop_dl import aio, constants, env, ffmpeg, storagefrom cyberdrop_dl.clients import etagfrom cyberdrop_dl.constants import FileExt, HashModefrom cyberdrop_dl.exceptions import DownloadError, InvalidContentTypeError, SlowDownloadError@@ -115,11 +115,7 @@ class DownloadClient:if not media_item.is_segment and (content_type := _get_content_type(resp.headers)):_check_content_type(content_type, media_item.ext)- try:- media_item.size = int(resp.headers[hdrs.CONTENT_LENGTH])- except KeyError:- msg = f"Download response has no `{hdrs.CONTENT_LENGTH}` header. Refusing to download"- raise DownloadError(HTTPStatus.LENGTH_REQUIRED, msg, retry=False) from None+ media_item.size = _get_content_length(resp.headers)if not media_item.path:_check_content_length(resp.headers)downloaded = await self._predownload_skip(media_item, domain)@@ -484,3 +480,13 @@ def make_speed_checker(media_item: MediaItem, hook: ProgressHook, speed_thresholraise SlowDownloadError(origin=media_item)return check_download_speed+++def _get_content_length(headers: Mapping[str, str]) -> int:+ try:+ return int(headers[hdrs.CONTENT_LENGTH])+ except KeyError:+ if env.ALLOW_MISSING_CONTENT_LENGTH:+ return 0+ msg = f"Download response has no `{hdrs.CONTENT_LENGTH}` header. Refusing to download"+ raise DownloadError(HTTPStatus.LENGTH_REQUIRED, msg, retry=False) from Nonediff --git a/cyberdrop_dl/env.py b/cyberdrop_dl/env.pyindex 5906f9a82..b816f0315 100644--- a/cyberdrop_dl/env.py+++ b/cyberdrop_dl/env.py@@ -19,7 +19,7 @@ RUNNING_IN_TERMUX = bool()FORCE_PORTRAIT_MODE = bool(_env("PORTRAIT_MODE") or RUNNING_IN_TERMUX)-+ALLOW_MISSING_CONTENT_LENGTH = bool(_env("ALLOW_MISSING_CONTENT_LENGTH"))DEBUG_LOG_FOLDER = _env("DEBUG_LOG_FOLDER")MAX_CRAWLER_ERRORS = int(_env("MAX_CRAWLER_ERRORS") or 10)diff --git a/tests/test_downloader.py b/tests/test_downloader.pyindex cc5e2b35e..705f24910 100644--- a/tests/test_downloader.py+++ b/tests/test_downloader.py@@ -1,13 +1,16 @@from __future__ import annotations+from http import HTTPStatusfrom pathlib import Pathfrom typing import TYPE_CHECKINGfrom unittest import mockimport pytest-from cyberdrop_dl.clients.downloads import filter_by_duration+from cyberdrop_dl import env+from cyberdrop_dl.clients.downloads import _get_content_length, filter_by_durationfrom cyberdrop_dl.downloader.http import Downloader+from cyberdrop_dl.exceptions import DownloadErrorfrom cyberdrop_dl.url_objects import AbsoluteHttpURL, MediaItemif TYPE_CHECKING:@@ -54,3 +57,20 @@ async def test_probe_duration_is_skipped_on_default_config(manager: Manager) ->assert not resultassert fn.self.call_count == 0+++def test_missing_content_lenght() -> None:++ assert _get_content_length({"Content-Length": "200"}) == 200++ with pytest.raises(DownloadError) as exc:+ _get_content_length({})++ assert exc.value.status is HTTPStatus.LENGTH_REQUIRED+ assert exc.value.retry is False++ env.ALLOW_MISSING_CONTENT_LENGTH, old_value = True, env.ALLOW_MISSING_CONTENT_LENGTH+ try:+ assert _get_content_length({}) == 0+ finally:+ env.ALLOW_MISSING_CONTENT_LENGTH = old_value