douyinWEB/scripts/_fix_neko7_chrome_only.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

175 lines
5.3 KiB
Python

# -*- coding: utf-8 -*-
"""Soft-fix ONLY neko-7 Chrome: restart google-chrome, fix studio URL, reopen Live tab.
Does NOT touch neko 1-6. Does NOT recreate container.
"""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
PASS = "4669805"
# From prior ops / channel asian streetfood
STUDIO = "https://studio.youtube.com/video/i6szUMA25RM/livestreaming"
REMOTE = r'''#!/bin/bash
set -uo pipefail
NAME=douyinweb-neko-7
STUDIO='__STUDIO__'
echo === pre: proxy from container ===
docker exec "$NAME" python3 - <<'PY'
import socket, urllib.request
for host in ("host.docker.internal","172.17.0.1","172.18.0.1"):
try:
print(host, socket.getaddrinfo(host, 7890)[0][4])
except Exception as e:
print(host, "dns_fail", e)
s=socket.socket(); s.settimeout(2)
try:
print(host, "connect", s.connect_ex((host if host!="host.docker.internal" else socket.gethostbyname(host), 7890)))
except Exception as e:
print(host, "connect_err", e)
finally:
s.close()
PY
echo === soft restart google-chrome ONLY on neko-7 ===
docker exec "$NAME" supervisorctl restart google-chrome
sleep 12
docker exec "$NAME" supervisorctl status google-chrome || true
echo === wait CDP ===
for i in 1 2 3 4 5 6 7 8 9 10; do
if docker exec "$NAME" python3 -c 'import urllib.request; urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=2); print("ok")' 2>/dev/null | grep -q ok; then
echo CDP_UP try$i
break
fi
echo CDP_WAIT $i
sleep 2
done
echo === open studio tab ===
docker exec "$NAME" python3 - <<PY
import json, urllib.request, urllib.parse
url = "$STUDIO"
# Prefer activate existing livestreaming tab
try:
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=5).read().decode())
except Exception as e:
print("no_tabs", e); raise SystemExit(1)
live=[t for t in tabs if "livestreaming" in (t.get("url") or "") and "studio.youtube.com" in (t.get("url") or "")]
if live:
tid=live[0].get("id")
try:
urllib.request.urlopen(f"http://127.0.0.1:9222/json/activate/{tid}", timeout=3)
except Exception:
pass
print("activated", (live[0].get("url") or "")[:120])
else:
req=urllib.request.Request("http://127.0.0.1:9222/json/new?"+urllib.parse.quote(url, safe=":/?=&"), method="PUT")
try:
with urllib.request.urlopen(req, timeout=10) as r:
print("opened", r.read()[:120])
except Exception as e:
# fallback older endpoint
try:
with urllib.request.urlopen("http://127.0.0.1:9222/json/new?"+url, timeout=10) as r:
print("opened2", r.read()[:120])
except Exception as e2:
print("open_fail", e, e2)
import time; time.sleep(3)
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=5).read().decode())
print("tabs", len(tabs))
for t in tabs:
print("-", (t.get("title") or "")[:50], "|", (t.get("url") or "")[:130])
PY
echo === fix neko_studio_url in DB ===
cd /opt/douyinWEB/backend
PYTHONPATH=/opt/douyinWEB/backend .venv/bin/python - <<PY
from app import yt_assign_db, db
studio = "$STUDIO"
# find group
gid=None
for g in yt_assign_db.list_groups(include_urls=False):
if int(g.get("neko_instance_id") or 0)==7:
gid=str(g.get("id") or "")
print("before", g.get("name"), g.get("neko_studio_url"))
break
if not gid:
raise SystemExit("no group for neko 7")
# update via assign db helpers if any
try:
yt_assign_db.update_group(gid, neko_studio_url=studio)
print("updated via update_group")
except Exception as e:
print("update_group fail", e)
conn = db._connect()
try:
cols=[r[1] for r in conn.execute("PRAGMA table_info(yt_assign_groups)").fetchall()]
print("cols", cols)
if "neko_studio_url" in cols:
conn.execute("UPDATE yt_assign_groups SET neko_studio_url=? WHERE id=?", (studio, gid))
conn.commit()
print("updated via sql")
finally:
conn.close()
for g in yt_assign_db.list_groups(include_urls=False):
if int(g.get("neko_instance_id") or 0)==7:
print("after", g.get("neko_studio_url"))
PY
echo === scrape ===
python3 - <<'PY'
import subprocess
from pathlib import Path
script=Path("/opt/douyinWEB/backend/neko/cdp_studio_scrape.py").read_text(encoding="utf-8")
p=subprocess.run(["docker","exec","-i","douyinweb-neko-7","python3","-"],input=script,capture_output=True,text=True,timeout=35)
print(((p.stdout or "")+(p.stderr or ""))[-800:])
PY
echo FIX7_OK
'''.replace("__STUDIO__", STUDIO)
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(12):
try:
c.connect(
"107.173.30.245",
4014,
"eric",
PASS,
timeout=45,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
break
except Exception as ex:
print("ssh", i, type(ex).__name__, flush=True)
time.sleep(2)
else:
return 1
sftp = c.open_sftp()
with sftp.file("/tmp/_fix_neko7.sh", "w") as f:
f.write(REMOTE)
sftp.close()
_, o, e = c.exec_command(
f"echo '{PASS}' | sudo -S -p '' bash /tmp/_fix_neko7.sh",
timeout=180,
)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())