# -*- coding: utf-8 -*- """Activate correct live Studio tab on neko-7 and ingest CDP.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") PASS = "4669805" GOOD = "jOxa90ifFCc" REMOTE = r'''#!/bin/bash set -uo pipefail NAME=douyinweb-neko-7 GOOD=jOxa90ifFCc cat >/tmp/_act7.py <<'PY' import json, urllib.request, time GOOD = "jOxa90ifFCc" tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read().decode()) live = [t for t in tabs if GOOD in (t.get("url") or "") and "livestreaming" in (t.get("url") or "")] other = [t for t in tabs if "studio.youtube.com" in (t.get("url") or "") and "livestreaming" in (t.get("url") or "") and GOOD not in (t.get("url") or "")] print("good_tabs", len(live), "other_live_tabs", [(t.get("url") or "")[:100] for t in other]) if live: tid = live[0].get("id") try: urllib.request.urlopen("http://127.0.0.1:9222/json/activate/%s" % tid, timeout=3).read() print("activated", tid) except Exception as e: print("activate_err", e) else: url = "https://studio.youtube.com/video/%s/livestreaming" % GOOD import urllib.parse req = urllib.request.Request("http://127.0.0.1:9222/json/new?" + urllib.parse.quote(url, safe=""), method="PUT") print("open", urllib.request.urlopen(req, timeout=10).read()[:120]) time.sleep(2) tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read().decode()) for t in tabs: u = t.get("url") or "" if "studio.youtube.com" in u: print("TAB", (t.get("title") or "")[:40], "|", u[:120]) PY cat /tmp/_act7.py | docker exec -i "$NAME" python3 - echo === scrape === cd /opt/douyinWEB/backend cat /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | docker exec -i "$NAME" python3 - echo === ingest via poll one === PYTHONPATH=/opt/douyinWEB/backend .venv/bin/python - <<'PY' from app import youtube_neko as yn sig = yn._cdp_studio_page_signals(7, force=True) print("sig", {k: sig.get(k) for k in ("ok","on_air","live_status","health","viewer_count","total_views","live_duration","url")}) if sig.get("ok"): print("ingest", yn._ingest_cdp_signals(7, sig)) snap = yn.studio_on_air_lookup(7, max_age_sec=120) print("snap", {k: snap.get(k) for k in ("on_air","live_status","health","viewer_count","total_views","live_duration","url","detect_source")}) PY echo DONE7 ''' def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( "107.173.30.245", 4014, "eric", PASS, timeout=45, banner_timeout=120, allow_agent=False, look_for_keys=False, ) sftp = c.open_sftp() with sftp.file("/tmp/_neko7_act.sh", "w") as f: f.write(REMOTE) sftp.close() _, o, e = c.exec_command( f"echo '{PASS}' | sudo -S -p '' bash /tmp/_neko7_act.sh", timeout=90, ) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() return 0 if __name__ == "__main__": raise SystemExit(main())