#!/usr/bin/env python3
import os, re, json, time, sys
import requests
from datetime import datetime, timezone, timedelta

env_path = os.path.expanduser("~/.hermes/.env")
API_KEY = None
with open(env_path) as f:
    for line in f:
        if line.startswith("GETXAPI_KEY="):
            API_KEY = line.strip().split("=", 1)[1]
            break
if not API_KEY:
    print("ERROR: no GETXAPI_KEY")
    sys.exit(1)

WATCHLIST = "/home/kiro/trend-engine/trends/x/raw/rising-accounts.md"
text = open(WATCHLIST).read()
handles = re.findall(r"@(\w+)", text)

SKIP_WORDS = {
    "x", "youtube", "facebook", "marketingbrew", "blogs", "nvidia",
    "theankler", "xo2", "besttwitteraccounts", "etcjournal", "letsdatascience",
    "pixflow", "lang", "en", "with_replies", "highlights", "replies"
}
handles = [h for h in handles if h not in SKIP_WORDS and h.lower() not in SKIP_WORDS]
seen = set(); out = []
for h in handles:
    if h not in seen:
        seen.add(h); out.append(h)
handles = out

IRRELEVANT_HANDLES = {
    "AIBuzzNews", "AIGuideHQ", "AITECHio", "AIFrontliner", "AIWarper", "TheAIColony", "AITOPIAai",
    "GoogleLabs", "GoogleAI", "DonutAI", "cladlabs", "4aibsc", "FaithJaiye2187", "KammelaBala", "MyStageAI", "AlexStudio44",
    "AI_Video_Dumps", "AI_with_jasmin", "Ai_Vaidehi", "CharaspowerAI", "ChrisLaubAI",
    "CINEMA505", "Chandan_Perla"
}

def scan_handle(handle):
    url = f"https://api.getxapi.com/twitter/user/tweets?userName={handle}&count=5"
    headers = {"Authorization": f"Bearer {API_KEY}"}
    try:
        resp = requests.get(url, headers=headers, timeout=60)
        data = resp.json()
        return data.get("tweets", []), None
    except Exception as e:
        return None, str(e)

now = datetime.now(timezone.utc)
cutoff = (now - timedelta(days=3)).replace(hour=0, minute=0, second=0, microsecond=0)

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

all_results = {}
failed = []

for handle in handles:
    tweets, err = scan_handle(handle)
    if err:
        failed.append((handle, err))
    elif tweets is None:
        failed.append((handle, "no tweets field"))
    else:
        qualifying = []
        for t in tweets:
            if t.get("retweeted_tweet"):
                continue  # skip RTs
            created = t.get("createdAt", "")
            dt = parse_date(created)
            if dt and dt < cutoff:
                continue
            likes = t.get("likeCount", 0) or 0
            rts = t.get("retweetCount", 0) or 0
            replies = t.get("replyCount", 0) or 0
            views = t.get("viewCount", 0) or 0
            if likes >= 50 or rts >= 10 or views >= 1000:
                score = likes + (rts * 2) + (replies * 3) + (views / 100)
                qualifying.append({
                    "id": t.get("id"),
                    "handle": handle,
                    "text": (t.get("text") or "")[:200],
                    "likes": likes, "rts": rts, "replies": replies, "views": views,
                    "score": score,
                    "created": created,
                })
        if qualifying:
            all_results[handle] = qualifying
    time.sleep(0.8)

today = now.strftime("%Y-%m-%d")
outpath = f"/home/kiro/trend-engine/trends/x/scan_results_{today}.json"
with open(outpath, "w") as f:
    json.dump({"results": all_results, "failed": failed, "date": today}, f, indent=2, default=str)

print(f"Scan complete. Handles scanned: {len(handles)}, failed: {len(failed)}, accounts with qualifying tweets: {len(all_results)}")
print(f"Saved: {outpath}")
