Add config.py module to read user settings from config.json (not tracked
by git). Proxy URL and request timeout are no longer hard-coded.
Configuration priority:
1. config.json 'civitai.proxy' — explicit user setting
2. HTTP_PROXY/HTTPS_PROXY environment variables — fallback
3. No proxy — if neither is set
Files added:
- config.py — configuration loader with get_proxy_url() and get_timeout()
- config.json.example — template showing available options
Files modified:
- civitai_client.py — uses config.py instead of hard-coded values;
proxy is passed explicitly to each request
- .gitignore — ignores config.json so user settings survive updates
Users in regions where Civitai is blocked can copy config.json.example
to config.json and adjust proxy settings without touching source code.
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import json
|
|
import os
|
|
|
|
# User config file (not tracked by git)
|
|
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
|
|
|
|
DEFAULT_CONFIG = {
|
|
"civitai": {
|
|
"proxy": None,
|
|
"timeout_seconds": 30,
|
|
}
|
|
}
|
|
|
|
|
|
def load_config() -> dict:
|
|
"""Load user config from config.json, fall back to defaults."""
|
|
if not os.path.exists(CONFIG_PATH):
|
|
return DEFAULT_CONFIG.copy()
|
|
try:
|
|
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
|
user_config = json.load(f)
|
|
config = DEFAULT_CONFIG.copy()
|
|
if "civitai" in user_config:
|
|
config["civitai"].update(user_config["civitai"])
|
|
return config
|
|
except (json.JSONDecodeError, OSError):
|
|
return DEFAULT_CONFIG.copy()
|
|
|
|
|
|
def get_proxy_url() -> str | None:
|
|
"""Get proxy URL string (e.g. 'http://127.0.0.1:7890') or None.
|
|
|
|
Priority:
|
|
1. config.json 'civitai.proxy' (explicit user setting)
|
|
2. HTTP_PROXY / HTTPS_PROXY environment variables
|
|
3. No proxy
|
|
"""
|
|
config = load_config()
|
|
proxy = config.get("civitai", {}).get("proxy")
|
|
if proxy is not None:
|
|
return str(proxy) if proxy else None
|
|
|
|
# Fallback to environment variables
|
|
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
|
|
if https_proxy:
|
|
return https_proxy
|
|
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
|
if http_proxy:
|
|
return http_proxy
|
|
return None
|
|
|
|
|
|
def get_timeout() -> int:
|
|
"""Get request timeout in seconds."""
|
|
config = load_config()
|
|
return config.get("civitai", {}).get("timeout_seconds", 30)
|