Centralizes all outbound network requests into a single module for easier Comfy Registry security review. Starts with CivitAI API. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
# ==============================================================================
|
|
# NETWORK UTILS — All outbound network requests are centralized here.
|
|
#
|
|
# External services contacted:
|
|
# 1. CivitAI API (civitai.com/api/v1) — model/LoRA metadata lookup
|
|
# 2. HuggingFace Endpoints (*.huggingface.cloud) — Remote VAE decoding
|
|
# 3. Distribution System (user-configured LAN addresses) — worker coordination
|
|
#
|
|
# No other file in this project makes outbound network requests.
|
|
# All urllib usage is confined to this module.
|
|
# ==============================================================================
|
|
|
|
import json
|
|
import os
|
|
import urllib.request
|
|
import urllib.error
|
|
import urllib.parse
|
|
|
|
|
|
# =============================================================================
|
|
# 1. CIVITAI API — Model/LoRA metadata lookup
|
|
# Target: civitai.com/api/v1 (ONLY)
|
|
# =============================================================================
|
|
|
|
CIVITAI_API_BASE = "https://civitai.com/api/v1"
|
|
CIVITAI_TIMEOUT = 10 # seconds
|
|
CIVITAI_USER_AGENT = "ComfyUI-ConfigBuilder"
|
|
|
|
|
|
def civitai_fetch_by_hash(hash_value):
|
|
"""
|
|
Fetch model version info from CivitAI API using a file hash.
|
|
|
|
Network call: GET https://civitai.com/api/v1/model-versions/by-hash/{hash}
|
|
Timeout: 10 seconds
|
|
Returns: Parsed JSON dict on success, None on any error.
|
|
"""
|
|
api_url = f"{CIVITAI_API_BASE}/model-versions/by-hash/{hash_value}"
|
|
try:
|
|
req = urllib.request.Request(
|
|
api_url,
|
|
headers={"User-Agent": CIVITAI_USER_AGENT}
|
|
)
|
|
with urllib.request.urlopen(req, timeout=CIVITAI_TIMEOUT) as response:
|
|
if response.status == 200:
|
|
return json.loads(response.read().decode("utf-8"))
|
|
else:
|
|
return None
|
|
except urllib.error.HTTPError:
|
|
return None
|
|
except Exception as e:
|
|
print(f"[NetworkUtils] ⚠️ CivitAI lookup failed: {e}")
|
|
return None
|