#!/usr/bin/env python3 # -*- coding: utf-8 -*- 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] DATA = (ROOT / "backend" / "app" / "youtube_live.py").read_bytes() VERIFY = r''' import asyncio, os, sys os.chdir("/opt/douyinWEB/backend"); sys.path.insert(0, ".") from app import youtube_live print("PROXY", os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") or "-") # physical often only sets proxy on douyinweb-yt; fall back to ShellCrash if not (os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")): os.environ["HTTPS_PROXY"] = "http://127.0.0.1:7890" os.environ["HTTP_PROXY"] = "http://127.0.0.1:7890" print("PROXY_FALLBACK", os.environ["HTTPS_PROXY"]) youtube_live._LAST_OAUTH_AT = 0.0 async def main(): for cid, name in [ ("UCXFErW9DeE8EXrInlnyCnVw", "asian"), ("UCYnwXkCMmVFf8Vr6MlWb-Jw", "hack"), ("UCf-uYs6f5yg3iWylnHkIKug", "nomadSF"), ]: r = youtube_live._http_channel_live_probe(cid) print("http", name, "live", r.get("is_live"), "err", (r.get("error") or "")[:70], "url", (r.get("live_url") or "")[:55]) m = await youtube_live.build_live_map(force=True) live = [ (k, v.get("source"), (v.get("title") or "")[:36]) for k, v in (m.get("entries") or {}).items() if v.get("is_live") and str(k).startswith("assign:") ] print("live_count", m.get("live_count"), "assign_live", len(live)) for row in live: print(" ", row) asyncio.run(main()) ''' def run(c, cmd, t=120): _, 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/_yl2.py", "wb") as f: f.write(DATA) with sftp.file("/tmp/_v2.py", "w") as f: f.write(VERIFY) sftp.close() dst = "/opt/douyinWEB/backend/app/youtube_live.py" if sudo: print(run(c, f"echo '{pw}' | sudo -S -p '' bash -lc \"cat /tmp/_yl2.py > {dst}; systemctl restart douyinweb-api\"", 50)) else: print(run(c, f"cat /tmp/_yl2.py > {dst}; systemctl restart douyinweb-api >/tmp/r.log 2>&1 &", 15)) for i in range(12): 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 == 5 and not sudo: run(c, "systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api", 30) print(run(c, "systemctl show douyinweb-api -p Environment --no-pager | head -c 500")) py = "/opt/douyinWEB/backend/.venv/bin/python /tmp/_v2.py" if sudo: py = f"echo '{pw}' | sudo -S -p '' {py}" print(run(c, py, 200)[:2500]) c.close() def main(): deploy("107.173.30.245", 22, "root", "Xiao4669805", False) deploy("107.173.30.245", 4014, "eric", "4669805", True) if __name__ == "__main__": main()