# -*- coding: utf-8 -*- """Finish PB start, enable dy shorts/relay, verify live+shorts both sites.""" from __future__ import annotations import json import sys import time import http.cookiejar import urllib.request from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ENABLE_PY = r''' import sqlite3 con=sqlite3.connect("/opt/douyinWEB/backend/data/monitor.db") con.execute("create table if not exists kv_settings (key text primary key, value text not null)") for k,v in [("shorts_enabled","1"),("youtube_relay_enabled","1")]: con.execute( "insert into kv_settings(key,value) values(?,?) on conflict(key) do update set value=excluded.value", (k,v), ) con.commit() rows=con.execute( "select key,value from kv_settings where key in ('shorts_enabled','youtube_relay_enabled')" ).fetchall() print("kv", rows) print("shorts_ch", con.execute("select count(1) from shorts_channels where enabled=1").fetchone()[0]) print("assign_g", con.execute("select count(1) from yt_assign_groups where enabled=1").fetchone()[0]) ''' PB_FIX = r''' set -e ls -la /opt/douyinWEB/pb/pocketbase file /opt/douyinWEB/pb/pocketbase chmod +x /opt/douyinWEB/pb/pocketbase # stop crash loop, try foreground once for error systemctl stop douyinweb-pb || true sleep 1 # migrate old windows leftover if any ls -la /opt/douyinWEB/pb/ | head -20 # run briefly to see boot error timeout 4 /opt/douyinWEB/pb/pocketbase serve --http=127.0.0.1:8092 --dir=/opt/douyinWEB/pb/pb_data 2>&1 | tail -40 || true # fix unit WorkingDirectory / args if needed cat >/etc/systemd/system/douyinweb-pb.service <<'EOF' [Unit] Description=DouyinWEB PocketBase After=network.target [Service] Type=simple WorkingDirectory=/opt/douyinWEB/pb ExecStart=/opt/douyinWEB/pb/pocketbase serve --http=127.0.0.1:8092 --dir=/opt/douyinWEB/pb/pb_data Restart=always RestartSec=3 LimitNOFILE=65535 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl reset-failed douyinweb-pb systemctl restart douyinweb-pb sleep 2 systemctl is-active douyinweb-pb systemctl status douyinweb-pb --no-pager -l | head -25 curl -sS -m 3 http://127.0.0.1:8092/api/health || true echo ''' def ssh(host, port, user, pw, sudo=False): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False) def run(cmd, t=180): if sudo: cmd = f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}" _, o, e = c.exec_command(cmd, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") def put(local_text: str, remote: str): sftp = c.open_sftp() with sftp.file(remote, "w") as f: f.write(local_text.replace("\r\n", "\n")) sftp.close() return c, run, put def call(base, path, opener=None, method="GET", body=None, timeout=60): data = None if body is None else json.dumps(body).encode() req = urllib.request.Request( f"{base}{path}", data=data, method=method, headers={"Content-Type": "application/json"} ) op = opener or urllib.request.build_opener() with op.open(req, timeout=timeout) as r: return r.status, json.loads(r.read().decode()) def login(base): cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) call(base, "/api/auth/login", op, "POST", {"username": "hackrobot", "password": "Xiao4669805@"}) return op # ---- VPS ---- print("===== VPS PB + verify =====", flush=True) v, vr, vp = ssh("107.173.30.245", 22, "root", "Xiao4669805") vp(PB_FIX, "/tmp/_pb_fix.sh") print(vr("bash /tmp/_pb_fix.sh", 60)) # kick shorts sample if API supports print(vr( "curl -sS -m 5 http://127.0.0.1:8020/health; echo; " "curl -sS -m 5 http://127.0.0.1:8092/api/health; echo; " "free -h | head -2" )) v.close() # ---- PHYS ---- print("===== PHYS PB + enable shorts/relay =====", flush=True) p, pr, pp = ssh("107.173.30.245", 4014, "eric", "4669805", sudo=True) pp(PB_FIX, "/tmp/_pb_fix.sh") pp(ENABLE_PY, "/tmp/_enable_shorts.py") print(pr("bash /tmp/_pb_fix.sh", 60)) print(pr("/opt/douyinWEB/backend/.venv/bin/python /tmp/_enable_shorts.py", 30)) # reload settings into running API without full restart if possible — restart api to pick kv print(pr( "systemctl restart douyinweb-api; sleep 2; " "systemctl is-active douyinweb-api douyinweb-yt douyinweb-pb douyinweb-s3" )) for i in range(24): time.sleep(5) code = pr("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo").strip() print(f"phys[{i}] {code}", flush=True) if code.endswith("200"): break # force enable via API settings if endpoint exists p.close() print("===== API enable + verify =====", flush=True) for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]: print(f"\n--- {base} ---") op = login(base) st, h = call(base, "/api/health", op) print("health youtube", h.get("youtube"), "pb", h.get("pocketbase"), "s3", h.get("s3"), "ytmon", h.get("youtube_monitor")) # try settings update endpoints for path, body in [ ("/api/settings", {"shorts_enabled": True, "youtube_relay_enabled": True}), ("/api/shorts/start", {}), ("/api/youtube/relay/start", {}), ("/api/youtube-monitor/start", {}), ]: try: st, body_out = call(base, path, op, "POST", body if path == "/api/settings" else None, timeout=30) print(path, st, str(body_out)[:160]) except Exception as ex: # try PUT try: if path == "/api/settings": st, body_out = call(base, path, op, "PUT", body, timeout=30) print(path, "PUT", st, str(body_out)[:160]) else: print(path, "ERR", ex) except Exception as ex2: print(path, "ERR", ex2) shorts = call(base, "/api/shorts/status", op)[1] print("shorts", {k: shorts.get(k) for k in ("enabled", "running", "quota_gap", "processed", "sampled", "last_error", "busy")}) relay = call(base, "/api/youtube/relay/status", op)[1] print("relay", {k: relay.get(k) for k in ("enabled", "running", "pushing_count", "enabled_groups", "last_error")}) ytmon = call(base, "/api/youtube-monitor/status", op)[1] print("yt_svc", ytmon.get("youtube_svc"), "running", ytmon.get("running")) try: live = call(base, "/api/youtube/live-map?force=1", op, timeout=120)[1] print("live_map", live.get("live_count"), live.get("source"), "entries", len(live.get("entries") or {})) except Exception as ex: print("live_map ERR", ex)