douyinWEB/scripts/apply_sh2_antirisk.py
eric 76afafa6c9 Initial commit: Douyin live monitor with sh2-aligned probe.
Next.js + FastAPI + PocketBase stack for room/session monitoring, cookie sync from sh2, and anti-risk polling defaults.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 09:03:00 -05:00

68 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
"""Apply sh2-aligned anti-risk settings and restagger room checks."""
from __future__ import annotations
import random
import sys
import time
from pathlib import Path
BACKEND = Path(r"C:\Users\admin\Desktop\douyinWEB\backend")
sys.path.insert(0, str(BACKEND))
from app import cookie_loader, db # noqa: E402
def main() -> None:
db.init_db()
sync = cookie_loader.force_sync_cookie_from_sh2()
print("cookie", sync, flush=True)
# Match sh2 config.ini: 循环时间=300, 线程数偏保守
db.update_settings_map(
{
"monitor_enabled": True,
"max_concurrency": 1,
"min_request_gap_sec": 3.0,
"request_jitter_sec": 2.0,
"offline_interval_sec": 300.0,
"live_interval_sec": 120.0,
"priority_interval_sec": 90.0,
"error_backoff_base_sec": 60.0,
"error_backoff_max_sec": 600.0,
"global_pause_on_risk_sec": 90.0,
"capture_cover_on_start": False,
}
)
rooms = db.list_rooms(limit=2000).get("rooms") or []
now = time.time()
# Spread first wave over ~ (n * ~4s) with 60s headroom — no burst after cool-down clear
with db._lock: # noqa: SLF001
conn = db._connect() # noqa: SLF001
try:
for i, r in enumerate(rooms):
# live rooms keep a shorter but still gentle cadence
if r.get("last_status") == "live":
delay = 30.0 + i * 2.0 + random.uniform(0, 3)
else:
delay = 90.0 + i * 4.0 + random.uniform(0, 5)
conn.execute(
"UPDATE rooms SET next_check_at = ?, last_error = '', updated_at = ? WHERE id = ?",
(now + delay, now, r["id"]),
)
conn.commit()
finally:
conn.close()
print(f"restaggered={len(rooms)}", flush=True)
print("settings", {k: db.get_settings_map().get(k) for k in [
"max_concurrency", "offline_interval_sec", "live_interval_sec",
"min_request_gap_sec", "global_pause_on_risk_sec", "capture_cover_on_start",
"monitor_enabled",
]}, flush=True)
if __name__ == "__main__":
main()