Skip to content

Bulk asynchronous downloader for multiple file hosts

Switch to a custom type for cache length validation

Jacob6a0662d58fea
diff --git a/cyberdrop_dl/config_definitions/custom_types.py b/cyberdrop_dl/config_definitions/custom_types.py
index b0ede57a7..51dd672c1 100644
--- a/cyberdrop_dl/config_definitions/custom_types.py
+++ b/cyberdrop_dl/config_definitions/custom_types.py
@@ -1,6 +1,8 @@
from __future__ import annotations
from typing import Annotated
+from datetime import timedelta
+import humanfriendly
from pydantic import (
AfterValidator,
@@ -108,3 +110,34 @@ class HttpAppriseURLModel(AppriseURLModel):
class HttpAppriseURL(AppriseURL):
_validator = HttpAppriseURLModel
+
+
+class CacheDuration(BaseModel):
+ duration: timedelta = timedelta(days=7)
+
+ @staticmethod
+ def parse_cache_duration(value: str) -> timedelta:
+ """
+ Parses a human-readable duration string (e.g., '7 days', '2 hours').
+ """
+ try:
+ seconds = humanfriendly.parse_timespan(value)
+ return timedelta(seconds=seconds)
+ except humanfriendly.InvalidTimespan:
+ raise ValueError(f"Invalid cache duration format: {value}")
+
+ @model_validator(mode="before")
+ @classmethod
+ def validate_cache_duration(cls, values):
+ if isinstance(values, dict) and isinstance(values.get("duration"), str):
+ values["duration"] = cls.parse_cache_duration(values["duration"])
+ elif isinstance(values, str): # For direct string initialization
+ return {"duration": cls.parse_cache_duration(values)}
+ return values
+
+ @model_serializer()
+ def serialize_cache_duration(self):
+ """
+ Serializes the duration into a human-readable string.
+ """
+ return humanfriendly.format_timespan(self.total_seconds(), detailed=False)
diff --git a/cyberdrop_dl/config_definitions/global_settings.py b/cyberdrop_dl/config_definitions/global_settings.py
index 8ba9ec034..62f92d83b 100644
--- a/cyberdrop_dl/config_definitions/global_settings.py
+++ b/cyberdrop_dl/config_definitions/global_settings.py
@@ -3,7 +3,7 @@ from yarl import URL
from datetime import timedelta
import humanfriendly
-from .custom_types import AliasModel, HttpURL, NonEmptyStr
+from .custom_types import AliasModel, HttpURL, NonEmptyStr, CacheDuration
class General(BaseModel):
@@ -37,8 +37,8 @@ class RateLimitingOptions(BaseModel):
max_simultaneous_downloads: PositiveInt = 15
max_simultaneous_downloads_per_domain: PositiveInt = 3
download_speed_limit: ByteSize = ByteSize(0)
- file_host_cache_length: timedelta = Field(default=timedelta(days=7))
- forum_cache_length: timedelta = Field(default=timedelta(days=28, hours=12, minutes=30, seconds=15, milliseconds=500, microseconds=250))
+ file_host_cache_length: CacheDuration = timedelta(days=7)
+ forum_cache_length: CacheDuration = timedelta(weeks=4)
@field_serializer("download_speed_limit")
def human_readable(self, value: ByteSize | int) -> str:
@@ -46,20 +46,6 @@ class RateLimitingOptions(BaseModel):
value = ByteSize(value)
return value.human_readable(decimal=True)
- @field_serializer("file_host_cache_length", "forum_cache_length")
- def serialize_timedelta(self, value: timedelta) -> str:
- return humanfriendly.format_timespan(value.total_seconds())
-
- @field_validator("file_host_cache_length", "forum_cache_length", mode="before")
- def parse_timedelta(cls, value):
- if isinstance(value, str):
- try:
- seconds = humanfriendly.parse_timespan(value)
- return timedelta(seconds=seconds)
- except humanfriendly.InvalidTimespan:
- raise ValueError(f"Invalid time span format: {value}")
- return value
-
class UIOptions(BaseModel):
vi_mode: bool = False