#!/usr/bin/env python3
"""Deeper pull of @adamcitron_ mechanics posts via search (older + keyword)."""
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"

Q = [
    ('recent_all', 'from:adamcitron_ since:2026-10-01'),
    ('creatives', 'from:adamcitron_ (créa OR crea OR statique OR vidéo OR video OR hook OR angle) since:2026-08-15'),
    ('structure', 'from:adamcitron_ (CBO OR ABO OR "bid cap" OR "cost cap" OR zombie OR adset OR adsets) since:2026-08-15'),
    ('product_offer', 'from:adamcitron_ (offre OR offer OR bundle OR pricing OR panier OR AOV OR "sales tax") since:2026-08-15'),
]

seen = {}
for name, q in Q:
    r = requests.get(URL, params={"q": q, "product": "Latest"}, headers=H, timeout=45)
    if r.status_code != 200:
        print(f"[{name}] HTTP {r.status_code}"); continue
    tw = r.json().get("tweets", [])
    print(f"[{name}] {len(tw)}")
    for t in tw:
        seen[t.get("id")] = t
    time.sleep(0.4)

tw = sorted(seen.values(), key=lambda t: str(t.get("createdAt")), reverse=True)
print("\nunique:", len(tw))
json.dump(tw, open("/home/kiro/trend-engine/trends/x/banger-x-results/2026-11-05_adamcitron_deep.json", "w"), indent=1)

# print the mechanic-heavy ones
KEY = ("créa", "crea", "statique", "vidéo", "video", "hook", "angle", "abo", "cbo",
       "bid cap", "cost cap", "zombie", "adset", "offre", "bundle", "panier", "aov")
for t in tw:
    txt = " ".join((t.get("text") or "").split())
    low = txt.lower()
    if any(k in low for k in KEY) and "Résultats du Jour" not in txt:
        print(f"\n[{str(t.get('createdAt'))[:10]} | {t.get('likeCount')} likes]")
        print(txt[:900])
