Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martin12333/fa8c25a2413db1221868fb483bb8f830 to your computer and use it in GitHub Desktop.
Save martin12333/fa8c25a2413db1221868fb483bb8f830 to your computer and use it in GitHub Desktop.
Quick and dirty script to import bookmarks into Evernote. Expected bookmarks file is in Delicious format. In my case, I needed to export Diigo bookmarks into Evernote.
from xml.sax.saxutils import escape
from datetime import datetime
from bs4 import BeautifulSoup
headerXml = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export application="Evernote" version="Evernote Mac">"""
footerXml = "</en-export>"
noteXml = unicode("""<note>
<title>{0}</title>
<content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><a href="{1}">{0}</a></en-note>]]>
</content>
<created>{2}</created>
<updated>{2}</updated>
{3}
<note-attributes>
<source-url>{1}</source-url>
</note-attributes>
</note>""")
notes = open('bookmarks.enex', 'w')
soup = BeautifulSoup(open('bookmarks.html'))
links = soup.find_all('a')
notes.write(headerXml)
for link in links:
tagXml = ''
tags = link['tags']
if tags != 'no_tag':
tagXml = ''.join([''.join(['<tag>', tag.strip(), '</tag>']) for tag in tags.split(',')])
notes.write(noteXml.format(
escape(link.contents[0]),
escape(link['href']),
datetime.fromtimestamp(int(link['add_date'])).strftime('%Y%m%dT%H%M%SZ'),
tagXml).encode('utf-8').strip())
notes.write(footerXml)
notes.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment