#!/usr/bin/env python3
import subprocess, json, os, re
from datetime import datetime, timezone

BASE = os.path.expanduser("~/Claude/Projects/에이전트구축")
X_FEED = os.path.join(BASE, "x_feed.jsonl")
AS_FILE = os.path.join(BASE, "x_scrape.applescript")

def collect():
    r = subprocess.run(['osascript', AS_FILE], capture_output=True, text=True, timeout=90)
    raw = r.stdout.strip()
    if not raw or raw == "missing value":
        print("수집 실패:", r.stderr[:200])
        return
    existing = set()
    if os.path.exists(X_FEED):
        with open(X_FEED) as f:
            for line in f:
                try: existing.add(json.loads(line).get("url"))
                except: pass
    added = 0
    with open(X_FEED, 'a') as f:
        for item in raw.split('###'):
            parts = item.split('@@@')
            if len(parts) != 3: continue
            pub, text, url = parts[0].strip(), parts[1].strip(), parts[2].strip()
            if not url or url in existing: continue
            m = re.search(r'x\.com/([^/]+)/', url)
            author = '@'+m.group(1) if m else ''
            f.write(json.dumps({"url": url, "pub": pub, "text": text,
                "author": author, "ts": datetime.now(timezone.utc).isoformat()},
                ensure_ascii=False) + "\n")
            existing.add(url); added += 1
    print(f"{added}개 신규 수집")

if __name__ == "__main__":
    collect()
