"""Test Eastmoney rate limit — find safe interval between requests"""
import urllib.request, json, time

UA = 'Mozilla/5.0'
CODE = '00700'
url = f"https://push2.eastmoney.com/api/qt/stock/get?secid=116.{CODE}&fields=f43,f170&fltt=2"

def try_fetch():
    start = time.time()
    try:
        req = urllib.request.Request(url, headers={"User-Agent": UA})
        with urllib.request.urlopen(req, timeout=5) as r:
            resp = json.loads(r.read().decode("utf-8"))
            elapsed = time.time() - start
            return True, elapsed, resp.get('data', {}).get('f43', '?'), ''
    except Exception as e:
        elapsed = time.time() - start
        return False, elapsed, 0, str(e)[:50]

# Test 1: single request
ok, t, price, err = try_fetch()
print(f"Single: {'OK' if ok else 'FAIL'} in {t:.2f}s  price={price} {err}")

if ok:
    # If it works, test minimal interval
    for delay in [0.5, 1, 2, 3, 5]:
        time.sleep(delay)
        ok2, t2, p2, e2 = try_fetch()
        print(f"After {delay:.1f}s: {'OK' if ok2 else 'FAIL'} in {t2:.2f}s  price={p2} {e2}")
        if not ok2:
            print(f"  -> Rate limited! Need > {delay}s between requests")
else:
    # Currently blocked, wait and retry
    print("Currently blocked. Waiting 10s then retry...")
    time.sleep(10)
    ok, t, price, err = try_fetch()
    print(f"After 10s: {'OK' if ok else 'FAIL'} in {t:.2f}s  price={price} {err}")
    if ok:
        time.sleep(2)
        ok2, t2, p2, e2 = try_fetch()
        print(f"After +2s: {'OK' if ok2 else 'FAIL'} in {t2:.2f}s  price={p2} {e2}")
    else:
        for w in [20, 30, 60]:
            print(f"Waiting {w}s...")
            time.sleep(w)
            ok, t, price, err = try_fetch()
            print(f"After {w}s: {'OK' if ok else 'FAIL'} in {t:.2f}s  price={price} {err}")
            if ok:
                break
