Skip to content

Bulk asynchronous downloader for multiple file hosts

Use more HTTPStatus

Arkadiy Illarionove0ae4e496c58
diff --git a/cyberdrop_dl/client/client.py b/cyberdrop_dl/client/client.py
index 37cd36851..ef83aaa43 100644
--- a/cyberdrop_dl/client/client.py
+++ b/cyberdrop_dl/client/client.py
@@ -4,6 +4,7 @@ import asyncio
import json
import logging
import ssl
+from http import HTTPStatus
from pathlib import Path
import aiofiles
@@ -131,12 +132,12 @@ class DownloadSession:
raise_for_status=True, proxy=proxy) as resp:
content_type = resp.headers.get('Content-Type')
if not content_type:
- raise DownloadFailure(code=418, message="No content-type in response header")
+ raise DownloadFailure(code=HTTPStatus.IM_A_TEAPOT, message="No content-type in response header")
if resp.url in self.bunkr_maintenance:
- raise DownloadFailure(code=503, message="Bunkr under maintenance")
+ raise DownloadFailure(code=HTTPStatus.SERVICE_UNAVAILABLE, message="Bunkr under maintenance")
if 'text' in content_type.lower() or 'html' in content_type.lower():
logger.debug("Server for %s is experiencing issues, you are being ratelimited, or cookies have expired", str(media.url))
- raise DownloadFailure(code=418, message="Unexpectedly got text as response")
+ raise DownloadFailure(code=HTTPStatus.IM_A_TEAPOT, message="Unexpectedly got text as response")
total = int(resp.headers.get('Content-Length', str(0))) + resume_point
file.parent.mkdir(parents=True, exist_ok=True)
@@ -159,12 +160,12 @@ class DownloadSession:
raise_for_status=True, proxy=proxy) as resp:
content_type = resp.headers.get('Content-Type')
if not content_type:
- raise DownloadFailure(code=418, message="No content-type in response header")
+ raise DownloadFailure(code=HTTPStatus.IM_A_TEAPOT, message="No content-type in response header")
if resp.url in self.bunkr_maintenance:
- raise DownloadFailure(code=503, message="Bunkr under maintenance")
+ raise DownloadFailure(code=HTTPStatus.SERVICE_UNAVAILABLE, message="Bunkr under maintenance")
if 'text' in content_type.lower() or 'html' in content_type.lower():
logger.debug("Server for %s is experiencing issues, you are being ratelimited, or cookies have expired", str(media.url))
- raise DownloadFailure(code=418, message="Unexpectedly got text as response")
+ raise DownloadFailure(code=HTTPStatus.IM_A_TEAPOT, message="Unexpectedly got text as response")
total = int(resp.headers.get('Content-Length', str(0))) + resume_point
file.parent.mkdir(parents=True, exist_ok=True)
diff --git a/cyberdrop_dl/downloader/downloader_utils.py b/cyberdrop_dl/downloader/downloader_utils.py
index 6c38841f5..6b76428ea 100644
--- a/cyberdrop_dl/downloader/downloader_utils.py
+++ b/cyberdrop_dl/downloader/downloader_utils.py
@@ -7,6 +7,10 @@ from cyberdrop_dl.base_functions.base_functions import FILE_FORMATS
from cyberdrop_dl.base_functions.data_classes import MediaItem
+class CloudflareHTTPStatus(HTTPStatus):
+ WEB_SERVER_IS_DOWN = 521, 'Web Server Is Down'
+
+
async def allowed_filetype(media: MediaItem, block_images: bool, block_video: bool, block_audio: bool, block_other: bool):
"""Checks whether the enclosed file is allowed to be downloaded"""
ext = media.ext
diff --git a/cyberdrop_dl/downloader/downloaders.py b/cyberdrop_dl/downloader/downloaders.py
index 778a6af87..f437e2571 100644
--- a/cyberdrop_dl/downloader/downloaders.py
+++ b/cyberdrop_dl/downloader/downloaders.py
@@ -26,6 +26,7 @@ from cyberdrop_dl.client.client import Client, DownloadSession
from cyberdrop_dl.scraper.Scraper import ScrapeMapper
from .downloader_utils import (
+ CloudflareHTTPStatus,
allowed_filetype,
basic_auth,
check_free_space,
@@ -294,7 +295,7 @@ class Downloader:
self.current_attempt.pop(url_path)
await self.output_failed(media, e)
return
- if e.code == HTTPStatus.SERVICE_UNAVAILABLE or e.code == 521:
+ if e.code == HTTPStatus.SERVICE_UNAVAILABLE or e.code == CloudflareHTTPStatus.WEB_SERVER_IS_DOWN:
if hasattr(e, "message"):
if not e.message:
e.message = "Web server is down"
diff --git a/cyberdrop_dl/downloader/old_downloaders.py b/cyberdrop_dl/downloader/old_downloaders.py
index ac93bdf58..2bd5a7ca6 100644
--- a/cyberdrop_dl/downloader/old_downloaders.py
+++ b/cyberdrop_dl/downloader/old_downloaders.py
@@ -25,6 +25,7 @@ from cyberdrop_dl.client.client import Client, DownloadSession
from cyberdrop_dl.scraper.Scraper import ScrapeMapper
from .downloader_utils import (
+ CloudflareHTTPStatus,
allowed_filetype,
basic_auth,
check_free_space,
@@ -243,7 +244,7 @@ class Old_Downloader:
self.current_attempt.pop(url_path)
await self.output_failed(media, e)
return
- if e.code == HTTPStatus.SERVICE_UNAVAILABLE or e.code == 521:
+ if e.code == HTTPStatus.SERVICE_UNAVAILABLE or e.code == CloudflareHTTPStatus.WEB_SERVER_IS_DOWN:
if hasattr(e, "message"):
if not e.message:
e.message = "Web server is down"