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

154 lines
5.4 KiB
Python

#!/usr/bin/env python3
"""Resume merge of /tmp/dylive_ops_export.db into dy.nomadro.live."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
TABLES = [
"rooms",
"blacklist",
"channel_groups",
"shorts_channels",
"yt_long_channels",
"yt_assign_groups",
"yt_assign_pool_urls",
"yt_assign_featured_urls",
"yt_assign_slot_urls",
]
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"108.174.50.68",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
cmd = r"""
set -euo pipefail
cd /opt/douyinWEB/backend
test -f /tmp/dylive_ops_export.db
./.venv/bin/python - <<'PY'
import json, sqlite3
from pathlib import Path
from app import db
SRC = Path("/tmp/dylive_ops_export.db")
DST = Path("/opt/douyinWEB/backend/data/monitor.db")
TABLES = __TABLES__
# touch schema by listing
_ = db.list_rooms()
_ = db.list_shorts_channels()
from app import yt_assign_db
_ = yt_assign_db.list_groups(include_urls=False)
src = sqlite3.connect(str(SRC))
dst = sqlite3.connect(str(DST))
src.row_factory = sqlite3.Row
def cols(con, table):
return [r[1] for r in con.execute(f"PRAGMA table_info([{table}])").fetchall()]
def exists(con, table):
return bool(con.execute(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", (table,)
).fetchone())
report = {}
for t in TABLES:
if not exists(src, t):
report[t] = {"skipped": "missing_src"}
continue
if not exists(dst, t):
schema = src.execute(
"SELECT sql FROM sqlite_master WHERE type='table' AND name=?", (t,)
).fetchone()
if schema and schema[0]:
dst.execute(schema[0]); dst.commit()
else:
report[t] = {"skipped": "no_schema"}
continue
sc, dc = cols(src, t), cols(dst, t)
common = [c for c in sc if c in dc]
rows = src.execute(f"SELECT * FROM [{t}]").fetchall()
before = dst.execute(f"SELECT COUNT(*) FROM [{t}]").fetchone()[0]
dst.execute(f"DELETE FROM [{t}]")
inserted = 0
for r in rows:
d = dict(r)
if t == "yt_assign_groups" and "neko_instance_id" in d:
try:
nid = int(d.get("neko_instance_id") or 0)
except Exception:
nid = 0
if nid > 8:
d["neko_instance_id"] = ((nid - 1) % 8) + 1
vals = [d.get(c) for c in common]
placeholders = ",".join(["?"] * len(common))
colnames = ",".join(f"[{c}]" for c in common)
dst.execute(
f"INSERT OR REPLACE INTO [{t}] ({colnames}) VALUES ({placeholders})",
vals,
)
inserted += 1
dst.commit()
after = dst.execute(f"SELECT COUNT(*) FROM [{t}]").fetchone()[0]
report[t] = {"src": len(rows), "before": before, "after": after, "inserted": inserted}
dst.close(); src.close()
print(json.dumps(report, ensure_ascii=False, indent=2))
rooms = db.list_rooms()
print("rooms_total", rooms.get("total") if isinstance(rooms, dict) else len(rooms))
chs = db.list_shorts_channels()
print("shorts_channels", len(chs), "with_rt", sum(1 for x in chs if str(x.get("refresh_token") or "").strip()))
groups = yt_assign_db.list_groups(include_urls=False)
print("assign_groups", len(groups))
print("neko_ids", sorted({int(g.get("neko_instance_id") or 0) for g in groups}))
PY
systemctl restart douyinweb-api douyinweb-web
sleep 5
systemctl is-active douyinweb-api douyinweb-web
# firewall
set +e
if command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -qi active; then
ufw allow 52000:52499/udp || true
ufw allow 52000:52499/tcp || true
ufw allow 52300:52320/udp || true
ufw allow 52300:52320/tcp || true
else
iptables -C INPUT -p udp --dport 52000:52499 -j ACCEPT 2>/dev/null || iptables -I INPUT -p udp --dport 52000:52499 -j ACCEPT
iptables -C INPUT -p tcp --dport 52000:52499 -j ACCEPT 2>/dev/null || iptables -I INPUT -p tcp --dport 52000:52499 -j ACCEPT
iptables -C INPUT -p udp --dport 52300:52320 -j ACCEPT 2>/dev/null || iptables -I INPUT -p udp --dport 52300:52320 -j ACCEPT
echo iptables_ok
fi
curl -sS -c /tmp/dyj -b /tmp/dyj -o /tmp/l.json -w 'login:%{http_code}\n' -X POST https://dy.nomadro.live/api/auth/login \
-H 'Content-Type: application/json' -d '{"username":"hackrobot","password":"Xiao4669805@"}'
cat /tmp/l.json; echo
curl -sS -b /tmp/dyj 'https://dy.nomadro.live/api/rooms?limit=3' | ./.venv/bin/python -c 'import sys,json;d=json.load(sys.stdin);print("rooms",d.get("total"), "n",len(d.get("rooms") or []))'
curl -sS -b /tmp/dyj https://dy.nomadro.live/api/shorts/channels | ./.venv/bin/python -c 'import sys,json;d=json.load(sys.stdin);rows=d if isinstance(d,list) else d.get("channels") or d.get("items") or [];print("channels",len(rows));print([r.get("name") for r in rows[:6]])'
curl -sS -b /tmp/dyj https://dy.nomadro.live/api/youtube/assign/groups | ./.venv/bin/python -c 'import sys,json;d=json.load(sys.stdin);rows=d if isinstance(d,list) else d.get("groups") or [];print("assign",len(rows));print([(r.get("name"), r.get("neko_instance_id")) for r in rows[:8]])'
""".replace("__TABLES__", repr(TABLES))
_i, o, e = c.exec_command(cmd, timeout=300)
print((o.read() + e.read()).decode("utf-8", "replace"))
print("exit", o.channel.recv_exit_status())
c.close()
print("DONE", time.strftime("%H:%M:%S"))