forked from storizzi/notes-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_to_notes.py
More file actions
497 lines (414 loc) · 19.2 KB
/
sync_to_notes.py
File metadata and controls
497 lines (414 loc) · 19.2 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import hashlib
import json
import os
import subprocess
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from bs4 import BeautifulSoup
from notes_export_utils import NotesExportTracker, get_tracker
from sync_notes_bridge import create_note, get_modified_date, update_note
from sync_settings import load_settings, apply_cli_overrides
import output_format as fmt
def compute_file_hash(file_path: Path) -> str:
"""Compute SHA-256 hash of a file's contents."""
h = hashlib.sha256()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
return h.hexdigest()
def embed_images_as_base64(html_content: str, base_dir: Path) -> str:
"""Embed local images as base64 data URIs in HTML content.
Inverse of extract_images.py: converts local file references back to
inline base64 so Apple Notes can accept them.
"""
import base64
import mimetypes
soup = BeautifulSoup(html_content, "html.parser")
for img_tag in soup.find_all("img"):
src = img_tag.get("src", "")
if not src or src.startswith("data:"):
continue
# Resolve the image path relative to the base directory
img_path = None
candidates = [
base_dir / src,
base_dir / "attachments" / Path(src).name,
]
# Also try stripping leading ./
if src.startswith("./"):
candidates.insert(0, base_dir / src[2:])
for candidate in candidates:
if candidate.exists():
img_path = candidate
break
if img_path is None:
print(f" Warning: Image not found: {src}")
continue
# Read and encode
mime_type, _ = mimetypes.guess_type(str(img_path))
if mime_type is None:
ext = img_path.suffix.lower().lstrip(".")
mime_map = {"png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg",
"gif": "image/gif", "webp": "image/webp", "tiff": "image/tiff"}
mime_type = mime_map.get(ext, "image/png")
with open(img_path, "rb") as f:
img_data = base64.b64encode(f.read()).decode("ascii")
img_tag["src"] = f"data:{mime_type};base64,{img_data}"
return str(soup)
def markdown_to_html(markdown_path: Path) -> str:
"""Convert a markdown file to HTML using pandoc."""
result = subprocess.run(
["pandoc", "-f", "markdown", "-t", "html", str(markdown_path)],
capture_output=True, text=True, timeout=60,
)
if result.returncode != 0:
raise RuntimeError(f"pandoc failed: {result.stderr.strip()}")
return result.stdout
def get_sync_status(note_info: Dict[str, Any], md_file: Path
) -> Tuple[bool, bool]:
"""Determine if local and/or remote have changed since last sync.
Returns (local_changed, remote_changed).
"""
# Local change detection
last_hash = note_info.get("localFileHashAtLastSync", "")
if md_file.exists() and last_hash:
current_hash = compute_file_hash(md_file)
local_changed = current_hash != last_hash
elif md_file.exists() and not last_hash:
# Never synced before - if file exists and no hash recorded, consider it unchanged
# (it was generated by export, not manually edited)
local_changed = False
else:
local_changed = False
# Remote change detection
last_remote_mod = note_info.get("appleNotesModifiedAtLastSync", "")
current_remote_mod = note_info.get("modified", "")
if last_remote_mod and current_remote_mod:
remote_changed = current_remote_mod != last_remote_mod
elif not last_remote_mod:
# Never synced - not considered "changed"
remote_changed = False
else:
remote_changed = False
return local_changed, remote_changed
def create_conflict_file(md_file: Path, local_content: str,
note_info: Dict[str, Any]) -> Path:
"""Create a .conflict.md sidecar file showing both versions."""
conflict_path = md_file.with_suffix(".conflict.md")
remote_mod = note_info.get("modified", "unknown")
with open(conflict_path, "w", encoding="utf-8") as f:
f.write("# CONFLICT DETECTED\n\n")
f.write(f"Both the local file and Apple Notes have been modified since last sync.\n\n")
f.write(f"- Local file: `{md_file.name}`\n")
f.write(f"- Remote modified: {remote_mod}\n\n")
f.write("---\n\n")
f.write("## LOCAL VERSION\n\n")
f.write(local_content)
f.write("\n\n---\n\n")
f.write("## REMOTE VERSION\n\n")
f.write("(Re-export from Apple Notes to see the remote version)\n")
return conflict_path
def find_new_local_files(tracker: NotesExportTracker) -> List[Dict[str, Any]]:
"""Find markdown files that don't match any note in tracking JSON."""
new_files = []
md_root = Path(tracker.root_directory) / "md"
if not md_root.exists():
return new_files
for json_file in tracker.get_all_data_files():
notebook_data = tracker.load_notebook_data(json_file)
folder_name = json_file.stem
# Get all known filenames for this notebook
known_filenames = set()
for note_info in notebook_data.values():
fn = note_info.get("filename", "")
if fn:
known_filenames.add(fn)
# Scan md directory for this notebook
if tracker._uses_subdirs():
md_folder = md_root / folder_name
else:
md_folder = md_root
if not md_folder.exists():
continue
for md_file in md_folder.glob("*.md"):
if md_file.suffix == ".md" and not md_file.name.endswith(".conflict.md"):
stem = md_file.stem
if stem not in known_filenames:
# Determine account and folder from the notebook name
# Format is typically "account-folder"
parts = folder_name.split("-", 1)
account = parts[0] if len(parts) > 0 else "iCloud"
folder = parts[1] if len(parts) > 1 else "Notes"
new_files.append({
"md_file": md_file,
"filename": stem,
"notebook": folder_name,
"json_file": json_file,
"account": account,
"folder": folder,
})
return new_files
class SyncEngine:
"""Main sync engine for bidirectional Apple Notes sync."""
def __init__(self, settings: Optional[Dict[str, Any]] = None,
dry_run: bool = False):
self.tracker = get_tracker()
self.settings = settings or load_settings()
self.dry_run = dry_run
self.stats = {
"synced": 0,
"skipped": 0,
"conflicts": 0,
"created": 0,
"errors": 0,
}
def run(self, create_new: bool = False,
filter_folders: Optional[str] = None,
filter_accounts: Optional[str] = None):
"""Run the sync-back process."""
folder_filter = set()
if filter_folders:
folder_filter = {f.strip() for f in filter_folders.split(",")}
account_filter = set()
if filter_accounts:
account_filter = {a.strip() for a in filter_accounts.split(",")}
# Process existing notes
for json_file in self.tracker.get_all_data_files():
notebook_data = self.tracker.load_notebook_data(json_file)
folder_name = json_file.stem
# Apply folder/account filters
if folder_filter or account_filter:
parts = folder_name.split("-", 1)
account_part = parts[0] if len(parts) > 0 else ""
folder_part = parts[1] if len(parts) > 1 else ""
if folder_filter and folder_part not in folder_filter and folder_name not in folder_filter:
continue
if account_filter and account_part not in account_filter and folder_name not in account_filter:
continue
for note_id, note_info in notebook_data.items():
if "deletedDate" in note_info:
continue
self._process_note(note_id, note_info, folder_name, json_file)
# Create new notes if requested
if create_new or self.settings.get("createNewNotes"):
self._create_new_notes()
self._print_summary()
def _get_md_file(self, note_info: Dict, folder_name: str) -> Optional[Path]:
"""Get the markdown file path for a note."""
filename = note_info.get("filename", "")
if not filename:
return None
md_root = Path(self.tracker.root_directory) / "md"
if self.tracker._uses_subdirs():
return md_root / folder_name / f"{filename}.md"
return md_root / f"{filename}.md"
def _process_note(self, note_id: str, note_info: Dict,
folder_name: str, json_file: Path):
"""Process a single note for sync-back."""
md_file = self._get_md_file(note_info, folder_name)
if md_file is None or not md_file.exists():
return
full_note_id = note_info.get("fullNoteId", "")
if not full_note_id:
# Can't sync without full note ID
return
local_changed, remote_changed = get_sync_status(note_info, md_file)
if not local_changed and not remote_changed:
self.stats["skipped"] += 1
return
if local_changed and remote_changed:
# Conflict
strategy = self.settings.get("conflictStrategy", "abort")
if strategy == "abort":
local_content = md_file.read_text(encoding="utf-8")
conflict_path = create_conflict_file(md_file, local_content, note_info)
fmt.emit("conflict", filename=md_file.name, conflict_file=conflict_path.name)
print(f" CONFLICT: {md_file.name} - both sides changed. Created {conflict_path.name}")
self.stats["conflicts"] += 1
return
elif strategy == "local":
pass # Fall through to sync local -> remote
elif strategy == "remote":
self.stats["skipped"] += 1
return # Let the next export overwrite local
if local_changed:
self._sync_local_to_remote(note_id, note_info, md_file, folder_name, json_file)
elif remote_changed:
# Remote changed only - this will be handled by re-export
self.stats["skipped"] += 1
def _sync_local_to_remote(self, note_id: str, note_info: Dict,
md_file: Path, folder_name: str, json_file: Path):
"""Sync a locally-changed markdown file back to Apple Notes."""
filename = note_info.get("filename", "")
full_note_id = note_info.get("fullNoteId", "")
print(f" Syncing: {filename} -> Apple Notes")
if self.dry_run:
print(f" [DRY RUN] Would sync {md_file.name} to Apple Notes")
self.stats["synced"] += 1
return
try:
# Convert markdown to HTML
html_content = markdown_to_html(md_file)
# Embed images as base64
html_content = embed_images_as_base64(html_content, md_file.parent)
# Pre-write conflict check
current_mod = get_modified_date(full_note_id)
stored_mod = note_info.get("appleNotesModifiedAtLastSync", "")
if stored_mod and current_mod and current_mod != stored_mod:
print(f" WARNING: Remote note changed since detection. Aborting sync for {filename}")
local_content = md_file.read_text(encoding="utf-8")
create_conflict_file(md_file, local_content, note_info)
self.stats["conflicts"] += 1
return
# Extract title from markdown (first # heading or filename)
title = self._extract_title(md_file)
# Update the note
result = update_note(full_note_id, title, html_content)
if result.get("success"):
self._mark_note_synced(json_file, note_id, md_file,
result.get("modifiedDate", ""))
self.stats["synced"] += 1
fmt.emit("synced", filename=filename, notebook=folder_name, note_id=note_id)
print(f" Synced successfully")
else:
err = result.get('error', 'Unknown error')
fmt.emit("error", filename=filename, notebook=folder_name, message=err)
print(f" ERROR: {err}")
self.stats["errors"] += 1
except Exception as e:
fmt.emit("error", filename=filename, notebook=folder_name, message=str(e))
print(f" ERROR syncing {filename}: {e}")
self.stats["errors"] += 1
def _create_new_notes(self):
"""Create new Apple Notes from unmatched local markdown files."""
new_files = find_new_local_files(self.tracker)
for new_file in new_files:
md_file = new_file["md_file"]
print(f" Creating new note: {md_file.name}")
if self.dry_run:
print(f" [DRY RUN] Would create new note from {md_file.name}")
self.stats["created"] += 1
continue
try:
html_content = markdown_to_html(md_file)
html_content = embed_images_as_base64(html_content, md_file.parent)
title = self._extract_title(md_file)
result = create_note(
new_file["account"],
new_file["folder"],
title,
html_content,
)
if result.get("success"):
new_note_id = result["fullNoteId"]
# Add to tracking JSON
self._add_new_note_to_tracking(
new_file["json_file"],
new_note_id,
new_file["filename"],
md_file,
result.get("modifiedDate", ""),
)
self.stats["created"] += 1
print(f" Created successfully (ID: ...{new_note_id[-20:]})")
else:
print(f" ERROR: {result.get('error', 'Unknown error')}")
self.stats["errors"] += 1
except Exception as e:
print(f" ERROR creating note from {md_file.name}: {e}")
self.stats["errors"] += 1
def _extract_title(self, md_file: Path) -> str:
"""Extract the title from a markdown file."""
try:
with open(md_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line.startswith("# "):
return line[2:].strip()
if line and not line.startswith("#"):
break
except Exception:
pass
# Fallback to filename
return md_file.stem.replace("-", " ")
def _mark_note_synced(self, json_file: Path, note_id: str,
md_file: Path, new_mod_date: str):
"""Update the tracking JSON after a successful sync."""
notebook_data = self.tracker.load_notebook_data(json_file)
if note_id in notebook_data:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
notebook_data[note_id]["lastSyncedToNotes"] = now
notebook_data[note_id]["localFileHashAtLastSync"] = compute_file_hash(md_file)
notebook_data[note_id]["appleNotesModifiedAtLastSync"] = new_mod_date
notebook_data[note_id]["syncCount"] = notebook_data[note_id].get("syncCount", 0) + 1
notebook_data[note_id]["syncSource"] = self.settings.get("syncSource", "markdown")
self.tracker.save_notebook_data(json_file, notebook_data)
def _add_new_note_to_tracking(self, json_file: Path, full_note_id: str,
filename: str, md_file: Path, mod_date: str):
"""Add a newly created note to the tracking JSON."""
from sync_notes_bridge import get_modified_date as _get_mod
notebook_data = self.tracker.load_notebook_data(json_file)
# Extract a simple ID from the full note ID
parts = full_note_id.split("/")
simple_id = parts[-1] if parts else full_note_id
if simple_id.startswith("p"):
simple_id = simple_id[1:]
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
notebook_data[simple_id] = {
"filename": filename,
"fullNoteId": full_note_id,
"created": mod_date,
"modified": mod_date,
"firstExported": now,
"lastExported": now,
"exportCount": 1,
"lastSyncedToNotes": now,
"localFileHashAtLastSync": compute_file_hash(md_file),
"appleNotesModifiedAtLastSync": mod_date,
"syncCount": 1,
"syncSource": self.settings.get("syncSource", "markdown"),
}
self.tracker.save_notebook_data(json_file, notebook_data)
def _print_summary(self):
"""Print sync summary."""
fmt.emit("summary", command="sync_to_notes", **self.stats)
print("")
print("SYNC SUMMARY:")
print(f" Synced to Apple Notes: {self.stats['synced']}")
print(f" New notes created: {self.stats['created']}")
print(f" Skipped (no changes): {self.stats['skipped']}")
print(f" Conflicts: {self.stats['conflicts']}")
print(f" Errors: {self.stats['errors']}")
def run_sync(dry_run: bool = False, create_new: bool = False,
conflict: Optional[str] = None,
filter_folders: Optional[str] = None,
filter_accounts: Optional[str] = None):
"""Entry point for running sync from the command line."""
settings = load_settings()
settings = apply_cli_overrides(settings, conflict=conflict, create_new=create_new)
engine = SyncEngine(settings=settings, dry_run=dry_run)
engine.run(
create_new=create_new,
filter_folders=filter_folders,
filter_accounts=filter_accounts,
)
return engine.stats
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Sync local markdown changes back to Apple Notes")
parser.add_argument("--dry-run", action="store_true", help="Show what would be synced")
parser.add_argument("--create-new", action="store_true", help="Create new notes from unmatched files")
parser.add_argument("--conflict", choices=["abort", "local", "remote"], help="Conflict strategy")
parser.add_argument("--filter-folders", help="Comma-separated folder filter")
parser.add_argument("--filter-accounts", help="Comma-separated account filter")
fmt.add_json_arg(parser)
args = parser.parse_args()
fmt.setup_from_args(args)
run_sync(
dry_run=args.dry_run,
create_new=args.create_new,
conflict=args.conflict,
filter_folders=args.filter_folders,
filter_accounts=args.filter_accounts,
)