#!/usr/bin/env python3
"""Direct re-query of suspiciously stale key handles -- the 2026-10-19 stale-cache test."""
import os, sys, time, json
import requests
from datetime import datetime, timezone

ENV = os.path.expanduser("~/.hermes/.env")
API_KEY = None
for line in open(ENV):
    if line.startswith("GETXAPI_KEY="):
        API_KEY = line.strip().split("=", 1)[1]
        break

def pdt(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

now = datetime.now(timezone.utc)
print("now:", now.strftime("%Y-%m-%d %H:%M UTC"))
d = json.load(open("/home/kiro/trend-engine/trends/x/raw/scan_results_2026-11-09_FULL.json"))
raw = d["raw"]

SUS = ["higgsfield", "runwayml", "perplexity_ai", "AIWarper", "pika_labs", "Kling_ai",
       "CharaspowerAI", "openart_ai", "Flovaai", "ItsMaryamAi", "uns__nstudio", "heyglif",
       "AI__TSUBAKI", "higgsfield_ai"]
url = "https://api.getxapi.com/twitter/user/tweets"
for h in SUS:
    cached = raw.get(h) or []
    cmax = max((pdt(t["createdAt"]) for t in cached if t.get("createdAt")), default=None)
    cached_ids = {t.get("id") for t in cached}
    try:
        r = requests.get(url, headers={"Authorization": f"Bearer {API_KEY}"},
                         params={"userName": h, "count": 5}, timeout=60)
        if r.status_code != 200:
            print(f"@{h:16s} DIRECT HTTP {r.status_code}")
            time.sleep(1); continue
        tw = r.json().get("tweets", [])
        ds = [pdt(t["createdAt"]) for t in tw if t.get("createdAt")]
        dmax = max(ds) if ds else None
        newest_id = tw[0].get("id") if tw else None
        # find newest by date not by list order
        if tw and ds:
            newest_tw = max(tw, key=lambda t: pdt(t["createdAt"]))
            newest_id = newest_tw.get("id")
        delta = ((dmax - cmax).total_seconds()/3600) if (dmax and cmax) else None
        new_ids = [t.get("id") for t in tw if t.get("id") not in cached_ids]
        flag = "STALE-CACHE" if (delta and delta > 2) else "ok"
        print(f"@{h:16s} cached_max={cmax.strftime('%m-%d %H:%M') if cmax else 'NA'} "
              f"direct_max={dmax.strftime('%m-%d %H:%M') if dmax else 'NA'} "
              f"delta={delta if delta is None else round(delta,1)}h n_direct={len(tw)} "
              f"new_ids={len(new_ids)} -> {flag}")
        if new_ids:
            for t in sorted([t for t in tw if t.get("id") in new_ids], key=lambda t: pdt(t["createdAt"]))[:6]:
                print(f"    NEW {t['createdAt']}: {t['likeCount']}L/{t['viewCount']}V :: {(t.get('text') or '')[:140]}")
    except Exception as e:
        print(f"@{h:16s} ERR {e}")
    time.sleep(1.0)
