69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
"""Extract ALL todos from ALL existing inspiration digests and push to todo API."""
|
|
import os, re, requests
|
|
|
|
DIGEST_DIR = os.path.expanduser("~/inspiration-collector/ai-insights")
|
|
TODO_API = "http://127.0.0.1:9001/api/todos"
|
|
|
|
|
|
def get_all_digests():
|
|
"""Recursively find all digest files."""
|
|
result = []
|
|
for root, dirs, files in os.walk(DIGEST_DIR):
|
|
for f in files:
|
|
if f.endswith("_digest.md") and not f.endswith(".bak.md"):
|
|
result.append(os.path.join(root, f))
|
|
result.sort()
|
|
return result
|
|
|
|
|
|
def extract_todos(filepath):
|
|
with open(filepath) as f:
|
|
content = f.read()
|
|
todos = []
|
|
in_todo_section = False
|
|
for line in content.split("\n"):
|
|
# Markdown checkbox: - [ ] something
|
|
m = re.match(r"\s*[-*]\s*\[\s*[ ]\s*\]\s*(.+)", line)
|
|
if m:
|
|
todos.append(m.group(1).strip())
|
|
continue
|
|
# Numbered todos under "待办事项" section
|
|
m2 = re.match(r"\s*\d+\.\s*\*{0,2}(.+?)\*{0,2}\s*$", line)
|
|
if m2 and in_todo_section:
|
|
text = m2.group(1).strip()
|
|
if text and len(text) > 5:
|
|
todos.append(text)
|
|
# Track if we're in the todo section
|
|
if "待办" in line or "TODO" in line:
|
|
in_todo_section = True
|
|
elif line.strip() == "" and in_todo_section:
|
|
in_todo_section = False
|
|
return todos
|
|
|
|
|
|
def push_todos(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__":
|
|
digests = get_all_digests()
|
|
print(f"Found {len(digests)} digest files")
|
|
total = 0
|
|
for fp in digests:
|
|
todos = extract_todos(fp)
|
|
if todos:
|
|
pushed = push_todos(todos)
|
|
total += pushed
|
|
print(f" {os.path.basename(fp)}: {len(todos)} found, {pushed} new")
|
|
print(f"\nTotal: {total} new todos pushed")
|