# -*- coding: utf-8 -*- """Deploy Shorts offline rebind/skip + stop retrying dead streams.""" from __future__ import annotations import json import sys import http.cookiejar import urllib.request from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB") FILES = [ "backend/app/db.py", "backend/app/shorts_pipeline.py", ] HEAL = r''' import time from app import db from app.shorts_pipeline import skip_or_rebind_offline_pending from app.timeutil import push_date_from_ts day = push_date_from_ts(time.time()) now = time.time() conn = db._connect() try: cur = conn.execute( """ UPDATE shorts_jobs SET status='skipped', error=CASE WHEN IFNULL(error,'')='' THEN '直播间已停播,已释放当日名额' WHEN error LIKE '%已释放%' THEN error ELSE error || '(已改为释放名额)' END, updated_at=?, finished_at=CASE WHEN IFNULL(finished_at,0)=0 THEN ? ELSE finished_at END WHERE push_date=? AND status='failed' AND ( error LIKE '%无法获取直播流%' OR error LIKE '%已停播%' OR lower(error) LIKE '%probe 404%' OR lower(error) LIKE '%server returned 404%' ) """, (now, now, day), ) print("healed_failed_to_skipped", cur.rowcount, "day", day) conn.commit() finally: conn.close() r = skip_or_rebind_offline_pending(limit=80) print("offline_pending", r) print("stats", db.shorts_stats(day).get("by_status")) ''' REMOTE = r"""#!/bin/bash set -euo pipefail ROOT=/opt/douyinWEB i=0 while [ -f "/tmp/_dy_off_${i}.path" ]; do rel=$(cat "/tmp/_dy_off_${i}.path") dst="$ROOT/$rel" python3 -c "from pathlib import Path; p=Path('/tmp/_dy_off_${i}.bin'); d=Path('$dst'); d.write_bytes(p.read_bytes().replace(b'\r\n',b'\n').replace(b'\r',b'\n')); print('ok', d)" i=$((i+1)) done systemctl restart douyinweb-api.service sleep 5 systemctl is-active douyinweb-api.service cd "$ROOT/backend" /opt/douyinWEB/backend/.venv/bin/python /tmp/_dy_off_heal.py grep -nE 'skip_or_rebind_offline|_try_rebind_shorts|无法解析直播流' \ "$ROOT/backend/app/shorts_pipeline.py" | head -15 curl -sS -m 5 http://127.0.0.1:8014/api/ping; echo """ def deploy(label, host, port, user, pw, *, sudo): print(f"\n===== {label} =====") 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) sftp = c.open_sftp() for i, rel in enumerate(FILES): data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") with sftp.file(f"/tmp/_dy_off_{i}.bin", "wb") as f: f.write(data) with sftp.file(f"/tmp/_dy_off_{i}.path", "w") as f: f.write(rel.replace("\\", "/")) for j in range(len(FILES), 8): try: sftp.remove(f"/tmp/_dy_off_{j}.path") except Exception: pass with sftp.file("/tmp/_dy_off_heal.py", "w") as f: f.write(HEAL.replace("\r\n", "\n")) with sftp.file("/tmp/_dy_off_remote.sh", "w") as f: f.write(REMOTE.replace("\r\n", "\n")) sftp.close() cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_off_remote.sh" if sudo else "bash /tmp/_dy_off_remote.sh" _, o, e = c.exec_command(cmd, timeout=180) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() def check(base): cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) data = json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode() op.open( urllib.request.Request( base + "/api/auth/login", data=data, method="POST", headers={"Content-Type": "application/json"}, ), timeout=30, ) with op.open(urllib.request.Request(base + "/api/shorts/status"), timeout=60) as r: st = json.loads(r.read().decode()) print(base, "stats", (st.get("stats") or {}).get("by_status"), "live", st.get("douyin_live_count")) deploy("vps", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False) deploy("phys", "107.173.30.245", 4014, "eric", "4669805", sudo=True) print("\n===== public =====") for b in ("https://dylive.nomadro.com", "https://dy.nomadro.com"): try: check(b) except Exception as exc: print(b, "ERR", exc) print("done")