#!/usr/bin/env python3
"""2026-11-02 freshness probe: re-query suspicious handles directly and compare
max(createdAt) against the saved scan (stale-cache check). Plus full texts/media."""
import os, json, time
import requests
from datetime import datetime

ENV = os.path.expanduser("~/.hermes/.env")
API_KEY = None
for line in open(ENV):
    if line.startswith("GETXAPI_KEY="):
        API_KEY = line.strip().split("=", 1)[1]; break

D = "/home/kiro/trend-engine/trends/x/raw/"
d = json.load(open(D + "scan_results_2026-11-02_FULL.json"))

def pd(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

print("FRESHNESS PROBE (saved scan vs live re-query)")
for h in ["higgsfield_ai","AIWarper","higgsfield","Flovaai","runwayml","pika_labs","Kling_ai","NVIDIAAI","CharaspowerAI"]:
    saved = d["raw"].get(h, [])
    smax = max([pd(t.get("createdAt","")) for t in saved if pd(t.get("createdAt",""))], default=None)
    try:
        r = requests.get("https://api.getxapi.com/twitter/user/tweets",
                         headers={"Authorization": f"Bearer {API_KEY}"},
                         params={"userName": h, "count": 5}, timeout=60)
        tw = r.json().get("tweets", [])
        lmax = max([pd(t.get("createdAt","")) for t in tw if pd(t.get("createdAt",""))], default=None)
        lmin = min([pd(t.get("createdAt","")) for t in tw if pd(t.get("createdAt",""))], default=None)
        same = (smax == lmax)
        print(f"  @{h:15s} saved_max={smax} live_max={lmax} live_min={lmin} n={len(tw)} "
              f"{'SAME (no new posts)' if same else 'NEWER DATA AVAILABLE'}")
    except Exception as e:
        print(f"  @{h:15s} ERROR {e}")
    time.sleep(0.8)

print("\n" + "="*70)
print("FULL TEXT + MEDIA for key posts")
IDS = {
 "Peak SF Halloween": None,
}
# walk all raw for specific tweet texts
KEYS = ["Peak SF Halloween", "AI made this 3D animation", "Loose Watercolor Drift",
        "IP Safety Check", "Remove watermark", "in the Seedance 2.5 Draft",
        "automate UGC ads", "NemotronLabs RE-USE", "Kling AI Originals",
        "religious exemption so he wouldn't", "LinkedIn for your Grok",
        "Chroma Awards", "Magnific Move", "Hailuo", "Seedance 2.5 made this crazy"]
seen=set()
for h, tw in d["raw"].items():
    for t in tw:
        txt = (t.get("text") or "")
        for k in KEYS:
            if k.lower() in txt.lower() and t.get("id") not in seen:
                seen.add(t.get("id"))
                med = t.get("media") or []
                mtype = med[0].get("type") if med and isinstance(med[0], dict) else "none"
                print(f"\n@{h} | {t.get('createdAt')} | {t.get('likeCount')}L {t.get('retweetCount')}RT "
                      f"{t.get('replyCount')}R {t.get('viewCount')}V | media={mtype} | id={t.get('id')}")
                print(f"  https://x.com/{h}/status/{t.get('id')}")
                print(f"  {' '.join(txt.split())[:900]}")
                q = t.get("quoted_tweet")
                if q:
                    print(f"  QUOTED @{(q.get('author') or {}).get('userName','?')}: {' '.join((q.get('text') or '').split())[:500]}")
                break

print("\n" + "="*70)
print("AI LABS bucket (all)")
for x in d["qualifying"]:
    if x["handle"] in ("OpenAI","AnthropicAI"):
        print(f"  @{x['handle']} | {x['likes']}L {x['rt']}RT {x['replies']}R {x['views']}V | {x['date']}")
        print(f"    {' '.join(x['text'].split())[:300]}")
        print(f"    {x['url']}")

print("\n" + "="*70)
print("IRRELEVANT bucket AI-relevant (15)")
import re
AI = re.compile(r"\b(ai\b|a\.i\.|seedance|kling|runway|veo|sora|minimax|hailuo|higgsfield|midjourney|"
  r"nano ?banana|gpt-?[0-9.]*|chatgpt|gemini|generative|render|vfx|ai film|aifilm|grokbot|"
  r"motion capture|deepfake|comfyui|diffusion|(ai|3d|generated) ?animation|prompt)", re.I)
for x in d["qualifying"]:
    if x["handle"] in ("AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai",
        "GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI",
        "AlexStudio44","AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","AIScottAdams",
        "AISuperDomain","AI_Jasonyu","AIByLeo","Soaima_Ai","different_ai","via_ai_","Teamily_AI") and AI.search(x["text"]):
        print(f"  @{x['handle']} | {x['likes']}L {x['views']}V | {x['date']} | {' '.join(x['text'].split())[:150]}")
