# -*- coding: utf-8 -*- """Fix audit book API 500 (missing chapter order) + deploy read_policy/store.""" from __future__ import annotations import time from pathlib import Path import paramiko REPO = Path(__file__).resolve().parents[3] HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" FILES = [ ( REPO / "apps/api/services/ebook_svc/domains/books/read_policy.py", "/opt/nomadmini/backend/app/domains/books/read_policy.py", ), ( REPO / "apps/api/services/ebook_svc/services/store.py", "/opt/nomadmini/backend/app/services/store.py", ), ] def main() -> int: 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() for src, dest in FILES: data = src.read_text(encoding="utf-8").replace("ebook_svc.", "app.").encode("utf-8") tmp = f"/tmp/{Path(dest).name}" with sftp.file(tmp, "wb") as rf: rf.write(data) print("upload", src.name, flush=True) sftp.close() cmd = r""" set -e sudo cp /tmp/read_policy.py /opt/nomadmini/backend/app/domains/books/read_policy.py sudo cp /tmp/store.py /opt/nomadmini/backend/app/services/store.py python3 - <<'PY' import json, sqlite3, time db='/opt/nomadmini/data/nomadmini.db' con=sqlite3.connect(db) row=con.execute("select chapters_json from books where id='audit'").fetchone() if row: ch=json.loads(row[0] or '[]') fixed=[] for i,c in enumerate(ch if isinstance(ch,list) else []): if not isinstance(c, dict): continue c=dict(c) c['id']=str(c.get('id') or f'ch-{i+1}') c['title']=str(c.get('title') or f'章节 {i+1}') c['order']=int(c['order']) if c.get('order') is not None else i+1 c['content']=str(c.get('content') or '') fixed.append(c) if not fixed: fixed=[{ 'id':'01-audit','title':'欢迎阅读','order':1, 'content':'# 数字游民·提审版\n\n这是微信提审用的独立内容,与会员正式书无关。\n' }] con.execute( "update books set title=?, subtitle=?, unlock_percent=100, price_fen=0, product_id='', chapters_json=?, updated_at=? where id='audit'", ('数字游民·提审版','微信提审专用 · 与正式会员书无关', json.dumps(fixed, ensure_ascii=False), time.time()) ) con.commit() print('fixed chapters', len(fixed), 'orders', [x['order'] for x in fixed]) else: print('no audit row') PY sudo systemctl restart nomadmini-api sleep 2 systemctl is-active nomadmini-api curl -sk -o /tmp/audit.json -w "books/audit %{http_code}\n" "https://api.nomadro.cn/api/ebook/books/audit" python3 - <<'PY' import json d=json.load(open('/tmp/audit.json',encoding='utf-8')) print('title', d.get('title'), 'chapters', len(d.get('chapters') or []), 'free', d.get('is_free')) print('ch0', (d.get('chapters') or [{}])[0]) PY curl -sk -o /tmp/audit_read.json -w "books/audit/read %{http_code}\n" "https://api.nomadro.cn/api/ebook/books/audit/read" python3 - <<'PY' import json d=json.load(open('/tmp/audit_read.json',encoding='utf-8')) chs=d.get('chapters') or [] print('read chapters', len(chs), 'title', (d.get('book') or {}).get('title')) if chs: print('body', (chs[0].get('content') or '')[:120].replace('\n',' ')) PY rm -f /tmp/read_policy.py /tmp/store.py /tmp/audit.json /tmp/audit_read.json """ _, stdout, _ = c.exec_command(cmd, timeout=120, 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())