Skip to content

Instantly share code, notes, and snippets.

@lmuntaner
Last active July 12, 2023 02:03
Show Gist options
  • Save lmuntaner/21a15ba50e2e0a04b6bc4dd8ee0ce9ec to your computer and use it in GitHub Desktop.
Save lmuntaner/21a15ba50e2e0a04b6bc4dd8ee0ce9ec to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Converts Day One exported notes to `enex` for Evernote importing
# Not complete
# It only exports:
# - title (assumed as the first line of the content starting with `# `)
# - content (rest of the content)
# - tags
# - created
# - updated
# NOTE: Apparently Tags are not imported. There seems to be a problem on the Evernote side.
# I exported a note with tags from Evernote, and then reimported it and the tags were gone.
import sys
import os
import json
import base64
import hashlib
from datetime import datetime
from xml.sax.saxutils import escape
def convert_journal(journal, name):
notes = []
for entry in journal['entries']:
title = entry['text'].splitlines()[0][2:]
paragraphs = [ '<p>' + line.replace('\.', '.') + '</p>' for line in entry['text'].splitlines()[1:] ]
content = ''.join(paragraphs)
tags = ''.join([ '<tag>' + tag + '</tag>' for tag in entry.get('tags', []) ])
xml_note = """
<note>
<title>%(title)s</title>
<content>
<![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
<div>
%(content)s
</div>
</en-note>]]>
</content>
%(tags)s
<created>%(created)s</created>
<updated>%(updated)s</updated>
<note-attributes>
<latitude>39.56948852539062</latitude>
<longitude>3.210029445028365</longitude>
<altitude>81.99990081787109</altitude>
<author>Llorenç Muntaner</author>
<source>desktop.mac</source>
<reminder-order>0</reminder-order>
</note-attributes>
</note>
""" % {
"title": title,
"content": content,
"tags": tags,
"created": datetime.strptime(entry['creationDate'], '%Y-%m-%dT%H:%M:%SZ').strftime("%Y%m%dT%H%M%SZ"),
"updated": datetime.strptime(entry['modifiedDate'], '%Y-%m-%dT%H:%M:%SZ').strftime("%Y%m%dT%H%M%SZ"),
}
notes.append(xml_note)
xml = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="%(created)s" application="Evernote" version="Evernote Mac 7.14 (458265)">
%(notes)s
</en-export>
""" % { "created": datetime.today().strftime("%Y%m%d - %d %B, %Y"), "notes": ''.join(notes) }
out_file = name + '.enex'
with open( out_file, "w" ) as f:
f.write(xml)
def find_and_convert():
for fn in [fn for fn in os.listdir() if fn.endswith(".json")]:
with open(fn, "r") as f:
journal = json.loads(f.read())
convert_journal(journal, fn.split('.')[0])
find_and_convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment