Created
July 19, 2013 23:12
-
-
Save winny-/6043033 to your computer and use it in GitHub Desktop.
Because Notes.app blew up on me, and here is the solution. :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Simple script to extract your notes from Notes.app's sqlite3 database. It | |
# places the HTML files in the current working directory. The database store | |
# should be located at: | |
# | |
# ~/Library/Containers/com.apple.Notes/Data/Library/Notes/NotesV1.storedata | |
# | |
# This file is released in the public domain. | |
import itertools | |
import re | |
import sqlite3 | |
import sys | |
try: | |
from html.parser import HTMLParser | |
except ImportError: | |
from HTMLParser import HTMLParser | |
class NotesHTMLParser(HTMLParser): | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.expecting_title = False | |
self.note_title = None | |
def handle_starttag(self, tag, attrs): | |
if tag == 'body' and self.note_title is None: | |
self.expecting_title = True | |
def handle_data(self, data): | |
if self.expecting_title: | |
self.note_title = data | |
self.expecting_title = False | |
def retrieve_notes_from_db(notes_file): | |
with sqlite3.connect(notes_file) as conn: | |
c = conn.cursor() | |
c.execute('SELECT zhtmlstring FROM znotebody;') | |
notes = c.fetchall() | |
notes = list(itertools.chain(*notes)) | |
return filter(None, notes) | |
def create_note_html(note): | |
parser = NotesHTMLParser() | |
parser.feed(note) | |
title = parser.note_title | |
if not '</title>' in note: | |
p = note.partition('</head>') | |
assert p[1] | |
note = p[0] + '<title></title>' + p[1] + p[2] | |
p = note.partition('</title>') | |
note = p[0] + title + p[1] + p[2] | |
return (title, note) | |
if __name__ == '__main__': | |
for arg in sys.argv[1:]: | |
for note in retrieve_notes_from_db(arg): | |
note = create_note_html(note) | |
file_name = 'Note_' + re.sub('[^A-Za-z0-9 _-]+', '', note[0]) + '.html' | |
with open(file_name, 'wb') as f: | |
f.write(note[1].encode('utf-8')) | |
sys.stderr.write('Wrote {0}\n'.format(file_name)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment