Skip to content

Instantly share code, notes, and snippets.

@lucianofuentes
Created April 9, 2012 00:59
Show Gist options
  • Save lucianofuentes/2340627 to your computer and use it in GitHub Desktop.
Save lucianofuentes/2340627 to your computer and use it in GitHub Desktop.
Evernote to .html file python script
import sys
import os.path
import datetime
import codecs
import sqlite3
import codecs
import getopt
import datetime
from optparse import OptionParser
from xml.etree.ElementTree import Element,ElementTree,SubElement
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
EVERNOTE_DIR = os.path.join(os.path.expanduser("~"), "Library", "Application Support", "Evernote", "data")
# this might be different, depending on the Evernote version
EVERNOTE_DIR = os.path.join(EVERNOTE_DIR, "101370")
EVERNOTE_DB_PATH = os.path.join(EVERNOTE_DIR, "Evernote.sql")
def get_article_tags(conn,pk):
c = conn.cursor()
c.execute("""
SELECT e.zname2
FROM Z_12TAGS t,
ZENATTRIBUTEDENTITY e
WHERE e.z_ent = 17 and t.z_17tags = e.z_pk
AND zdeleted is null
and t.z_12notes = ?
""",(pk,))
tags = []
for row in c:
tags.append(row[0])
c.close()
return tags
def read_url(path):
if not os.path.exists(path):
raise Exception("Evernote database file does not exist at: %s" % path)
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute("SELECT Z_PK,ZTITLE, ZSOURCEURL,ZCREATED FROM Zenattributedentity where Z_ENT=12 and ZSOURCEURL IS NOT NULL AND zdeleted is null")
data = []
# From: https://github.com/kjk/web-blog/blob/master/scripts/evernote-to-file.py
# ZCREATED timestamp is in weird format that looks like 31 years after
# unix timestamp. so this is a crude way to approximate this. Might be off
# by a day or so
td = datetime.timedelta(days=31*365+8)
for pk,title,url,dt in c:
tags = get_article_tags(conn,pk)
created_on = datetime.datetime.fromtimestamp(dt)+td
# print "%s - %s" % (title,created_on)
data.append((title,url,tags,created_on))
conn.close()
return data
def write_bookmarks(data,fname):
dl = Element("DL")
for title,url,tags,created in data:
dt = SubElement(dl,"DT")
a = SubElement(dt,"A",attrib=dict(HREF=url,TAGS=",".join(tags),ADD_DATE=created.strftime('%s')))
a.text = title
dt.tail = "\n"
file = open(fname,"w")
file.write("""<!DOCTYPE NETSCAPE-Bookmark-file-1>\n""")
file.write("""<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n""")
file.write("""<TITLE>Evernote Bookmarks</TITLE>\n""")
ElementTree(dl).write(file,encoding="UTF-8")
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-i", dest="dbpath",default=EVERNOTE_DB_PATH,
help='Evernote sqlite database. Defaults to %default', metavar="FILE")
parser.add_option("-o", dest="out",default="bookmarks.html",
help="write bookmarks to FILE. Defaults to %default", metavar="FILE")
parser.add_option("-t", dest="tag",
help="additional tag to add to all bookmarks", metavar="TAG")
(options, args) = parser.parse_args()
urls = read_url(options.dbpath)
if options.tag:
for u in urls:
u[2].append(options.tag)
write_bookmarks(urls,options.out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment