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

156 lines
4.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Compare stream key suffix vs Studio + watch one start cycle."""
from __future__ import annotations
import json
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
def ssh(host, port, user, pwd, cmd, t=90):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pwd, timeout=25, allow_agent=False, look_for_keys=False)
_, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
c.close()
return out
def main():
print("=== VPS group key / rtmp (suffix only) ===")
print(
ssh(
*VPS,
r"""
cd /opt/douyinWEB/backend && python3 - <<'PY'
import sqlite3, re
con=sqlite3.connect('data/monitor.db')
con.row_factory=sqlite3.Row
gid='3fb8cbf7-2180-45bc-99f0-836169f0bf97'
g=dict(con.execute('select * from yt_assign_groups where id=?',(gid,)).fetchone() or {})
# find key-ish columns
for k,v in g.items():
if v is None: continue
s=str(v)
kl=k.lower()
if any(x in kl for x in ('key','rtmp','ingest','stream','youtube')):
if 'key' in kl or 'rtmp' in kl or 'ingest' in kl:
# redact
if len(s)>8:
print(k, 'len', len(s), 'tail4', s[-4:], 'head20', s[:20].replace('\n',' '))
else:
print(k, s)
else:
print(k, s[:80])
print('category', g.get('category'), 'enabled', g.get('enabled'))
# settings time slot
for row in con.execute("select key,value from settings where key like 'youtube_relay%' or key like '%exclusive%' or key like '%time_slot%'"):
print('set', row['key'], row['value'])
PY
""",
)
)
print("=== PHYS CDP: studio key fragment + nodata ===")
print(
ssh(
*PHYS,
r"""
# use venv if present
PY=/opt/douyinWEB/backend/.venv/bin/python3
[ -x "$PY" ] || PY=python3
cd /opt/douyinWEB/backend
$PY - <<'PY'
import json, urllib.request
# try CDP scrape script if exists
import subprocess, os
os.chdir('/opt/douyinWEB')
# find ops-neko-1 cdp port
import docker
# fallback: docker inspect
out=subprocess.check_output(['docker','inspect','-f','{{range .NetworkSettings.Ports}}{{.}}{{end}}','douyinweb-ops-neko-1'], text=True, stderr=subprocess.DEVNULL)
print('ports_raw', out[:200])
PY
# simpler: run existing cdp scrape via docker exec
docker exec douyinweb-ops-neko-1 bash -lc 'ls /home/neko 2>/dev/null | head' 2>/dev/null | head -5
# host-side CDP
cd /opt/douyinWEB/backend
if [ -f .venv/bin/python ]; then .venv/bin/python neko/cdp_studio_scrape.py --slot 1 2>/dev/null | head -c 2500; else
python3 - <<'PY'
# locate chrome debug port for ops-neko-1
import subprocess, re, json, urllib.request
name='douyinweb-ops-neko-1'
# get host mapped 9222?
insp=subprocess.check_output(['docker','inspect',name], text=True)
j=json.loads(insp)[0]
ports=j.get('NetworkSettings',{}).get('Ports') or {}
print('Ports', ports)
# try common host ports from labels/env
env={e.split('=',1)[0]:e.split('=',1)[1] for e in j.get('Config',{}).get('Env',[]) if '=' in e}
print('CDP related', {k:v for k,v in env.items() if 'CDP' in k.upper() or 'DEBUG' in k.upper() or '922' in v})
# IP
ip=j['NetworkSettings']['IPAddress'] or list((j['NetworkSettings'].get('Networks') or {}).values())[0].get('IPAddress')
print('ip', ip)
for port in (9222, 9223, 35000, 35001):
try:
with urllib.request.urlopen(f'http://{ip}:{port}/json/list', timeout=2) as r:
tabs=json.loads(r.read().decode())
print('cdp', port, 'tabs', len(tabs))
for t in tabs:
u=t.get('url') or ''
if 'studio.youtube' in u or 'livestreaming' in u:
print(' TAB', u[:100], 'id', t.get('id'))
break
except Exception as ex:
print('fail', port, type(ex).__name__)
PY
fi
""",
60,
)
)
print("=== VPS: poll relay 45s ===")
print(
ssh(
*VPS,
r"""
cd /opt/douyinWEB/backend
python3 - <<'PY'
import sqlite3, json, time, os
gid='3fb8cbf7-2180-45bc-99f0-836169f0bf97'
for i in range(9):
con=sqlite3.connect('data/monitor.db')
con.row_factory=sqlite3.Row
r=dict(con.execute('select status,error,ffmpeg_pid,push_seconds,recent_log,updated_at from yt_relay_states where group_id=?',(gid,)).fetchone() or {})
pid=int(r.get('ffmpeg_pid') or 0)
alive=bool(pid and os.path.exists(f'/proc/{pid}'))
log=r.get('recent_log') or ''
try:
lines=json.loads(log) if isinstance(log,str) and log.startswith('[') else str(log).splitlines()
except Exception:
lines=[]
tail=lines[-3:] if lines else []
print(f"t+{i*5:02d}s", r.get('status'), 'pid', pid, 'alive', alive, 'push', r.get('push_seconds'), 'err', (r.get('error') or '')[:90])
for ln in tail:
print(' ', (ln if isinstance(ln,str) else str(ln))[:160])
time.sleep(5)
PY
""",
70,
)
)
if __name__ == "__main__":
main()