#!/usr/bin/env python3
"""2026-10-28: new-since-yesterday diff, Runway Ads check, irrelevant/labs buckets, like rates."""
import json, re
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-28_FULL.json"))
Y = json.load(open(f"{RAW}/scan_results_2026-10-27_FULL.json"))
def pd(s): return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

qtoday = {x["url"] for x in D["qualifying"]}
qyest  = {x["url"] for x in Y["qualifying"]}
new = [x for x in D["qualifying"] if x["url"] not in qyest]
new.sort(key=lambda x: x["score"], reverse=True)
print(f"=== QUALIFYING URLS NEW SINCE YESTERDAY: {len(new)} of {len(D['qualifying'])} ===")
for x in new[:25]:
    print(f"@{x['handle']}|{x['likes']}L|{x['views']}V|{x['date']}|{x['score']:.0f}|{x['text'][:120]}|{x['url']}")

print("\n=== RUNWAY ADS / ADS grep over raw (any date) ===")
for h, tws in D["raw"].items():
    for t in tws:
        txt = t.get("text") or ""
        if re.search(r"runway\s*ads|runway\.com/product/ads", txt, re.I):
            print(f"@{h} {pd(t['createdAt']).astimezone(timezone.utc):%m-%d %H:%M} {t.get('likeCount',0)}L/{t.get('viewCount',0)}V | {txt[:400]}")

print("\n=== IRRELEVANT bucket, AI-relevant only ===")
AI = re.compile(r"\b(ai|seedance|kling|runway|sora|veo|llm|gpt|claude|gemini|video|prompt|model)\b", re.I)
IRR = {"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",
       "interesting_aIl","hubermanlab","EinsiaAI","different_ai","numerai","openart_ai","via_ai_"}
irr = [x for x in D["qualifying"] if x["handle"] in IRR and AI.search(x["text"])]
irr.sort(key=lambda x: x["score"], reverse=True)
print(f"{len(irr)} AI-relevant")
for x in irr[:15]:
    print(f"@{x['handle']}|{x['likes']}L|{x['views']}V|{x['date']}|{x['text'][:150]}")

print("\n=== AI LABS ===")
for x in sorted([x for x in D["qualifying"] if x["handle"] in {"OpenAI","AnthropicAI","NVIDIAAI"}], key=lambda z: z["score"], reverse=True):
    print(f"@{x['handle']}|{x['likes']}L|{x['rt']}RT|{x['views']}V|{x['date']}|{x['text'][:170]}")

print("\n=== CORRECTED CORE TOP 10 LIKE RATES ===")
OFF={"interesting_aIl","hubermanlab"}; FILM={"DiscussingFilm","nofilmschool"}; LAB={"OpenAI","AnthropicAI","NVIDIAAI"}
core=[x for x in D["qualifying"] if x["handle"] not in IRR and x["handle"] not in FILM and x["handle"] not in LAB]
core.sort(key=lambda x: x["score"], reverse=True)
for i,x in enumerate(core[:10],1):
    lr = x["likes"]/x["views"]*100 if x["views"] else 0
    print(f"{i}|@{x['handle']}|{x['likes']}L|{x['views']}V|like_rate={lr:.2f}%|{x['date']}")

print("\n=== KLING / azed / ElsaSofia in-window originals ===")
for h in ("Kling_ai","azed_ai","ElsaSofia__AI","c_valenzuelab","AI__TSUBAKI","ItsMaryamAi","Soaima_Ai"):
    tws=[t for t in D["raw"].get(h,[]) if not t.get("retweeted_tweet") and not (t.get("text") or "").startswith("RT @")]
    print(f"@{h}: {len(tws)} originals fetched")
    for t in sorted(tws, key=lambda z:z.get("likeCount",0), reverse=True)[:4]:
        print(f"   {pd(t['createdAt']).astimezone(timezone.utc):%m-%d %H:%M} {t.get('likeCount',0)}L/{t.get('viewCount',0)}V | {(t.get('text') or '')[:200]}")
