Skip to content

Instantly share code, notes, and snippets.

@solos
Created February 23, 2013 07:03
Show Gist options
  • Save solos/5018772 to your computer and use it in GitHub Desktop.
Save solos/5018772 to your computer and use it in GitHub Desktop.
Offline evernote on android - solos's or diaosi's way to evernote pro.
#!/usr/bin/python
#coding=utf-8
import sqlite3
import os
from lxml import etree
def get_notes(ever_enex):
'''extract notes from .enex file'''
content = open(ever_enex, 'r').read()
content = content.replace('&nbsp', '&#160')
enex_notes = etree.XML(content)
notes = []
for note in enex_notes:
title = note.xpath('title')[0].text
content = note.xpath('content')[0].text
notes.append((title, content))
return notes
def get_note_guid(cursor, note_title):
'''get note guid from sqlite db by note title'''
try:
cursor.execute('''select guid from notes where title = ?;''', (note_title,))
result = cursor.fetchall()
guids = []
for (guid,) in result:
guids.append(guid)
return guids
except Exception, e:
print e
return []
def update_status(conn, guids):
'''update note cached status'''
cursor = conn.cursor()
for guid in guids:
try:
cursor.execute('''update notes set cached = 1 where guid = ?;''', (guid,))
conn.commit()
except Exception, e:
print e
return True
def generate_file(guids, note_content):
'''generate note dirs and files as same as android app'''
for guid in guids:
dir = 'notes/%s/%s/' % (guid[0:3], guid)
os.makedirs(dir)
filename = '%s%s' % (dir, 'content.enml')
open(filename, 'w').write(note_content)
return True
def journey(ever_db, ever_enex):
'''solos's or diaosi's way to evernote pro.'''
conn = sqlite3.connect(ever_db)
conn.text_factory = str
cursor = conn.cursor()
notes = get_notes(ever_enex)
total = len(notes)
for index, note in enumerate(notes):
note_title, note_content = note
note_content = note_content.encode('utf8')
guids = get_note_guid(cursor, note_title)
print 'total: %s, now: %s, title: %s' % (total, index, note_title)
try:
generate_file(guids, note_content)
except Exception, e:
print e
try:
update_status(conn, guids)
except Exception, e:
print e
cursor.close()
conn.close()
if __name__ == '__main__':
ever_db = 'external-xxxxxxxxxx-Evernote.db'
ever_enex = 'Evernote.enex'
journey(ever_db, ever_enex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment