"""服务器上探测币安期权盘口（只读）。用法: python scripts/probe_binance_options.py"""

from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "backend"))

import httpx  # noqa: E402


def main() -> None:
    proxy = None
    env = ROOT / ".env"
    if env.exists():
        for line in env.read_text(encoding="utf-8").splitlines():
            if line.startswith("BINANCE_HTTP_PROXY=") or line.startswith("OKX_HTTP_PROXY="):
                v = line.split("=", 1)[1].strip().strip('"').strip("'")
                if v:
                    proxy = v
    print("proxy=", proxy)
    kw: dict = {"timeout": 30, "trust_env": False}
    if proxy:
        kw["proxy"] = proxy
    with httpx.Client(**kw) as cli:
        info = cli.get("https://eapi.binance.com/eapi/v1/exchangeInfo")
        print("exchangeInfo", info.status_code)
        opts = (info.json() or {}).get("optionSymbols") or []
        near = [
            x
            for x in opts
            if str(x.get("symbol", "")).startswith("ETH-260726-1850")
            or str(x.get("symbol", "")).startswith("ETH-250726-1850")
        ]
        print("near1850", [x.get("symbol") for x in near[:6]])
        if not near:
            near = [x for x in opts if str(x.get("symbol", "")).startswith("ETH-") ][:4]
            print("fallback sample", [x.get("symbol") for x in near])
        for row in near[:4]:
            sym = row["symbol"]
            for limit in (5, 10, 20):
                d = cli.get(
                    "https://eapi.binance.com/eapi/v1/depth",
                    params={"symbol": sym, "limit": limit},
                )
                print(f"depth limit={limit}", sym, d.status_code, d.text[:180])
            t = cli.get("https://eapi.binance.com/eapi/v1/ticker", params={"symbol": sym})
            print("ticker", sym, t.status_code, t.text[:240])
            print("---")


if __name__ == "__main__":
    main()
