nomadro/apps/navmini/weapp/scripts/sync_data.py
eric d93a355fd5 Migrate navmini/ebook/nomadweb into apps/ as same-origin portal subpaths.
Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:30:34 -05:00

70 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Sync H5 links.json + icons into weapp/ (local icons only, no CDN)."""
from __future__ import annotations
import json
import shutil
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1] # weapp/
PROJ = ROOT.parent # navmini/
SRC_JSON = PROJ / "src" / "data" / "links.json"
SRC_ICONS = PROJ / "public" / "icons"
OUT_JSON = ROOT / "data" / "links.json"
OUT_JS = ROOT / "data" / "links.js"
OUT_ICONS = ROOT / "icons"
def main() -> int:
if not SRC_JSON.is_file():
print(f"missing {SRC_JSON}", file=sys.stderr)
return 1
data = json.loads(SRC_JSON.read_text(encoding="utf-8"))
OUT_ICONS.mkdir(parents=True, exist_ok=True)
OUT_JSON.parent.mkdir(parents=True, exist_ok=True)
for old in OUT_ICONS.glob("*"):
if old.is_file():
old.unlink()
needed: list[str] = ["_brand"]
for cat in data.get("categories", []):
for group in cat.get("groups", []):
for link in group.get("links", []):
lid = link["id"]
needed.append(lid)
# 强制本地路径,禁止 CDN / 远程 URL
link["icon"] = f"/icons/{lid}.webp"
missing = []
for lid in needed:
src = SRC_ICONS / f"{lid}.webp"
dst = OUT_ICONS / f"{lid}.webp"
if not src.is_file():
missing.append(lid)
continue
shutil.copy2(src, dst)
if missing:
print("warn missing icons:", ", ".join(missing), file=sys.stderr)
data["brandIcon"] = "/icons/_brand.webp"
payload = json.dumps(data, ensure_ascii=False, indent=2)
OUT_JSON.write_text(payload + "\n", encoding="utf-8")
OUT_JS.write_text(
"/* generated by scripts/sync_data.py — do not edit */\n"
f"module.exports = {payload};\n",
encoding="utf-8",
)
icon_count = len(list(OUT_ICONS.glob("*.webp")))
print(f"synced data/links.json + links.js + {icon_count} icons -> {ROOT}")
return 0
if __name__ == "__main__":
raise SystemExit(main())