#!/usr/bin/env python3
"""Live staleness probe + key-post context for xmon 2026-11-04."""
import os, json, time, re
import requests
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
ENV = os.path.expanduser("~/.hermes/.env")
KEY = None
for line in open(ENV):
    if line.startswith("GETXAPI_KEY="):
        KEY = line.strip().split("=", 1)[1]; break

d = json.load(open(f"{RAW}/scan_results_2026-11-04_FULL.json"))
raw = d["raw"]
NOW = datetime.now(timezone.utc)

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

PROBE = ["higgsfield","higgsfield_ai","CuriousRefuge","karenxcheng","ItsMaryamAi","AIWarper",
         "pika_labs","EHuanglu","Kling_ai","runwayml","CharaspowerAI","AI__TSUBAKI"]
print("===== LIVE STALENESS PROBE =====")
for h in PROBE:
    try:
        r = requests.get("https://api.getxapi.com/twitter/user/tweets",
                         headers={"Authorization": f"Bearer {KEY}"},
                         params={"userName": h, "count": 5}, timeout=60)
        tw = r.json().get("tweets", []) if r.status_code == 200 else []
        lv = [pd(t.get("createdAt","")) for t in tw]
        lv = [x for x in lv if x]
        saved = [pd(t.get("createdAt","")) for t in raw.get(h,[])]
        saved = [x for x in saved if x]
        live_max = max(lv) if lv else None
        saved_max = max(saved) if saved else None
        same = "SAME" if (live_max and saved_max and live_max == saved_max) else "DIFF"
        hrs = (NOW-live_max).total_seconds()/3600 if live_max else -1
        print(f"  @{h}: live_max={live_max.strftime('%m-%d %H:%M') if live_max else '?'} ({hrs:.1f}h) "
              f"saved_max={saved_max.strftime('%m-%d %H:%M') if saved_max else '?'} -> {same} status={r.status_code}")
    except Exception as e:
        print(f"  @{h}: ERROR {e}")
    time.sleep(0.8)

print("\n===== EHuanglu CYBERPUNK CONTEXT =====")
for t in raw.get("EHuanglu", []):
    txt = (t.get("text") or "").replace("\n"," ")
    if "Cyberpunk" in txt or "cyberpunk" in txt:
        dt = pd(t.get("createdAt",""))
        print(f"  {dt.strftime('%Y-%m-%d %H:%M') if dt else '?'}|{t.get('likeCount')}L|{t.get('retweetCount')}RT|{t.get('replyCount')}R|{t.get('viewCount')}V|type={t.get('type')}|media={[m.get('type') for m in (t.get('media') or [])]}")
        print(f"    TEXT: {txt[:400]}")
        print(f"    ID: {t.get('id')}")
        q = t.get("quoted_tweet")
        if q: print(f"    QUOTED: {(q.get('text') or '')[:300]}")

print("\n===== OPENART LAUNCH VIDEOS + OTHER NEW TOOL POSTS =====")
for h in ("openart_ai","heyglif","Kling_ai","runwayml"):
    for t in raw.get(h, []):
        dt = pd(t.get("createdAt",""))
        if not dt or dt < NOW - timedelta(days=3): continue
        txt = (t.get("text") or "").replace("\n"," ")
        if re.search(r"(Launch Video|Introducing|now on|is live|new series|Originals)", txt, re.I):
            print(f"  @{h}|{dt.strftime('%m-%d %H:%M')}|{t.get('likeCount')}L|{t.get('viewCount')}V|{txt[:320]}")
            print(f"     https://x.com/{h}/status/{t.get('id')}")

print("\n===== PRICING / COST MENTIONS IN-WINDOW =====")
rx = re.compile(r"(\$\d|per second|/sec|price|pricing|credits|subscription|per month|/mo)", re.I)
seen=set()
for h, tw in raw.items():
    for t in tw:
        dt = pd(t.get("createdAt",""))
        if not dt or dt < NOW.replace(hour=0,minute=0,second=0,microsecond=0)-timedelta(days=3): continue
        txt = (t.get("text") or "").replace("\n"," ")
        if rx.search(txt) and (t.get("likeCount") or 0) >= 50:
            k = txt[:60]
            if k in seen: continue
            seen.add(k)
            print(f"  @{h}|{t.get('likeCount')}L|{t.get('viewCount')}V|{txt[:230]}")
