import json, re, datetime, subprocess
base="/home/kiro/trend-engine/trends/x/raw/"
D="2026-10-17"
cur=json.load(open(base+f"scan_results_{D}_FULL.json"))
q=cur["qualifying"]; raw=cur["raw"]
def P(s): return datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

print("=== higgsfield_ai / Flovaai / Kling_ai / azed_ai fetch windows ===")
for h in ["higgsfield_ai","Flovaai","Kling_ai","azed_ai","AIWarper","AI__TSUBAKI","WuxiaRocks","pika_labs","Pika_labs","EHuanglu","c_valenzuelab","TheAIColony"]:
    ts=raw.get(h) or raw.get(h.lower())
    if not ts: print(h,"MISSING"); continue
    ds=[P(t["createdAt"]) for t in ts if t.get("createdAt")]
    print(f"{h:16s} n={len(ts):3d} oldest={min(ds).strftime('%Y-%m-%d %H:%M')} newest={max(ds).strftime('%Y-%m-%d %H:%M')}")

print("\n=== WAN 3.0 grep inside higgsfield_ai raw specifically ===")
for t in raw.get("higgsfield_ai",[]):
    txt=(t.get("text") or "")
    if re.search(r"WAN|Wan 3|unlimited|UNLIMITED|60%|API", txt):
        print(f"{t.get('createdAt','')[:16]} | {t.get('likeCount')}L {t.get('viewCount')}V | {txt.replace(chr(10),' ')[:230]}")

print("\n=== ALL qualifying: Flovaai / AI__TSUBAKI / WuxiaRocks / pika_labs / TheAIColony / AIWarper ===")
for h in ["Flovaai","AI__TSUBAKI","WuxiaRocks","pika_labs","TheAIColony","AIWarper","azed_ai","AIFrontliner","AIBuzzNews","DonutAI","AI_with_jasmin","ChrisLaubAI","Ai_Vaidehi","AISuperDomain","AI_Jasonyu","AIByLeo","AIWarper"]:
    rows=[t for t in q if t["handle"]==h]
    if not rows: continue
    print(f"\n-- @{h} ({len(rows)} qualifying)")
    for t in sorted(rows, key=lambda x:-x["score"]):
        print(f"   {t['date'][:16]} {t['likes']}L {t['views']}V score={t['score']} :: {t['text'].replace(chr(10),' ')[:170]}")

print("\n=== t.co resolution for text-light top posts ===")
def resolve(u):
    try:
        r=subprocess.run(["curl","-sIL","--max-time","20","-o","/dev/null","-w","%{url_effective}",u],capture_output=True,text=True,timeout=30)
        return r.stdout.strip()
    except Exception as e:
        return "ERR "+str(e)
for h,needle in [("c_valenzuelab","11028"),("CharaspowerAI","70648")]:
    for t in raw.get(h,[]):
        if str(t.get("likeCount"))==needle:
            txt=t.get("text") or ""
            print(f"\n@{h} {t.get('createdAt')[:16]} {t.get('likeCount')}L {t.get('viewCount')}V")
            print("  text:", repr(txt[:200]))
            m=re.findall(r"https://t\.co/\w+", txt)
            for u in m:
                print("  resolve", u, "->", resolve(u))
            md=t.get("media")
            print("  media:", json.dumps(md)[:400] if md else None)
            print("  isReply:", t.get("isReply"), "| quoted:", bool(t.get("quoted_tweet")), "| type:", t.get("type"))
