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

132 lines
3.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Copy remaining Douyin cookie / OAuth kv keys from physical → dylive (post channel sync)."""
from __future__ import annotations
import json
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
GUEST_SUDO = "4669805"
DB = "/opt/douyinWEB/backend/data/monitor.db"
TS = time.strftime("%Y%m%d_%H%M%S")
KEYS = [
"dy_cookie",
"dy_cookie_updated_at",
"shorts_oauth_profiles",
"shorts_oauth_client_id",
"shorts_oauth_client_secret",
"shorts_oauth_redirect_uri",
"shorts_oauth_brand_hints",
"yt_studio_cookies_by_profile",
]
EXPORT_PY = f"""
import json, sqlite3
c = sqlite3.connect("{DB}")
out = {{}}
for k in {KEYS!r}:
row = c.execute("SELECT value FROM kv_settings WHERE key=?", (k,)).fetchone()
if row:
out[k] = row[0]
print("has", k, "len", len(row[0] or ""))
else:
print("miss", k)
c.close()
open("/tmp/phys_kv_oauth.json","w",encoding="utf-8").write(json.dumps(out, ensure_ascii=False))
print("EXPORT_OK", len(out))
"""
IMPORT_PY = f"""
import json, sqlite3, shutil, time
from pathlib import Path
bak = Path("/root/dylive_bak_kv_oauth_{TS}")
bak.mkdir(parents=True, exist_ok=True)
shutil.copy2("{DB}", bak / "monitor.db")
data = json.loads(Path("/tmp/phys_kv_oauth.json").read_text(encoding="utf-8"))
c = sqlite3.connect("{DB}")
for k, v in data.items():
c.execute(
"INSERT INTO kv_settings(key, value) VALUES(?, ?) "
"ON CONFLICT(key) DO UPDATE SET value=excluded.value",
(k, v),
)
print("set", k, "len", len(v or ""))
c.commit()
c.close()
print("IMPORT_OK", "bak", str(bak))
"""
def connect(spec):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(*spec, timeout=60, banner_timeout=120, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, timeout=120):
print(f"\n$ {cmd[:200]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-5000:] if len(out) > 5000 else out, flush=True)
print(f"→ exit={code}", flush=True)
return code, out
def sftp_put(c, data: bytes, remote: str):
sftp = c.open_sftp()
try:
with sftp.open(remote, "wb") as f:
f.write(data)
finally:
sftp.close()
def sftp_get(c, remote: str) -> bytes:
sftp = c.open_sftp()
try:
with sftp.open(remote, "rb") as f:
return f.read()
finally:
sftp.close()
def main() -> int:
p = connect(PHYS)
try:
sftp_put(p, EXPORT_PY.encode("utf-8"), "/tmp/export_phys_kv.py")
code, out = run(
p,
f"echo '{GUEST_SUDO}' | sudo -S -p '' bash -lc 'python3 /tmp/export_phys_kv.py && chown eric:eric /tmp/phys_kv_oauth.json'",
)
if code != 0 or "EXPORT_OK" not in out:
return 1
blob = sftp_get(p, "/tmp/phys_kv_oauth.json")
finally:
p.close()
v = connect(VPS)
try:
sftp_put(v, blob, "/tmp/phys_kv_oauth.json")
sftp_put(v, IMPORT_PY.encode("utf-8"), "/tmp/import_phys_kv.py")
run(v, "systemctl stop douyinweb-api; sleep 1", 40)
code, out = run(v, "python3 /tmp/import_phys_kv.py", 60)
run(v, "systemctl start douyinweb-api; sleep 2; systemctl is-active douyinweb-api; "
"curl -sS -m 12 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health", 40)
return 0 if code == 0 and "IMPORT_OK" in out else 1
finally:
v.close()
if __name__ == "__main__":
raise SystemExit(main())