douyinWEB/scripts/_deploy_live_probe_tighten.py
2026-09-13 22:10:49 -05:00

105 lines
3.8 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy tightened live probes (http_live + youtube.mjs) to VPS + physical."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
YL = (ROOT / "backend" / "app" / "youtube_live.py").read_bytes()
YT = (ROOT / "youtube-svc" / "src" / "youtube.mjs").read_bytes()
VERIFY = r'''
import json, urllib.request, http.cookiejar
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=15).status)
raw=op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=120).read()
p=json.loads(raw)
print("live_count", p.get("live_count"), "source", p.get("source"), "svc", (p.get("svc_error") or "")[:80])
for k,v in sorted((p.get("entries") or {}).items()):
if v.get("is_live") and str(k).startswith("assign:"):
print(" LIVE", k, v.get("source"), (v.get("title") or "")[:36], (v.get("live_url") or "")[:70])
# also http probe via venv
import os,sys
os.chdir("/opt/douyinWEB/backend"); sys.path.insert(0,".")
from app import youtube_live
for cid,name in [
("UCXFErW9DeE8EXrInlnyCnVw","asian_UCXF"),
("UCjQ8jnBm2N4FGRowNXcXThw","asian_UCjQ"),
("UCYnwXkCMmVFf8Vr6MlWb-Jw","hack"),
("UCf-uYs6f5yg3iWylnHkIKug","nomadSF"),
]:
r=youtube_live._http_channel_live_probe(cid)
print("http", name, "live", r.get("is_live"), "title", (r.get("title") or "")[:40], "err", (r.get("error") or "-")[:60])
'''
def run(c, cmd, t=180):
_, o, e = c.exec_command(cmd, timeout=t)
o.channel.settimeout(t)
try:
return (o.read() + e.read()).decode("utf-8", "replace")
except Exception as ex:
return str(ex)
def deploy(host, port, user, pw, sudo=False):
print(f"\n===== {user}:{port} =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.file("/tmp/_yl_fix.py", "wb") as f:
f.write(YL)
with sftp.file("/tmp/_yt_fix.mjs", "wb") as f:
f.write(YT)
with sftp.file("/tmp/_v_live.py", "w") as f:
f.write(VERIFY)
sftp.close()
yl = "/opt/douyinWEB/backend/app/youtube_live.py"
yt = "/opt/douyinWEB/youtube-svc/src/youtube.mjs"
if sudo:
run(
c,
f"echo '{pw}' | sudo -S -p '' bash -lc \"cat /tmp/_yl_fix.py > {yl}; cat /tmp/_yt_fix.mjs > {yt}; "
f"systemctl restart douyinweb-yt; systemctl restart douyinweb-api\"",
60,
)
else:
run(c, f"cat /tmp/_yl_fix.py > {yl}; cat /tmp/_yt_fix.mjs > {yt}", 20)
run(c, "systemctl restart douyinweb-yt; systemctl restart douyinweb-api >/tmp/r.log 2>&1 &", 15)
for i in range(14):
time.sleep(2)
h = run(c, "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 == "200":
break
if i == 6 and not sudo:
run(
c,
"systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api",
30,
)
py = "/opt/douyinWEB/backend/.venv/bin/python /tmp/_v_live.py"
if sudo:
py = f"echo '{pw}' | sudo -S -p '' {py}"
print(run(c, py, 240)[:4000])
c.close()
if __name__ == "__main__":
deploy("107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
deploy("107.173.30.245", 4014, "eric", "4669805", sudo=True)