# -*- coding: utf-8 -*- """Unstick physical API if hung; verify live-map.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") def main(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=25) pw = "4669805" def sh(cmd, t=60): _, o, e = c.exec_command(f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}", timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") print("=== before ===") print(sh("systemctl is-active douyinweb-api; ss -lntp | grep 8014 || true; ps -eo pid,etime,cmd | grep -E 'uvicorn|douyinweb' | grep -v grep | head")) print(sh("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo fail")) print("=== restart api ===") print(sh("systemctl restart douyinweb-api", 40)) for i in range(15): time.sleep(2) h = sh("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x").strip() print("health", h) if h.endswith("200"): break # ensure latest youtube_live.py is in place print(sh("wc -c /opt/douyinWEB/backend/app/youtube_live.py; grep -n 'Receiving-first' /opt/douyinWEB/backend/app/youtube_live.py | head")) verify = r''' import json, urllib.request, http.cookiejar, time cj=http.cookiejar.CookieJar() op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST") print("login", op.open(req, timeout=10).status) t0=time.time() raw=op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=150).read() p=json.loads(raw) print("live_count", p.get("live_count"), "source", p.get("source"), "dt", round(time.time()-t0,1)) for k,v in sorted((p.get("entries") or {}).items()): if v.get("is_live") or (str(k).startswith("assign:") and (v.get("studio") or {}).get("on_air")): print((" LIVE" if v.get("is_live") else " snap"), k[:40], v.get("source"), (v.get("title") or "")[:30], "studio", (v.get("studio") or {}).get("live_status"), (v.get("studio") or {}).get("on_air"), (v.get("live_url") or "")[:55]) ''' sftp = c.open_sftp() with sftp.file("/tmp/_vfix.py", "w") as f: f.write(verify) sftp.close() print(sh("/opt/douyinWEB/backend/.venv/bin/python /tmp/_vfix.py", 200)) c.close() if __name__ == "__main__": main()