#!/usr/bin/env python3
"""advanced_search with the right param name (q) to bypass any user-timeline cache."""
import os, time
import requests
from datetime import datetime, timezone

ENV = os.path.expanduser("~/.hermes/.env")
API_KEY = [l.strip().split("=",1)[1] for l in open(ENV) if l.startswith("GETXAPI_KEY=")][0]
H = {"Authorization": f"Bearer {API_KEY}"}
def pdt(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

def run(path, params, tag):
    r = requests.get("https://api.getxapi.com"+path, headers=H, params=params, timeout=60)
    print(f"\n--- {tag} [{path}] -> {r.status_code}")
    if r.status_code != 200:
        print("   ", r.text[:200]); return []
    j = r.json()
    tw = j.get("tweets", j) if isinstance(j, dict) else j
    if isinstance(tw, dict): tw = list(tw.values())
    if not isinstance(tw, list):
        print("   keys:", list(j)[:15]); return []
    ds = [d for d in (pdt(t.get("createdAt","")) for t in tw) if d]
    print(f"    n={len(tw)} max={max(ds).strftime('%m-%d %H:%M') if ds else 'NA'} min={min(ds).strftime('%m-%d %H:%M') if ds else 'NA'}")
    for t in sorted(tw, key=lambda x: pdt(x.get("createdAt","")) or datetime.min.replace(tzinfo=timezone.utc))[-5:]:
        print(f"      {t.get('createdAt','')[:19]} {t.get('likeCount')}L/{t.get('viewCount')}V :: {(t.get('text') or '')[:110]}")
    return tw

run("/twitter/tweet/advanced_search", {"q":"from:higgsfield since:2026-11-07", "count":20}, "higgsfield since 11-07")
time.sleep(1)
run("/twitter/tweet/advanced_search", {"q":"from:runwayml since:2026-11-07"}, "runwayml since 11-07")
time.sleep(1)
run("/twitter/tweet/advanced_search", {"q":"from:Kling_ai since:2026-11-07"}, "Kling_ai control (known live)")
