# -*- coding: utf-8 -*- """Verify active session timers vs real Douyin live status.""" import asyncio import json import sys import time from pathlib import Path import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" def ssh_json(path: str): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) _, o, _ = c.exec_command( f"curl -s --max-time 20 http://127.0.0.1:8014{path}", timeout=40, get_pty=True, ) raw = o.read().decode("utf-8", errors="replace").strip() c.close() # strip ANSI / pty junk if "{" in raw: raw = raw[raw.index("{") :] if raw.endswith("%"): raw = raw[:-1] return json.loads(raw) def fmt_dur(sec: float) -> str: sec = int(max(0, sec)) h, r = divmod(sec, 3600) m, s = divmod(r, 60) return f"{h}:{m:02d}:{s:02d}" async def main(): active = ssh_json("/api/sessions/active") sessions = active.get("sessions") or [] now = time.time() print(f"server_active_total={active.get('total')} local_now={now}", flush=True) # focus names user asked focus = ["杭州光头煎饺", "阿超盖浇饭", "小易"] for s in sessions: name = s.get("anchor_name") or "" started = float(s.get("started_at") or 0) elapsed = now - started if started else 0 print( f"ACTIVE {name!r} room={s.get('room_id')} started={started} " f"elapsed={fmt_dur(elapsed)} ({elapsed:.0f}s) cover={bool(s.get('cover_url'))}", flush=True, ) # probe each active via API check one room c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) script = r""" import json, time, urllib.request now=time.time() d=json.load(urllib.request.urlopen('http://127.0.0.1:8014/api/sessions/active', timeout=30)) rooms=json.load(urllib.request.urlopen('http://127.0.0.1:8014/api/rooms?limit=2000&status=live', timeout=30)) print('active', d.get('total'), 'live_rooms_api', rooms.get('total')) # map room_pk by_pk={} allr=json.load(urllib.request.urlopen('http://127.0.0.1:8014/api/rooms?limit=2000', timeout=30)) for r in allr.get('rooms') or []: by_pk[r['id']]=r for s in d.get('sessions') or []: r=by_pk.get(s.get('room_pk') or '') or {} started=float(s.get('started_at') or 0) el=now-started print('---') print('anchor', s.get('anchor_name')) print('session', s.get('id')) print('room_id', s.get('room_id'), 'room_status', r.get('last_status'), 'room_last_check', r.get('last_checked_at')) print('started_at', started, 'elapsed_h', round(el/3600,2), 'elapsed', int(el)) print('url', s.get('source_url') or r.get('url')) """ # write and run probe with check_room for focus names cmd = f""" cd /opt/douyinWEB/backend . .venv/bin/activate python - <<'PY' import asyncio, json, time, urllib.request from app.douyin import check_room from app import db db.init_db() cfg=db.get_settings_map() cookie=str(cfg.get('dy_cookie') or '') now=time.time() active=db.list_active_sessions() print('db_active', len(active)) for s in active: name=s.get('anchor_name') or '' started=float(s.get('started_at') or 0) el=now-started room=db.get_room(str(s.get('room_pk') or '')) or {{}} print('===', name) print(' elapsed', f"{{int(el//3600)}}:{{int(el%3600//60):02d}}:{{int(el%60):02d}}", f"({{el/3600:.2f}}h)") print(' started', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(started)), 'unix', started) print(' room_last_status', room.get('last_status'), 'last_checked', room.get('last_checked_at')) print(' room_id', s.get('room_id'), room.get('url')) try: r=asyncio.run(check_room(room.get('url') or s.get('source_url') or '', cookie=cookie)) print(' probe_ok', r.get('ok'), 'is_live', r.get('is_live'), 'status', r.get('status'), 'anchor', r.get('anchor_name'), 'err', r.get('error')) except Exception as e: print(' probe_fail', type(e).__name__, e) PY """ print("\n--- remote probe ---", flush=True) _, o, _ = c.exec_command(cmd, timeout=300, get_pty=True) print(o.read().decode("utf-8", errors="replace")[-8000:], flush=True) c.close() if __name__ == "__main__": asyncio.run(main())