#!/usr/bin/env python3
"""GetXAPI search: databases/tools tracking bootstrapped AI-built SaaS growth."""
import json, os, sys, 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
assert key, "no GETXAPI_KEY"

H = {"Authorization": f"Bearer {key}"}
URL = "https://api.getxapi.com/twitter/tweet/advanced_search"

QUERIES = [
    ('A_tool_db',
     '("is there a tool" OR "is there a database" OR "any tool" OR "any directory") '
     '(bootstrapped OR "solo founder" OR indie) saas since:2026-06-01'),
    ('B_vibe_scale',
     '("vibe coded" OR "vibe coding") ("thousands of customers" OR "1000 customers" OR "10000 users") '
     'saas since:2026-06-01'),
    ('C_trustmrr',
     '(trustmrr OR "trust mrr" OR "verified mrr" OR "verified revenue") since:2026-06-01'),
    ('D_plg_boot',
     '("product-led growth" OR "product does the marketing" OR "product sells itself") '
     '(bootstrapped OR "no funding" OR "no vc") AI since:2026-06-01'),
    ('E_built_ai_mrr',
     '("built with cursor" OR "built with claude" OR "built with claude code" OR "built in a weekend") '
     'saas (MRR OR revenue) since:2026-06-01'),
    ('F_leaderboard',
     '(leaderboard OR directory) (bootstrapped OR "indie hackers" OR "solo founders") '
     '(MRR OR revenue) AI since:2026-06-01'),
]

out = {}
for name, q in QUERIES:
    try:
        r = requests.get(URL, params={"q": q, "product": "Top"}, headers=H, timeout=45)
        if r.status_code != 200:
            out[name] = {"error": f"HTTP {r.status_code}: {r.text[:160]}", "query": q}
            print(f"[{name}] HTTP {r.status_code}")
            continue
        d = r.json()
        tw = d.get("tweets", [])
        out[name] = {"query": q, "count": len(tw), "tweets": tw}
        print(f"[{name}] {len(tw)} tweets")
    except Exception as e:
        out[name] = {"error": str(e)[:160], "query": q}
        print(f"[{name}] ERR {e}")
    time.sleep(0.4)

p = "/home/kiro/trend-engine/trends/x/banger-x-results/2026-11-04_saas-db-search.json"
json.dump(out, open(p, "w"), indent=1)
print("saved", p)
