Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use system certificates instead of certifi (#988)

NTFSvolume944a5ab22d77
diff --git a/cyberdrop_dl/constants.py b/cyberdrop_dl/constants.py
index fe4b07c2b..12c6e8b13 100644
--- a/cyberdrop_dl/constants.py
+++ b/cyberdrop_dl/constants.py
@@ -55,6 +55,25 @@ DOWNLOAD_STORAGE = Path("./Downloads")
RESERVED_CONFIG_NAMES = ["all", "default"]
+class HashType(StrEnum):
+ md5 = "md5"
+ sha256 = "sha256"
+ xxh128 = "xxh128"
+
+
+class Hashing(StrEnum):
+ OFF = auto()
+ IN_PLACE = auto()
+ POST_DOWNLOAD = auto()
+
+ @classmethod
+ def _missing_(cls, value: str) -> "Hashing":
+ try:
+ return cls[str(value.upper())]
+ except KeyError as e:
+ raise e
+
+
class BROWSERS(StrEnum):
chrome = auto()
firefox = auto()
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index 444ea71bf..c67daff9e 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -8,7 +8,7 @@ from dataclasses import field
from datetime import datetime
from functools import wraps
from pathlib import Path
-from typing import TYPE_CHECKING, Any, ClassVar, NewType, ParamSpec, Protocol, TypeVar
+from typing import TYPE_CHECKING, Any, ClassVar, ParamSpec, Protocol, TypeVar
from aiolimiter import AsyncLimiter
from dateutil import parser
@@ -17,6 +17,7 @@ from yarl import URL
from cyberdrop_dl.data_structures.url_objects import MediaItem, ScrapeItem
from cyberdrop_dl.downloader.downloader import Downloader
from cyberdrop_dl.scraper import filters
+from cyberdrop_dl.types import TimeStamp, is_absolute_http_url
from cyberdrop_dl.utils import utilities
from cyberdrop_dl.utils.database.tables.history_table import get_db_path
from cyberdrop_dl.utils.logger import log, log_debug
@@ -29,7 +30,7 @@ from cyberdrop_dl.utils.utilities import (
)
_NEW_ISSUE_URL = "https://github.com/jbsparrow/CyberDropDownloader/issues/new/choose"
-TimeStamp = NewType("TimeStamp", int)
+
P = ParamSpec("P")
R = TypeVar("R")
@@ -147,6 +148,9 @@ class Crawler(ABC):
else:
original_filename = filename
+ assert is_absolute_http_url(url)
+ if isinstance(debrid_link, URL):
+ assert is_absolute_http_url(debrid_link)
download_folder = get_download_path(self.manager, scrape_item, self.folder_domain)
media_item = MediaItem(url, scrape_item, download_folder, filename, original_filename, debrid_link, ext=ext)
diff --git a/cyberdrop_dl/managers/client_manager.py b/cyberdrop_dl/managers/client_manager.py
index 168e99883..3f24c2361 100644
--- a/cyberdrop_dl/managers/client_manager.py
+++ b/cyberdrop_dl/managers/client_manager.py
@@ -10,7 +10,7 @@ from http.cookiejar import MozillaCookieJar
from typing import TYPE_CHECKING, Any
import aiohttp
-import certifi
+import truststore
from aiohttp import ClientResponse, ClientSession, ContentTypeError
from aiolimiter import AsyncLimiter
from bs4 import BeautifulSoup
@@ -67,10 +67,10 @@ class ClientManager:
self.download_delay = global_settings_data.rate_limiting_options.download_delay
self.user_agent = global_settings_data.general.user_agent
- self.verify_ssl = not global_settings_data.general.allow_insecure_connections
self.simultaneous_per_domain = global_settings_data.rate_limiting_options.max_simultaneous_downloads_per_domain
- self.ssl_context = ssl.create_default_context(cafile=certifi.where()) if self.verify_ssl else False
+ verify_ssl = not global_settings_data.general.allow_insecure_connections
+ self.ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) if verify_ssl else False
self.cookies = aiohttp.CookieJar(quote_cookie=False)
self.proxy: URL | None = global_settings_data.general.proxy # type: ignore
diff --git a/cyberdrop_dl/types.py b/cyberdrop_dl/types.py
index df10ac0c7..fd8f093d9 100644
--- a/cyberdrop_dl/types.py
+++ b/cyberdrop_dl/types.py
@@ -44,6 +44,7 @@ if TYPE_CHECKING:
absolute: Literal[True]
scheme: Literal["http", "https"]
+ @property
def host(self) -> str: # type: ignore
"""Decoded host part of URL."""
@@ -89,7 +90,7 @@ class AppriseURLModel(FrozenModel):
tags: set[str] = set()
@model_serializer()
- def serialize(self, info: SerializationInfo):
+ def serialize(self, info: SerializationInfo) -> str:
dump_secret = info.mode != "json"
url = self.url.get_secret_value() if dump_secret else self.url
tags = self.tags - set("no_logs")
@@ -117,4 +118,7 @@ U32Int: TypeAlias = int
U32IntArray: TypeAlias = Array[U32Int]
U32IntSequence: TypeAlias = Sequence[U32Int]
AnyDict: TypeAlias = dict[str, Any]
+
AbsolutePath = NewType("AbsolutePath", Path)
+HashValue = NewType("HashValue", str)
+TimeStamp = NewType("TimeStamp", int)
diff --git a/poetry.lock b/poetry.lock
index 7bed70233..da2ed9253 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -2312,6 +2312,18 @@ virtualenv = ">=20.29.1"
[package.extras]
test = ["devpi-process (>=1.0.2)", "pytest (>=8.3.4)", "pytest-mock (>=3.14)"]
+[[package]]
+name = "truststore"
+version = "0.10.1"
+description = "Verify certificates using native system trust stores"
+optional = false
+python-versions = ">=3.10"
+groups = ["main"]
+files = [
+ {file = "truststore-0.10.1-py3-none-any.whl", hash = "sha256:b64e6025a409a43ebdd2807b0c41c8bff49ea7ae6550b5087ac6df6619352d4c"},
+ {file = "truststore-0.10.1.tar.gz", hash = "sha256:eda021616b59021812e800fa0a071e51b266721bef3ce092db8a699e21c63539"},
+]
+
[[package]]
name = "types-python-dateutil"
version = "2.9.0.20241206"
@@ -2733,4 +2745,4 @@ propcache = ">=0.2.1"
[metadata]
lock-version = "2.1"
python-versions = ">=3.11,<4"
-content-hash = "44476db2ce4d92ff818203b0506dc312595d32b17674b4d78e9b2c568b5f0d67"
+content-hash = "70619b741c995f47c2afe39d331eb88f42ded0193c0e4a64e756a336607b9f39"
diff --git a/pyproject.toml b/pyproject.toml
index 395edc666..c627fe9b6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -29,6 +29,7 @@ dependencies = [
"rich >=13.9.4",
"send2trash >=1.8.3",
"textual >=3.2.0",
+ "truststore >=0.10.1",
"xxhash >=3.5.0"
]
description = "Bulk downloader for multiple file hosts"