refactor(build_community_interfaces): streamline JSON fetching by replacing custom fetch function with a dedicated interface builder

This commit is contained in:
Ivan
2026-04-27 11:16:05 -05:00
parent e9d6264293
commit bbc067b7c1
+8 -20
View File
@@ -7,11 +7,11 @@ import argparse
import json
import sys
import urllib.error
import urllib.request
from pathlib import Path
from meshchatx.src.backend.community_interfaces_directory import (
DEFAULT_SUBMITTED_URL,
build_interfaces_from_directory_url,
rows_from_payload,
transform_directory_rows,
)
@@ -20,19 +20,6 @@ ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "meshchatx" / "src" / "backend" / "data" / "community_interfaces.json"
def fetch_json(url: str, timeout: float = 60.0) -> dict | list:
req = urllib.request.Request(
url,
headers={
"Accept": "application/json",
"User-Agent": "MeshChatX-community-interfaces-build/1.0 (+https://meshchatx.com/)",
},
method="GET",
)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8"))
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
@@ -58,8 +45,13 @@ def main() -> int:
try:
if args.source:
payload = json.loads(Path(args.source).read_text(encoding="utf-8"))
rows = rows_from_payload(payload)
out_list = transform_directory_rows(rows)
used_url = str(Path(args.source).resolve())
else:
payload = fetch_json(args.url)
out_list, used_url = build_interfaces_from_directory_url(
args.url, timeout=60.0
)
except OSError as e:
print(f"Read/fetch failed: {e}", file=sys.stderr)
return 1
@@ -69,19 +61,15 @@ def main() -> int:
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}", file=sys.stderr)
return 1
try:
rows = rows_from_payload(payload)
except ValueError as e:
print(e, file=sys.stderr)
return 1
out_list = transform_directory_rows(rows)
doc = {
"_comment": "build_community_interfaces_json.py; source: directory.rns.recipes online listings. "
"RNode omitted. Backbone without transport_identity -> TCPClientInterface. "
"Optional override: public/community_interfaces.json.",
"_source": args.url if not args.source else str(Path(args.source).resolve()),
"_source": used_url,
"interfaces": out_list,
}
args.output.parent.mkdir(parents=True, exist_ok=True)