refactor: centralize progress reporters, default user-agent, improve error propagation and aria2 fetch usage

This commit is contained in:
Dariusz L
2026-07-02 02:54:38 +02:00
parent f428924ee0
commit 59e61ba077
8 changed files with 50 additions and 100 deletions
+21
View File
@@ -41,3 +41,24 @@ def report_progress(
progress_callback(payload)
except Exception as e:
log.debug(f"{error_context} failed: {e}")
def get_progress_reporter(error_context: str) -> Callable[[Optional[Callable[[Dict[str, Any]], None]], str, str, Optional[float]], None]:
"""Return a closure for reporting progress with a pre-configured error context."""
def reporter(
progress_callback: Optional[Callable[[Dict[str, Any]], None]],
stage: str,
message: str,
percent: Optional[float] = None,
**extra: Any,
) -> None:
report_progress(
progress_callback,
stage,
message,
percent,
error_context=error_context,
**extra,
)
return reporter
+4 -21
View File
@@ -29,8 +29,9 @@ from ..type_utils import (
select_primary_model_file,
parse_size_to_bytes,
get_version_sort_key,
DEFAULT_BROWSER_USER_AGENT,
)
from ..progress import report_progress
from ..progress import report_progress, get_progress_reporter
from ..log_system.log_funcs import create_module_logger
log = create_module_logger(__name__)
@@ -57,11 +58,7 @@ _download_size_cache: Dict[str, Optional[int]] = {}
REQUEST_HEADERS = {
"accept": "application/json,text/html;q=0.9,*/*;q=0.8",
"user-agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/147.0.0.0 Safari/537.36"
),
"user-agent": DEFAULT_BROWSER_USER_AGENT,
}
# Imported CIVARCHIVE_API_TYPE_MAP
@@ -77,21 +74,7 @@ def clear_search_cache():
_download_size_cache.clear()
def _report_progress(
progress_callback: Optional[Callable[[Dict[str, Any]], None]],
stage: str,
message: str,
percent: Optional[float] = None,
**extra: Any,
) -> None:
report_progress(
progress_callback,
stage,
message,
percent,
error_context="CivArchive progress callback",
**extra,
)
_report_progress = get_progress_reporter("CivArchive progress callback")
def is_civarchive_available() -> bool:
+6 -31
View File
@@ -27,8 +27,9 @@ from ..type_utils import (
parse_size_to_bytes,
get_version_sort_key,
check_credential_http,
DEFAULT_BROWSER_USER_AGENT,
)
from ..progress import report_progress
from ..progress import report_progress, get_progress_reporter
from ..path_utils import calculate_file_sha256
from ..log_system.log_funcs import create_module_logger
log = create_module_logger(__name__)
@@ -57,21 +58,7 @@ def clear_search_cache():
_hash_cache.clear()
def _report_progress(
progress_callback: Optional[Callable[[Dict[str, Any]], None]],
stage: str,
message: str,
percent: Optional[float] = None,
**extra: Any,
) -> None:
report_progress(
progress_callback,
stage,
message,
percent,
error_context="CivitAI progress callback",
**extra,
)
_report_progress = get_progress_reporter("CivitAI progress callback")
def check_civitai_session_token(session_token: Optional[str]) -> Dict[str, Any]:
@@ -88,11 +75,7 @@ def check_civitai_session_token(session_token: Optional[str]) -> Dict[str, Any]:
headers = {
"accept": "application/json",
"Cookie": f"__Secure-civitai-token={token}",
"user-agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/147.0.0.0 Safari/537.36"
),
"user-agent": DEFAULT_BROWSER_USER_AGENT,
}
def get_user(data):
@@ -121,11 +104,7 @@ def check_civitai_api_key(api_key: Optional[str]) -> Dict[str, Any]:
headers = {
"accept": "application/json",
"Authorization": f"Bearer {key}",
"user-agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/147.0.0.0 Safari/537.36"
),
"user-agent": DEFAULT_BROWSER_USER_AGENT,
}
def get_user(data):
@@ -419,11 +398,7 @@ def _search_civitai_trpc_candidates(
"accept": "application/json",
"content-type": "application/json",
"referer": f"https://civitai.red/models?query={quote(filename)}",
"user-agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/147.0.0.0 Safari/537.36"
),
"user-agent": DEFAULT_BROWSER_USER_AGENT,
"x-client": "web",
"x-client-version": "5.0.1657",
}
+2 -16
View File
@@ -13,7 +13,7 @@ import requests
from typing import Dict, Any, Optional, List, Callable
from urllib.parse import urlparse, quote
from ..progress import report_progress
from ..progress import report_progress, get_progress_reporter
from ..log_system.log_funcs import create_module_logger
log = create_module_logger(__name__)
@@ -46,21 +46,7 @@ def clear_search_cache():
_author_index_cache.clear()
def _report_progress(
progress_callback: Optional[Callable[[Dict[str, Any]], None]],
stage: str,
message: str,
percent: Optional[float] = None,
**extra: Any,
) -> None:
report_progress(
progress_callback,
stage,
message,
percent,
error_context="HuggingFace progress callback",
**extra,
)
_report_progress = get_progress_reporter("HuggingFace progress callback")
def check_huggingface_token(token: Optional[str]) -> Dict[str, Any]:
+2 -16
View File
@@ -19,7 +19,7 @@ from ..matcher import (
calculate_candidate_rank,
)
from ..type_utils import select_primary_model_file
from ..progress import report_progress
from ..progress import report_progress, get_progress_reporter
from ..log_system.log_funcs import create_module_logger
log = create_module_logger(__name__)
@@ -54,21 +54,7 @@ def clear_search_cache():
_db_path_cache = None
def _report_progress(
progress_callback: Optional[Callable[[Dict[str, Any]], None]],
stage: str,
message: str,
percent: Optional[float] = None,
**extra: Any,
) -> None:
report_progress(
progress_callback,
stage,
message,
percent,
error_context="LoRA Manager archive progress callback",
**extra,
)
_report_progress = get_progress_reporter("LoRA Manager archive progress callback")
def get_lora_manager_archive_db_path() -> Optional[str]:
+8
View File
@@ -449,6 +449,14 @@ def format_size_bytes(bytes_value: Any, include_space: bool = True) -> Optional[
return f"{val:.1f}{space}{sizes[i]}"
DEFAULT_BROWSER_USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/147.0.0.0 Safari/537.36"
)