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

126 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
"""Live self-test: CDP End Stream + confirm on dylive running Neko."""
from __future__ import annotations
import json
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def ssh():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
return c
def run(c, cmd, t=120):
print(f"\n>>> {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-12000:] if len(out) > 12000 else out, flush=True)
return code, out
c = ssh()
# 1) find running nekos + studio tabs
code, out = run(
c,
r"""
set -euo pipefail
echo '=== running nekos ==='
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}'
echo '=== studio tabs on each running ==='
for name in $(docker ps --filter name=douyinweb-neko --format '{{.Names}}'); do
echo "-- $name"
docker exec "$name" python3 - <<'PY' || echo cdp_fail
import json,urllib.request
tabs=json.loads(urllib.request.urlopen('http://127.0.0.1:9222/json/list',timeout=5).read())
for t in tabs:
if t.get('type')!='page': continue
u=t.get('url') or ''
if 'studio.youtube.com' in u or 'google.com' in u:
print((t.get('title') or '')[:50], '|', u[:120])
PY
done
""",
t=60,
)
# 2) pick first running with livestreaming tab
code, out = run(
c,
r"""
set -euo pipefail
TARGET=""
for name in $(docker ps --filter name=douyinweb-neko --format '{{.Names}}'); do
hit=$(docker exec "$name" python3 - <<'PY'
import json,urllib.request
tabs=json.loads(urllib.request.urlopen('http://127.0.0.1:9222/json/list',timeout=5).read())
for t in tabs:
u=(t.get('url') or '').lower()
if 'studio.youtube.com' in u and 'livestreaming' in u:
print('YES')
break
else:
print('NO')
PY
)
if [ "$hit" = "YES" ]; then TARGET="$name"; break; fi
done
echo "TARGET=$TARGET"
if [ -z "$TARGET" ]; then echo 'NO_LIVE_TAB'; exit 2; fi
echo "$TARGET" > /tmp/neko_end_target.txt
# BEFORE scrape: does page have End dialog / End Stream / LIVE?
echo '=== BEFORE page signals ==='
docker exec -i "$TARGET" python3 - < /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | tail -1
echo '=== RUN cdp_end_stream ==='
docker exec -i "$TARGET" python3 - < /opt/douyinWEB/backend/neko/cdp_end_stream.py | tee /tmp/cdp_end_result.json | tail -5
echo '=== AFTER page signals (sleep 3) ==='
sleep 3
docker exec -i "$TARGET" python3 - < /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | tee /tmp/cdp_after.json | tail -1
python3 - <<'PY'
import json
from pathlib import Path
end=json.loads(Path('/tmp/cdp_end_result.json').read_text(encoding='utf-8').strip().splitlines()[-1])
after=json.loads(Path('/tmp/cdp_after.json').read_text(encoding='utf-8').strip().splitlines()[-1])
print('RESULT_OK', end.get('ok'))
print('end_click', end.get('end_click'))
print('confirm_click', end.get('confirm_click'))
print('pre_confirm', end.get('pre_confirm'))
print('after_live_status', after.get('live_status'), 'on_air', after.get('on_air'))
# success criteria: confirm clicked OR already ended / not session live
cc=end.get('confirm_click') or {}
pre=end.get('pre_confirm') or {}
ok = bool(end.get('ok')) or bool(cc.get('ok')) or bool(pre.get('ok'))
status=str(after.get('live_status') or '')
if status in {'ended','waiting'} or after.get('on_air') is False:
ok = True
print('SELFTEST', 'PASS' if ok else 'FAIL')
PY
""",
t=90,
)
c.close()
raise SystemExit(0 if "SELFTEST PASS" in out else 1)