douyinWEB/scripts/deploy_yt_analytics.py
2026-08-10 20:11:23 -05:00

82 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy YouTube analytics (groups tab + API)."""
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REMOTE = "/opt/douyinWEB"
LOCAL = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/db.py",
"backend/app/main.py",
"frontend/src/lib/realtime.ts",
"frontend/src/app/groups/page.tsx",
"frontend/src/app/page.tsx",
]
def run(c, cmd, timeout=420):
print("$", cmd[:180], flush=True)
chan = c.get_transport().open_session()
chan.settimeout(timeout)
chan.get_pty()
chan.exec_command(cmd)
buf = []
while True:
if chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
if chan.exit_status_ready() and not chan.recv_ready():
break
time.sleep(0.25)
while chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
out = "".join(buf)
print(out[-2000:], flush=True)
return chan.recv_exit_status()
def main():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASSWORD, timeout=30)
sftp = c.open_sftp()
for rel in FILES:
print("upload", rel, flush=True)
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
sftp.close()
run(
c,
"systemctl kill -s SIGKILL douyinweb-api || true; sleep 1; "
"systemctl reset-failed douyinweb-api || true; "
"systemctl start douyinweb-api; sleep 4; systemctl is-active douyinweb-api",
timeout=90,
)
py = r"""
from app.session_auth import create_session_token, get_admin_credentials, SESSION_COOKIE
import httpx
user,_=get_admin_credentials(); tok=create_session_token(user)
with httpx.Client(base_url='http://127.0.0.1:8014', timeout=60, cookies={SESSION_COOKIE: tok}) as c:
r=c.get('/api/groups/analytics')
print('status', r.status_code)
d=r.json()
st=(d.get('stats') or {}).get('overview') or {}
print('sessions', st.get('sessions'), 'hours', st.get('total_hours'), 'active', st.get('active_sessions'))
print('channels', len(d.get('channels') or []), 'recent', len(d.get('recent_sessions') or []))
"""
run(c, "cd /opt/douyinWEB/backend && .venv/bin/python - <<'PY'\n" + py + "\nPY\n", timeout=120)
if run(c, "cd /opt/douyinWEB/frontend && npm run build", timeout=420) != 0:
raise SystemExit(1)
run(c, "systemctl restart douyinweb-web --no-block; sleep 3; systemctl is-active douyinweb-api douyinweb-web")
c.close()
print("DONE")
if __name__ == "__main__":
main()