This commit is contained in:
nose
2025-12-17 03:16:41 -08:00
parent 86918f2ae2
commit 5104689a53
10 changed files with 146 additions and 620 deletions

View File

@@ -115,7 +115,57 @@ class HydrusNetwork(Store):
# Create a persistent client for this instance (auth via access key by default).
self._client = HydrusClient(url=self.URL, access_key=self.API, instance_name=self.NAME)
# Best-effort total count (fast on Hydrus side; does not fetch IDs/hashes).
# Best-effort total count (used for startup diagnostics). Avoid heavy payloads.
# Some Hydrus setups appear to return no count via the CBOR client for this endpoint,
# so prefer a direct JSON request with a short timeout.
try:
self.get_total_count(refresh=True)
except Exception:
pass
def get_total_count(self, *, refresh: bool = False) -> Optional[int]:
"""Best-effort total file count for this Hydrus instance.
Intended for diagnostics (e.g., REPL startup checks). This should be fast,
and it MUST NOT raise.
"""
if self.total_count is not None and not refresh:
return self.total_count
# 1) Prefer a direct JSON request (fast + avoids CBOR edge cases).
try:
import json as _json
url = f"{self.URL}/get_files/search_files"
params = {
"tags": _json.dumps(["system:everything"]),
"return_hashes": "false",
"return_file_ids": "false",
"return_file_count": "true",
}
headers = {
"Hydrus-Client-API-Access-Key": self.API,
"Accept": "application/json",
}
with httpx.Client(timeout=5.0, verify=False, follow_redirects=True) as client:
resp = client.get(url, params=params, headers=headers)
resp.raise_for_status()
payload = resp.json()
count_val = None
if isinstance(payload, dict):
count_val = payload.get("file_count")
if count_val is None:
count_val = payload.get("file_count_inclusive")
if count_val is None:
count_val = payload.get("num_files")
if isinstance(count_val, int):
self.total_count = count_val
return self.total_count
except Exception as exc:
debug(f"{self._log_prefix()} total count (json) unavailable: {exc}", file=sys.stderr)
# 2) Fallback to the API client (CBOR).
try:
payload = self._client.search_files(
tags=["system:everything"],
@@ -132,8 +182,11 @@ class HydrusNetwork(Store):
count_val = payload.get("num_files")
if isinstance(count_val, int):
self.total_count = count_val
return self.total_count
except Exception as exc:
debug(f"{self._log_prefix()} total count unavailable: {exc}", file=sys.stderr)
debug(f"{self._log_prefix()} total count (client) unavailable: {exc}", file=sys.stderr)
return self.total_count
def name(self) -> str:
return self.NAME