nomadro/apps/miniprogram/xhs/scripts/sync_data.py
eric 69b0bd8224 Ship Taro H5 Vite build and Next-parity hub/nav/book/vip pages.
Switch miniprogram H5 to Vite for fast local compile; align portal pages and static icons for cross-machine continue.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 04:36:12 -05:00

166 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""打包小红书离线数据:门户配置 + 导航 links + 小册/社群兜底。
对齐 H5 nomadro 首页模块,纯前端零网络。
"""
from __future__ import annotations
import json
import shutil
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MINI = ROOT.parent # apps/miniprogram
REPO = MINI.parent.parent # repo root
SRC_JSON = MINI / "src" / "data" / "links.json"
NAVMINI_JSON = MINI.parent / "navmini" / "src" / "data" / "links.json"
if not SRC_JSON.is_file():
SRC_JSON = NAVMINI_JSON
SRC_ICONS = MINI.parent / "navmini" / "public" / "icons"
if not SRC_ICONS.is_dir():
SRC_ICONS = MINI / "src" / "assets" / "icons"
CFG_JSON = REPO / "data" / "remote-config.json"
TOOL = ROOT / "tool"
OUT_ICONS = TOOL / "icons"
OUT_DATA = TOOL / "portal-data.js"
# 小册 / 社群离线种子(与 H5 模块一致,包内兜底)
BOOK_OFFLINE = {
"title": "数字游民一人公司小册",
"subtitle": "离线预览 · 完整章节见 H5 /book",
"chapters": [
{
"id": "c1",
"title": "为什么做数字游民",
"body": "地理套利、远程收入与低成本生活,是 nomadro 的核心命题。\n\n本页为小红书纯前端离线兜底;完整阅读请打开 nomadro.cn/book。",
},
{
"id": "c2",
"title": "一人公司最小闭环",
"body": "获客 → 交付 → 收款 → 复购。先跑通一条最小闭环,再谈规模化。",
},
],
}
COMMUNITY_OFFLINE = {
"meetups": [
{
"id": "online-roundtable",
"emoji": "🎙️",
"title": "远程工作节奏圆桌",
"city": "线上",
"date": "近期",
"desc": "异步协作、时区管理与旅居城市选择。",
},
{
"id": "chiangmai-cowork",
"emoji": "☕",
"title": "清迈联合办公下午茶",
"city": "清迈",
"date": "近期",
"desc": "签证经验、住宿踩坑与同城社交。",
},
{
"id": "lisbon-sunset",
"emoji": "🌅",
"title": "里斯本日落漫步",
"city": "里斯本",
"date": "近期",
"desc": "同城游民日落局,交换欧洲签证情报。",
},
]
}
def main() -> int:
if not SRC_JSON.is_file():
print(f"missing links {SRC_JSON}", file=sys.stderr)
return 1
links = json.loads(SRC_JSON.read_text(encoding="utf-8"))
cfg = {
"homeBrand": "nomadro",
"homeTagline": "数字游民操作系统",
"homeLead": "导航 · 小册 · 社群 · 离线可用",
"modules": {
"nav": {"enabled": True, "title": "出海导航", "emoji": "🧭", "desc": "工具与出海资源"},
"book": {"enabled": True, "title": "数字游民小册", "emoji": "📖", "desc": "离线预览章节"},
"community": {"enabled": True, "title": "旅居社群", "emoji": "🌏", "desc": "活动与目的地"},
},
}
if CFG_JSON.is_file():
raw = json.loads(CFG_JSON.read_text(encoding="utf-8"))
copy = raw.get("copy") or {}
mods = raw.get("modules") or {}
cfg["homeBrand"] = copy.get("homeBrand") or cfg["homeBrand"]
if copy.get("homeTagline"):
cfg["homeTagline"] = copy["homeTagline"]
if copy.get("homeLead"):
cfg["homeLead"] = copy["homeLead"]
for key in ("nav", "book", "community"):
m = mods.get(key) or {}
if key in cfg["modules"]:
if m.get("title"):
cfg["modules"][key]["title"] = m["title"]
if m.get("enabled") is False:
cfg["modules"][key]["enabled"] = False
OUT_ICONS.mkdir(parents=True, exist_ok=True)
for old in OUT_ICONS.glob("*"):
if old.is_file():
old.unlink()
needed = ["_brand", "nomadro"]
for cat in links.get("categories", []):
for group in cat.get("groups", []):
for link in group.get("links", []):
lid = str(link.get("id") or "").strip()
if not lid:
continue
needed.append(lid)
link["icon"] = f"./icons/{lid}.webp"
brand_src = SRC_ICONS / "_brand.webp"
if not brand_src.is_file():
brand_src = SRC_ICONS / "nomadro.webp"
missing = []
for lid in dict.fromkeys(needed):
src = SRC_ICONS / f"{lid}.webp"
dst = OUT_ICONS / f"{lid}.webp"
if src.is_file():
shutil.copy2(src, dst)
elif brand_src.is_file():
shutil.copy2(brand_src, dst)
missing.append(lid)
else:
missing.append(lid)
if missing:
print("warn icons fallback:", ", ".join(missing[:12]), file=sys.stderr)
links["brandIcon"] = "./icons/nomadro.webp" if (OUT_ICONS / "nomadro.webp").is_file() else "./icons/_brand.webp"
portal = {
"config": cfg,
"nav": links,
"book": BOOK_OFFLINE,
"community": COMMUNITY_OFFLINE,
}
payload = json.dumps(portal, ensure_ascii=False, indent=2)
OUT_DATA.write_text(
"/* generated — offline seed for xhs minitool */\n"
f"var PORTAL_DATA = {payload};\n"
"var NAV_DATA = PORTAL_DATA.nav;\n",
encoding="utf-8",
)
cats = len(links.get("categories") or [])
print(f"wrote {OUT_DATA} modules={list(cfg['modules'])} nav_cats={cats}")
print(f"icons {SRC_ICONS} -> {OUT_ICONS} count={len(list(OUT_ICONS.glob('*.webp')))}")
return 0
if __name__ == "__main__":
raise SystemExit(main())