navmini/xhs/scripts/sync_data.py
eric 518410edd0 feat: 游民导航 H5、微信小程序与小红书小工具
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-27 12:15:00 -05:00

69 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Sync H5 links.json + icons into xhs/tool for offline packaging."""
from __future__ import annotations
import json
import shutil
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1] # xhs/
PROJ = ROOT.parent # navmini/
SRC_JSON = PROJ / "src" / "data" / "links.json"
SRC_ICONS = PROJ / "public" / "icons"
TOOL = ROOT / "tool"
OUT_ICONS = TOOL / "icons"
OUT_DATA = TOOL / "nav-data.js"
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)
# wipe old icons except keep dir
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)
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)
# brand icon path for UI
data["brandIcon"] = "./icons/_brand.webp"
payload = json.dumps(data, ensure_ascii=False, indent=2)
OUT_DATA.write_text(
"/* generated by scripts/sync_data.py — do not edit */\n"
f"var NAV_DATA = {payload};\n",
encoding="utf-8",
)
icon_count = len(list(OUT_ICONS.glob("*.webp")))
print(f"synced nav-data.js + {icon_count} icons -> {TOOL}")
return 0
if __name__ == "__main__":
raise SystemExit(main())