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

104 lines
3.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Compare YouTube live counts on dy (physical) vs dylive (VPS)."""
from __future__ import annotations
import json
import sys
import urllib.request
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
SITES = [
("dylive/VPS", "https://dylive.nomadro.com"),
("dy/physical", "https://dy.nomadro.com"),
]
PATHS = [
"/api/health",
"/api/status",
"/api/youtube/status",
"/api/youtube/live",
"/api/youtube/assign/status",
"/api/youtube/relay/status",
]
def get(url: str, timeout: float = 12.0):
try:
req = urllib.request.Request(url, headers={"User-Agent": "dy-compare/1"})
with urllib.request.urlopen(req, timeout=timeout) as r:
body = r.read().decode("utf-8", "replace")
return r.status, body
except Exception as ex:
return 0, f"{type(ex).__name__}: {ex}"
def slim(path: str, body: str):
try:
d = json.loads(body)
except Exception:
return body[:200]
if path.endswith("/health"):
return {
k: d.get(k)
for k in ("ok", "host", "role", "host_profile", "version")
if k in d or True
}
if "youtube" in path and "relay" in path:
return {
"ok": d.get("ok"),
"pushing_count": d.get("pushing_count"),
"enabled": d.get("enabled") or d.get("pipeline_enabled"),
"groups_n": len(d.get("groups") or d.get("relay_groups") or []),
}
if "assign" in path:
return {k: d.get(k) for k in list(d)[:12]}
# generic pick live-ish keys
out = {}
for k in (
"live_count",
"yt_live_count",
"youtube_live_count",
"active_sessions",
"ok",
"groups",
"overview",
"monitor",
"youtube",
):
if k in d:
out[k] = d[k]
if "overview" in d and isinstance(d["overview"], dict):
out["overview"] = {
k: d["overview"].get(k)
for k in ("sessions", "active_sessions", "live", "live_count")
if k in d["overview"]
}
if not out:
# shallow scan
def walk(obj, prefix=""):
if isinstance(obj, dict):
for kk, vv in obj.items():
lk = str(kk).lower()
if any(x in lk for x in ("live", "active", "session", "push")):
if not isinstance(vv, (dict, list)) or (isinstance(vv, list) and len(vv) < 5):
out[f"{prefix}{kk}"] = vv
if isinstance(vv, dict) and len(prefix) < 20:
walk(vv, prefix + kk + ".")
walk(d)
return out or {k: d.get(k) for k in list(d)[:8]}
def main():
for name, base in SITES:
print(f"\n======== {name} {base} ========")
for p in PATHS:
code, body = get(base + p)
print(f"-- {p} [{code}]")
print(json.dumps(slim(p, body), ensure_ascii=False, indent=2)[:1200])
if __name__ == "__main__":
main()