#!/usr/bin/env python3
import json, os, time
import requests

key = None
for line in open(os.path.expanduser("~/.hermes/.env")):
    if line.startswith("GETXAPI_KEY="):
        key = line.split("=", 1)[1].strip()
        break
H = {"Authorization": f"Bearer {key}"}
URL = "https://api.getxapi.com/twitter/tweet/advanced_search"

QUERIES = [
    ('T_scale_billions', '("scale to billions" OR "scaling to billions" OR "billion users" OR "billions of users") ("my app" OR "our app" OR "we are building") since:2026-01-01'),
    ('U_documentary_app', '(documenting OR documentary OR "short film" OR cinema) ("my app" OR "the app I built" OR "app I am building") since:2026-01-01'),
    ('V_media_vibe_billion', '("vibe coding" OR "vibe coded") ("billion" OR "billions") filter:media since:2026-01-01'),
    ('W_build_public_video', '("building in public" OR "documenting my journey") filter:media ("app" OR "startup" OR "saas") since:2026-04-01'),
]

out = {}
for name, q in QUERIES:
    r = requests.get(URL, params={"q": q, "product": "Top"}, headers=H, timeout=45)
    if r.status_code != 200:
        print(f"[{name}] HTTP {r.status_code}: {r.text[:100]}"); continue
    tw = r.json().get("tweets", [])
    out[name] = {"query": q, "tweets": tw}
    print(f"\n[{name}] {len(tw)}")
    for t in sorted(tw, key=lambda x: x.get("likeCount", 0) or 0, reverse=True)[:10]:
        a = (t.get("author") or {}).get("userName", "?")
        bio = ((t.get("author") or {}).get("description") or "").replace("\n", " ")[:80]
        txt = " ".join((t.get("text") or "").split())[:210]
        print(f"  @{a} | {t.get('likeCount')} | {str(t.get('createdAt'))[:10]}")
        print(f"    bio: {bio}")
        print(f"    {txt}")
    time.sleep(0.4)

json.dump(out, open("/home/kiro/trend-engine/trends/x/banger-x-results/2026-11-05_vibe-hunt3.json", "w"), indent=1)
