#!/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 = [
    ('P_native_video_vibe', 'filter:native_video ("vibe coding" OR "vibe coded" OR "vibe-coded") (app OR product OR startup) since:2026-03-01'),
    ('Q_native_video_build_public', 'filter:native_video ("building in public" OR "build in public" OR "documenting") since:2026-03-01'),
    ('R_billion_app', '("billion dollar" OR "$1B" OR "next billion") ("my app" OR "my product" OR "vibe") filter:native_video since:2026-01-01'),
    ('S_cinematic_app', '(cinematic) ("my app" OR "the app I built" OR "built with AI") since:2026-01-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[:120]}"); 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", " ")[:70]
        txt = " ".join((t.get("text") or "").split())[:200]
        print(f"  @{a} | {t.get('likeCount')} | {str(t.get('createdAt'))[:10]} | 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-video-hunt.json", "w"), indent=1)
