#!/usr/bin/env python3
"""Round 2: pin down TrustMRR + AI-built bootstrapped trackers."""
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 = [
    ('G_trustmrr_what', 'trustmrr since:2026-09-15'),
    ('H_vibecoded_mrr', '("vibe coded" OR "vibe-coded" OR "vibecoded") (saas OR app OR tool) (MRR OR "customers" OR revenue) since:2026-09-01'),
    ('I_ai_directory', '("built with AI" OR "AI-built" OR "built by AI") (directory OR database OR "list of") (saas OR startups) since:2026-08-01'),
    ('J_boot_many_customers', '(bootstrapped OR "solo founder" OR "one person") ("10,000 customers" OR "100k users" OR "thousands of customers") since:2026-08-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}"); continue
    tw = r.json().get("tweets", [])
    out[name] = {"query": q, "count": len(tw), "tweets": tw}
    print(f"[{name}] {len(tw)}")
    time.sleep(0.4)

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

for name, blk in out.items():
    tw = sorted(blk["tweets"], key=lambda t: t.get("likeCount", 0) or 0, reverse=True)
    print(f"\n### {name}")
    for t in tw[:6]:
        a = (t.get("author") or {}).get("userName", "?")
        txt = " ".join((t.get("text") or "").split())
        print(f"- @{a} | {t.get('likeCount')} likes | {str(t.get('createdAt'))[:10]}")
        print("  " + txt[:300])
