# -*- coding: utf-8 -*- """ Safe finish for caption_pool: - NEVER upload/overwrite monitor.db, covers, clips, cookies, pb_data - ONLY create/seed caption_pool table (new text rows) - Restart services if needed; verify db size does not shrink """ from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REPO = "/opt/douyinWEB" DB = f"{REPO}/backend/data/monitor.db" c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False) def run(cmd: str, timeout: int = 300) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace").strip() def db_size() -> str: return run(f"stat -c '%s' {DB}") print("=== SAFE CAPTION POOL FINISH (code/table only) ===") before = db_size() print("[db size before]", before) print("[commit]", run(f"cd {REPO} && git log --oneline -1 && git status -sb | head -5")) # Wait / gently restart API only (no data ops) st = run("systemctl is-active douyinweb-api", timeout=20) print("[api now]", st) if st != "active": print("[restart api gently]") run("systemctl restart douyinweb-api", timeout=30) for i in range(40): st = run("systemctl is-active douyinweb-api", timeout=20) print(" wait", i, st) if st == "active": break time.sleep(2) # Seed ONLY caption_pool via app code (CREATE TABLE IF NOT EXISTS + insert if empty) print("[seed caption_pool only — no full init_db side effects]") print( run( f"""cd {REPO}/backend && .venv/bin/python - <<'PY' from app import caption_pool from app import db as D with D._lock: conn = D._connect() try: before_tables = {{r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()}} counts = caption_pool.seed_caption_pool(conn, force=False) conn.commit() after_tables = {{r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()}} rows = conn.execute( "SELECT platform, COUNT(*) n FROM caption_pool GROUP BY platform" ).fetchall() print('SEED', counts) print('COUNTS', {{r['platform']: r['n'] for r in rows}}) print('NEW_TABLES', sorted(after_tables - before_tables)) # business table row counts (read-only) for t in ('rooms','sessions','shorts_channels','shorts_jobs','tiktok_channels','tiktok_jobs'): try: n = conn.execute(f'SELECT COUNT(*) c FROM {{t}}').fetchone()['c'] print(f'ROWS {{t}}={{n}}') except Exception as ex: print(f'ROWS {{t}} err {{ex}}') finally: conn.close() print('PICK', caption_pool.pick_caption('x', custom='street food')) PY""", timeout=120, ) ) after = db_size() print("[db size after]", after) print("[size delta bytes]", int(after) - int(before) if before.isdigit() and after.isdigit() else "?") # Frontend: only rebuild if clash/caption UI files present; never touch data print("[web]", run("systemctl is-active douyinweb-web")) need_build = run( f"grep -n '100 条英文' {REPO}/frontend/src/app/tiktok/page.tsx >/dev/null && echo UI_OK || echo UI_OLD" ) print("[ui]", need_build) if "UI_OLD" in need_build or "UI_OK" in need_build: # rebuild only if source has new strings but .next may be stale — check source src_ok = "100" in run(f"grep -c '自定义文案' {REPO}/frontend/src/app/tiktok/page.tsx || true") print("[src has 自定义文案]", run(f"grep -c '自定义文案' {REPO}/frontend/src/app/tiktok/page.tsx || echo 0")) print("[npm build]") build = run(f"cd {REPO}/frontend && npm run build", timeout=360) for line in build.splitlines()[-15:]: print(line.encode("ascii", "replace").decode("ascii")) run("systemctl restart douyinweb-web", timeout=30) time.sleep(4) print("[services]", run("systemctl is-active douyinweb-api douyinweb-web")) print("[db size final]", db_size()) print( "[fingerprint]", run( f"stat -c '%n size=%s' {DB}; " f"du -sb {REPO}/backend/data/covers {REPO}/backend/data/clips {REPO}/backend/data/tiktok_cookies 2>/dev/null" ), ) # Do NOT apply any git stash print("[stash untouched]", run(f"cd {REPO} && git stash list | head -3")) c.close() print("DONE — no business data uploaded/overwritten")