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

76 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import hashlib
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
LOCAL = Path(__file__).resolve().parents[1] / "backend" / "app" / "youtube_live.py"
LOCAL_SHA = hashlib.sha256(LOCAL.read_bytes()).hexdigest()[:16]
print("local", LOCAL_SHA, "has_offline_hit", b"public_offline_hit" in LOCAL.read_bytes())
def check(host, port, user, pw, sudo=False):
print(f"\n== {user}:{port} ==")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
# force write via tmp + install
tmp = "/tmp/youtube_live_force.py"
data = LOCAL.read_bytes()
with sftp.file(tmp, "wb") as f:
f.write(data)
sftp.close()
dst = "/opt/douyinWEB/backend/app/youtube_live.py"
def run(cmd, t=60):
_, o, e = c.exec_command(cmd, timeout=t)
o.channel.settimeout(t)
try:
return (o.read() + e.read()).decode("utf-8", "replace")
except Exception as ex:
return str(ex)
if sudo:
print(
run(
f"echo '{pw}' | sudo -S -p '' bash -lc \"cp -a {dst} {dst}.bak_oa; cat {tmp} > {dst}; sha256sum {dst}; grep -c public_offline_hit {dst}; systemctl restart douyinweb-api\"",
50,
)
)
else:
print(run(f"cp -a {dst} {dst}.bak_oa; cat {tmp} > {dst}; sha256sum {dst}; grep -c public_offline_hit {dst}; systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api", 50))
import time
for i in range(10):
time.sleep(2)
h = run("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x").strip()
print(" health", h)
if h == "200":
break
# introspect running code
print(
run(
"""
/opt/douyinWEB/backend/.venv/bin/python - <<'PY'
import inspect
from app import youtube_live
src = inspect.getsource(youtube_live._build_live_map_unlocked)
print('module_file', youtube_live.__file__)
print('has_public_offline_hit', 'public_offline_hit' in src)
print('has_oauth_assign', 'Assign cards that bind' in src or 'pk_to_assign' in src)
PY
"""
)
)
c.close()
check("107.173.30.245", 22, "root", "Xiao4669805", False)
check("107.173.30.245", 4014, "eric", "4669805", True)