douyinWEB/scripts/_sync_cookie_and_relay.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

66 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Sync sh2 抖音cookie → VPS dy_cookie + deploy relay cookie/failover fixes."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "backend"))
from app.cookie_loader import find_sh2_cookie # noqa: E402
cookie, source = find_sh2_cookie()
print(f"local cookie len={len(cookie)} from {source}")
if not cookie:
raise SystemExit("no local sh2 cookie")
HOST = "108.174.50.68"
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel in (
"backend/app/youtube_relay.py",
"backend/app/youtube_relay_pipeline.py",
):
print("upload", rel)
sftp.put(str(ROOT / rel), f"/opt/douyinWEB/{rel}")
# write cookie file then import via python to avoid shell escaping
remote_cookie = "/tmp/dy_cookie_sync.txt"
with sftp.file(remote_cookie, "w") as f:
f.write(cookie)
sftp.close()
script = f"""
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from pathlib import Path
from app import db
cookie = Path('/tmp/dy_cookie_sync.txt').read_text(encoding='utf-8').strip()
print('importing cookie len', len(cookie))
db.update_settings_map({{'dy_cookie': cookie}})
cfg = db.get_settings_map()
print('db dy_cookie len', len(str(cfg.get('dy_cookie') or '')))
PY
rm -f /tmp/dy_cookie_sync.txt
systemctl kill -s SIGKILL douyinweb-api 2>/dev/null || true
sleep 1
systemctl reset-failed douyinweb-api 2>/dev/null || true
systemctl start douyinweb-api
sleep 4
systemctl is-active douyinweb-api
"""
_, o, e = c.exec_command(script, timeout=60)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace").strip()
if err:
print("ERR:", err[:500])
c.close()