"""Extract todos from the latest inspiration digest + sync to todo API.""" import json import os import re import requests DIGEST_DIR = os.path.expanduser("~/inspiration-collector/ai-insights/daily") TODO_API = "http://127.0.0.1:9001/api/todos" def get_latest_digest(): """Find the most recent digest file.""" if not os.path.isdir(DIGEST_DIR): return None files = [f for f in os.listdir(DIGEST_DIR) if f.endswith("_digest.md")] if not files: return None files.sort(reverse=True) return os.path.join(DIGEST_DIR, files[0]) def extract_todos(filepath): """Extract [ ] todo items from the digest markdown.""" with open(filepath) as f: content = f.read() todos = [] # Match markdown checkboxes: - [ ] something for line in content.split("\n"): m = re.match(r"\s*[-*]\s*\[\s*[ ]\s*\]\s*(.+)", line) if m: todos.append(m.group(1).strip()) return todos def push_todos(todos): """Send extracted todos to the API (skip if already exists).""" # Get existing todos try: resp = requests.get(TODO_API, timeout=5) existing = [t["text"] for t in resp.json().get("todos", [])] except: existing = [] count = 0 for text in todos: if text not in existing: requests.post(TODO_API, json={"text": text, "source": "inspiration"}, timeout=5) count += 1 return count if __name__ == "__main__": filepath = get_latest_digest() if not filepath: print("No digest found") exit(0) todos = extract_todos(filepath) print(f"Found {len(todos)} todos in {os.path.basename(filepath)}") if todos: pushed = push_todos(todos) print(f"Pushed {pushed} new todos to API")