Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created March 29, 2012 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slinkp/2239364 to your computer and use it in GitHub Desktop.
Save slinkp/2239364 to your computer and use it in GitHub Desktop.
iPhone Notes extractor
"""
This takes the sqlite 'notes' database from an iphone and dumps it to
flat text files in the current directory, preserving modification
times.
Uses 'lynx' (separate program) to dump html to plain text.
"""
import sqlite3
import datetime
import re
import subprocess
import os
import time
encoding = 'utf8'
def text_from_html(html):
lynx = subprocess.Popen('lynx --dump -stdin', shell=True, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
text, err = lynx.communicate(input=html)
if err or (lynx.returncode != 0):
raise RuntimeError("lynx failed with code %s and output: %r" % (lynx.returncode, err))
# Lynx leaves some extra leading space.
text = re.sub(r'^ ', '', text, flags=re.MULTILINE)
return text
def handle_row(row):
title = re.sub('\W+', ' ', row['title'])
fname = re.sub('\W+', '_', title) + '.txt'
output = open(fname, 'w')
output.write(('title: %s\n' % title).encode(encoding))
created = datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=row['creation_date'])
output.write('created: %s\n' % created.ctime())
modtime = datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=row['modification_date'])
output.write('modified: %s\n' % modtime.ctime())
output.write('\n\n')
html = row['data'].encode('utf8')
output.write(text_from_html(html))
output.close()
# Fix file timestamps.
atime = time.time()
mtime = time.mktime(modtime.timetuple())
os.utime(fname, (atime, mtime))
def main(db):
conn = sqlite3.connect(db)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('SELECT * FROM Note, note_bodies ON Note.ROWID = note_bodies.note_id')
for row in cursor.fetchall():
handle_row(row)
if __name__ == '__main__':
import sys
db = sys.argv[1]
main(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment