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

106 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""Re-check live-map + yt_studio_samples on_air on both hosts."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
REMOTE = r'''
import json, sqlite3, urllib.request, http.cookiejar, time
con=sqlite3.connect("/opt/douyinWEB/backend/data/monitor.db")
con.row_factory=sqlite3.Row
print("=== yt_studio_samples last 20 with on_air/enc ===")
try:
rows=list(con.execute("""
select ts, name, group_id, youtube_channel_id, on_air, live_status, health, neko_ref, studio_url, detect_source
from yt_studio_samples s
left join yt_assign_groups g on g.id=s.group_id
order by s.ts desc limit 20
"""))
except Exception:
rows=list(con.execute("""
select ts, group_id, youtube_channel_id, on_air, live_status, health, neko_ref, studio_url, detect_source
from yt_studio_samples order by ts desc limit 20
"""))
now=time.time()
for r in rows:
d=dict(r)
age=now-float(d.get("ts") or 0)
# handle weird future ts
if age < -1e6: age = float(d.get("ts") or 0) # leave raw
print(
"age", round(age,1) if isinstance(age,float) and abs(age)<1e7 else d.get("ts"),
"on_air", d.get("on_air"),
"st", d.get("live_status"),
"ref", d.get("neko_ref"),
"name", d.get("name") if "name" in d else "",
"url", (d.get("studio_url") or "")[:50],
)
# latest telemetry events
print("=== latest_studio_telemetry ===")
import sys, os
sys.path.insert(0,"/opt/douyinWEB/backend")
os.chdir("/opt/douyinWEB/backend")
from app import youtube_neko
for ev in youtube_neko.latest_studio_telemetry(limit=12):
pl=ev.get("payload") if isinstance(ev.get("payload"), dict) else {}
sig=pl.get("signals") if isinstance(pl.get("signals"), dict) else {}
print(
"recv_at", ev.get("received_at"),
"ref", ev.get("neko_ref") or pl.get("neko_ref"),
"st", pl.get("live_status"),
"on_air", sig.get("on_air"),
"enc", sig.get("encoder_receiving"),
"url", (pl.get("url") or "")[:55],
)
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")
op.open(req, timeout=15)
p=json.loads(op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=120).read())
print("live_count", p.get("live_count"), "source", p.get("source"))
for k,v in sorted((p.get("entries") or {}).items()):
if str(k).startswith("assign:"):
st=(v.get("studio") or {})
print((" LIVE" if v.get("is_live") else " off "), (v.get("title") or k)[:28], v.get("source"),
"studio_on_air", st.get("on_air"), "enc", st.get("encoder_receiving"), "st", st.get("live_status"),
(v.get("live_url") or "")[:50])
'''
def run(port, user, pwd, label, sudo=False):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", port=port, username=user, password=pwd, timeout=25)
if sudo:
sftp = c.open_sftp()
with sftp.file("/tmp/_lm_now.py", "w") as f:
f.write(REMOTE)
sftp.close()
cmd = f"echo '{pwd}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_lm_now.py"
else:
cmd = "/opt/douyinWEB/backend/.venv/bin/python -"
stdin, stdout, stderr = c.exec_command(cmd, timeout=180)
if not sudo:
stdin.write(REMOTE)
stdin.channel.shutdown_write()
print("########", label, "########")
print(stdout.read().decode("utf-8", "replace"))
err = stderr.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[-1500:])
c.close()
if __name__ == "__main__":
run(22, "root", "Xiao4669805", "vps")
run(4014, "eric", "4669805", "phys", sudo=True)