Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: remove `CacheDuration` model
NTFSvolume143dbd8e5eef
diff --git a/cyberdrop_dl/config_definitions/custom_types.py b/cyberdrop_dl/config_definitions/custom_types.pyindex c24eb3a42..51c421450 100644--- a/cyberdrop_dl/config_definitions/custom_types.py+++ b/cyberdrop_dl/config_definitions/custom_types.py@@ -1,9 +1,7 @@from __future__ import annotations-from datetime import datetime, timedeltafrom typing import Annotated-import humanfriendlyfrom pydantic import (AfterValidator,AnyUrl,@@ -110,43 +108,3 @@ 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 as e:- raise ValueError(f"Invalid cache duration format: {value}") from e-- @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.- """-- if isinstance(self, timedelta):- return humanfriendly.format_timespan(self.total_seconds(), detailed=False)- elif isinstance(self.duration, timedelta):- return humanfriendly.format_timespan(self.duration.total_seconds(), detailed=False)-- def __radd__(self, other):- if isinstance(other, datetime) or isinstance(other, timedelta):- return other + self.duration- return NotImplementeddiff --git a/cyberdrop_dl/config_definitions/global_settings.py b/cyberdrop_dl/config_definitions/global_settings.pyindex aed123181..f703c1c99 100644--- a/cyberdrop_dl/config_definitions/global_settings.py+++ b/cyberdrop_dl/config_definitions/global_settings.py@@ -4,7 +4,7 @@ from datetime import timedeltafrom pydantic import BaseModel, ByteSize, Field, NonNegativeFloat, PositiveInt, field_serializer, field_validatorfrom yarl import URL-from .custom_types import AliasModel, CacheDuration, HttpURL, NonEmptyStr+from .custom_types import AliasModel, HttpURL, NonEmptyStrDATE_PATTERN = re.compile(r"(\d+)\s*(second|seconds|minute|minutes|hour|hours|day|days|week|weeks|month|months|year|years)", re.IGNORECASE@@ -53,8 +53,8 @@ class RateLimitingOptions(BaseModel):max_simultaneous_downloads: PositiveInt = 15max_simultaneous_downloads_per_domain: PositiveInt = 3download_speed_limit: ByteSize = ByteSize(0)- file_host_cache_expire_after: CacheDuration = timedelta(days=7)- forum_cache_expire_after: CacheDuration = timedelta(weeks=4)+ file_host_cache_expire_after: timedelta = timedelta(days=7)+ forum_cache_expire_after: timedelta = timedelta(weeks=4)@field_serializer("download_speed_limit")def human_readable(self, value: ByteSize | int) -> str:diff --git a/cyberdrop_dl/managers/cache_manager.py b/cyberdrop_dl/managers/cache_manager.pyindex a4dc6319d..c47b49cbf 100644--- a/cyberdrop_dl/managers/cache_manager.py+++ b/cyberdrop_dl/managers/cache_manager.py@@ -9,7 +9,7 @@ from aiohttp_client_cache import SQLiteBackendfrom cyberdrop_dl.scraper.filters import filter_fnfrom cyberdrop_dl.utils import yaml-from cyberdrop_dl.utils.data_enums_classes.supported_domains import FORUMS, WEBSITES+from cyberdrop_dl.utils.data_enums_classes.supported_domains import SUPPORTED_FORUMS, SUPPORTED_WEBSITESif TYPE_CHECKING:from pathlib import Path@@ -40,17 +40,16 @@ class CacheManager:self._cache = yaml.load(self.cache_file)def load_request_cache(self) -> None:+ rate_limiting_options = self.manager.config_manager.global_settings_data.rate_limiting_optionsurls_expire_after = {- "*.simpcity.su": self.manager.config_manager.global_settings_data.rate_limiting_options.file_host_cache_expire_after.duration,+ "*.simpcity.su": rate_limiting_options.file_host_cache_expire_after,}- for host in WEBSITES.values():- urls_expire_after[f"*.{host}" if "." in host else f"*.{host}.*"] = (- self.manager.config_manager.global_settings_data.rate_limiting_options.file_host_cache_expire_after.duration- )- for forum in FORUMS.values():- urls_expire_after[f"{forum}"] = (- self.manager.config_manager.global_settings_data.rate_limiting_options.forum_cache_expire_after.duration- )+ for host in SUPPORTED_WEBSITES.values():+ match_host = f"*.{host}" if "." in host else f"*.{host}.*"+ urls_expire_after[match_host] = rate_limiting_options.file_host_cache_expire_after+ for forum in SUPPORTED_FORUMS.values():+ urls_expire_after[forum] = rate_limiting_options.forum_cache_expire_after+self.request_cache = SQLiteBackend(cache_name=self.manager.path_manager.cache_db,autoclose=False,