nomadro/apps/admin/deploy/_fix_vip_youtube_ebook.py
Eric dfc5f2d502 Fix VIP Youtube notes routing and VIP/join-group switch sync for weapp audit.
Tag 20261011-2: youtube-notes opens reader?id=default; VIP center toggle drives join popup vs VIP jump.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 19:42:28 +08:00

138 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy vip_content.py + patch remote-config: Youtube运营笔记 → books/default."""
from __future__ import annotations
import json
import time
from pathlib import Path
import paramiko
REPO = Path(__file__).resolve().parents[3]
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
SRC = REPO / "apps/api/services/ebook_svc/services/vip_content.py"
YT = {
"id": "youtube-notes",
"title": "Youtube运营笔记",
"desc": "试读可进,单买或 VIP 解锁全文",
"ebookId": "default",
"href": "/pages/reader/index?id=default",
"free": False,
"enabled": True,
"sort": 1,
}
def to_app_pkg(src: Path) -> bytes:
return src.read_text(encoding="utf-8").replace("ebook_svc.", "app.").encode("utf-8")
def main() -> int:
data = to_app_pkg(SRC)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.file("/tmp/vip_content_yt.py", "wb") as rf:
rf.write(data)
print("up vip_content", len(data), flush=True)
sftp.close()
patch_json = json.dumps(YT, ensure_ascii=False)
cmd = r"""
set -e
B=/opt/nomadmini/backend/app
sudo cp /tmp/vip_content_yt.py "$B/services/vip_content.py"
# patch remote-config ebooks
python3 <<'PY'
import json
from pathlib import Path
paths = [
Path("/var/www/nomadro.cn/data/remote-config.json"),
Path("/opt/nomadmini/backend/data/remote-config.json"),
]
yt = {
"id": "youtube-notes",
"title": "Youtube运营笔记",
"desc": "试读可进,单买或 VIP 解锁全文",
"ebookId": "default",
"href": "/pages/reader/index?id=default",
"free": False,
"enabled": True,
"sort": 1,
}
for p in paths:
if not p.is_file():
print("skip missing", p)
continue
raw = json.loads(p.read_text(encoding="utf-8"))
platforms = raw.setdefault("platforms", {})
weapp = platforms.setdefault("weapp", {})
vc = weapp.setdefault("vipContent", {})
ebooks = list(vc.get("ebooks") or [])
hit = -1
for i, e in enumerate(ebooks):
if not isinstance(e, dict):
continue
t = str(e.get("title") or "")
if (
e.get("id") == "youtube-notes"
or e.get("ebookId") == "default"
or "youtube" in t.lower()
or "运营笔记" in t
):
hit = i
break
if hit >= 0:
prev = ebooks[hit]
ebooks[hit] = {**yt, "title": prev.get("title") or yt["title"], "desc": prev.get("desc") or yt["desc"], "enabled": prev.get("enabled") is not False, "sort": prev.get("sort", 1)}
else:
idx = next((i for i, e in enumerate(ebooks) if isinstance(e, dict) and (e.get("id") == "1" or e.get("title") == "数字游民")), -1)
if idx >= 0:
ebooks.insert(idx + 1, dict(yt))
else:
ebooks.append(dict(yt))
# fix cloudphone sort if needed
for e in ebooks:
if isinstance(e, dict) and e.get("id") == "2" and e.get("sort") in (None, 1):
e["sort"] = 2
vc["ebooks"] = ebooks
stats = dict(vc.get("stats") or {})
stats["ebooks"] = "3"
vc["stats"] = stats
weapp["vipContent"] = vc
platforms["weapp"] = weapp
raw["platforms"] = platforms
raw["updatedAt"] = raw.get("updatedAt") or ""
bak = p.with_suffix(p.suffix + ".bak-yt")
bak.write_text(p.read_text(encoding="utf-8"), encoding="utf-8")
p.write_text(json.dumps(raw, ensure_ascii=False, indent=2), encoding="utf-8")
print("patched", p, "ebooks=", [e.get("id") for e in ebooks if isinstance(e, dict)])
PY
cd /opt/nomadmini/backend
.venv/bin/python -c 'from app.services import vip_content as v; d=v.default_vip_content(); ids=[e["id"] for e in d["ebooks"]]; assert "youtube-notes" in ids; print("default_ok", ids)'
sudo systemctl restart nomadmini-api
sleep 3
systemctl is-active nomadmini-api
curl -sk https://api.nomadro.cn/api/config | python3 -c 'import sys,json; d=json.load(sys.stdin); e=((d.get("config") or {}).get("platforms") or {}).get("weapp",{}).get("vipContent",{}).get("ebooks") or []); print("live_ebooks", [(x.get("id"), x.get("ebookId"), x.get("href")) for x in e])'
rm -f /tmp/vip_content_yt.py
"""
_, stdout, _ = c.exec_command(cmd, timeout=180, get_pty=True)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
time.sleep(0.05)
while stdout.channel.recv_ready():
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
code = stdout.channel.recv_exit_status()
print(f"[exit {code}]", flush=True)
c.close()
return code
if __name__ == "__main__":
raise SystemExit(main())