-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
257 lines (211 loc) · 8.53 KB
/
sync.py
File metadata and controls
257 lines (211 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import os
import json
import time
import subprocess
import tempfile
from datetime import datetime
from dotenv import load_dotenv
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from rich.table import Table
load_dotenv()
console = Console()
CACHE_FILE = os.path.expanduser("~/.notionmind_cache.json")
# ── load cache ────────────────────────────────────────────────────────────────
def load_cache() -> list:
if not os.path.exists(CACHE_FILE):
return []
with open(CACHE_FILE, "r") as f:
return json.load(f)
# ── save cache ────────────────────────────────────────────────────────────────
def save_cache(notes: list):
with open(CACHE_FILE, "w") as f:
json.dump(notes, f, indent=2)
# ── sync pull ─────────────────────────────────────────────────────────────────
def sync_pull():
from mcp_client import mcp_list_all_notes
console.print("\n[dim]Pulling notes from Notion...[/]")
notes = mcp_list_all_notes(limit=100)
old_cache = load_cache()
old_ids = {n["id"]: n for n in old_cache}
new_count = 0
updated_count = 0
for note in notes:
if note["id"] not in old_ids:
new_count += 1
elif note["summary"] != old_ids[note["id"]].get("summary"):
updated_count += 1
save_cache(notes)
console.print(Panel(
f"[bold green]✓ Sync Pull Complete![/]\n\n"
f"[cyan]Total notes:[/] {len(notes)}\n"
f"[cyan]New notes:[/] {new_count}\n"
f"[cyan]Updated notes:[/] {updated_count}\n"
f"[cyan]Cache:[/] {CACHE_FILE}",
title="Sync Pull"
))
return notes
# ── sync push ─────────────────────────────────────────────────────────────────
def sync_push():
import httpx
console.print("\n[dim]Checking for local edits to push...[/]")
cache = load_cache()
edited = [n for n in cache if n.get("edited", False)]
if not edited:
console.print("[yellow]No local edits found to push.[/]")
return
console.print(f"[green]Found {len(edited)} edited note(s) to push[/]\n")
for note in edited:
console.print(f"[dim]→ Pushing: {note['title']}...[/]")
httpx.patch(
f"https://api.notion.com/v1/pages/{note['id']}",
headers={
"Authorization": f"Bearer {os.environ['NOTION_API_KEY']}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
},
json={
"properties": {
"Summary": {
"rich_text": [{"text": {"content": note["summary"]}}]
},
"Tags": {
"multi_select": [{"name": t} for t in note.get("tags", [])]
}
}
}
)
note["edited"] = False
console.print(f" [green]✓ Pushed:[/] {note['title']}")
save_cache(cache)
console.print(Panel(
f"[bold green]✓ Pushed {len(edited)} note(s) to Notion![/]",
title="Sync Push"
))
# ── edit note locally ─────────────────────────────────────────────────────────
def edit_note():
cache = load_cache()
if not cache:
console.print("[yellow]Cache empty. Run sync pull first![/]")
return
table = Table(title="Select a note to edit", show_lines=True)
table.add_column("#", style="cyan", width=4)
table.add_column("Date", style="white", width=12)
table.add_column("Title", style="white", width=40)
table.add_column("Tags", style="dim", width=20)
for i, n in enumerate(cache, 1):
table.add_row(
str(i),
n["date"],
n["title"],
", ".join(n.get("tags", [])) or "—"
)
console.print(table)
idx = Prompt.ask("[green]Enter number[/]")
if not idx.isdigit() or not (1 <= int(idx) <= len(cache)):
console.print("[red]Invalid number.[/]")
return
note = cache[int(idx) - 1]
# write to temp file
with tempfile.NamedTemporaryFile(
mode="w", suffix=".md", delete=False
) as tmp:
tmp.write(f"# {note['title']}\n")
tmp.write(f"Date: {note['date']}\n")
tmp.write(f"Tags: {', '.join(note.get('tags', []))}\n")
tmp.write(f"\n---\n\n")
tmp.write(note["summary"])
tmpfile = tmp.name
# detect editor
editor = os.environ.get("EDITOR", "nano")
console.print(f"[dim]Opening in {editor}...[/]")
subprocess.run([editor, tmpfile])
# read back edited content
with open(tmpfile, "r") as f:
content = f.read()
# extract summary (everything after ---)
if "---" in content:
new_summary = content.split("---", 1)[1].strip()
else:
new_summary = content.strip()
os.unlink(tmpfile)
if new_summary == note["summary"]:
console.print("[yellow]No changes detected.[/]")
return
# mark as edited in cache
cache[int(idx) - 1]["summary"] = new_summary
cache[int(idx) - 1]["edited"] = True
save_cache(cache)
console.print(Panel(
f"[bold green]✓ Note edited locally![/]\n\n"
f"[cyan]Title:[/] {note['title']}\n"
f"[dim]Run 'sync push' to save changes to Notion.[/]",
title="Edit Note"
))
# ── watch mode ────────────────────────────────────────────────────────────────
def watch_mode():
from mcp_client import mcp_list_all_notes
console.print(Panel(
"[bold cyan]Watch Mode Active[/]\n"
"[dim]Polling Notion every 30 seconds for changes.\n"
"Press Ctrl+C to stop.[/]",
title="Sync Watch"
))
last_cache = {n["id"]: n for n in load_cache()}
try:
while True:
notes = mcp_list_all_notes(limit=100)
current = {n["id"]: n for n in notes}
# detect changes
for nid, note in current.items():
if nid not in last_cache:
console.print(
f"[green]✨ New note:[/] {note['title']} "
f"[dim]({note['date']})[/]"
)
elif note["summary"] != last_cache[nid].get("summary"):
console.print(
f"[cyan]✏️ Updated:[/] {note['title']} "
f"[dim]({note['date']})[/]"
)
# detect deletions
for nid in last_cache:
if nid not in current:
console.print(
f"[red]🗑️ Deleted:[/] {last_cache[nid]['title']}"
)
last_cache = current
save_cache(notes)
now = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{now} — watching... (Ctrl+C to stop)[/]", end="\r")
time.sleep(30)
except KeyboardInterrupt:
console.print("\n[dim]Watch mode stopped.[/]")
# ── sync interactive menu ─────────────────────────────────────────────────────
def run_sync():
console.print(Panel(
"[bold cyan]NotionMind Sync[/]\n\n"
"[dim]Options:\n"
" 1. Pull — fetch latest from Notion to local cache\n"
" 2. Push — push local edits back to Notion\n"
" 3. Edit — edit a note locally then push\n"
" 4. Watch — monitor Notion for real-time changes\n"
" 0. Back[/]",
title="Sync"
))
choice = Prompt.ask("[green]Choose[/]", choices=["0", "1", "2", "3", "4"])
if choice == "1":
sync_pull()
elif choice == "2":
sync_push()
elif choice == "3":
sync_pull()
edit_note()
elif choice == "4":
sync_pull()
watch_mode()
elif choice == "0":
return
if __name__ == "__main__":
run_sync()