Created
December 31, 2013 05:23
-
-
Save sit/8192947 to your computer and use it in GitHub Desktop.
Import various text files into DayOne without using the DayOne CLI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Usage: mkdir /tmp/export; find path -name *.html | xargs python | |
# import_dayone.py | tee /tmp/export.log | |
# | |
import datetime | |
import plistlib | |
import pytz | |
import uuid | |
import sys | |
def chop_microseconds(delta): | |
return delta - datetime.timedelta(microseconds=delta.microsecond) | |
def dayone_timestamp(t=None): | |
if not t: | |
t = datetime.datetime.now(pytz.utc) | |
now = chop_microseconds(t) | |
return now | |
def dayone_uuid(): | |
# https://dayone.zendesk.com/hc/en-us/articles/200145785-How-is-the-file-name-generated-for-each-entry- | |
return str(uuid.uuid4()).replace("-", "").upper() | |
def dayone_entry(date, text): | |
# https://dayone.zendesk.com/hc/en-us/articles/200145845-What-file-format-is-used-for-my-journal- | |
entry = {} | |
entry["Creation Date"] = dayone_timestamp(date) | |
entry["Creator"] = { | |
"Device Agent": "iPad/iPad2,5", | |
"Generation Date": dayone_timestamp(), | |
"Host Name": "ImportedPython", | |
"OS Agent": "iOS/7.0.4", | |
"Software Agent": "Day One iOS/1.12", | |
} | |
entry["Entry Text"] = text | |
entry["Time Zone"] = "America/New_York" | |
entry["Starred"] = False | |
entry["UUID"] = dayone_uuid() | |
return entry | |
def get_body(fh): | |
lines = fh.readlines() | |
# Deal with my various formats | |
if lines[0].strip() == "---": | |
print "... as YAML" | |
lines.pop(0) | |
while lines[0].strip() != "---": | |
lines.pop(0) | |
lines.pop(0) | |
elif lines[1].startswith("----"): | |
print "... as MD" | |
lines = lines[3:] | |
else: | |
print "... as unknown" | |
return ''.join(lines).strip() | |
def date_from(path): | |
# assumes yyyy/mm/dd.html files | |
(y, m, d) = path.split("/") | |
d, _ = d.split(".") | |
return datetime.datetime(int(y), int(m), int(d), 12, 1, tzinfo=pytz.utc) | |
for path in sys.stdin: | |
path = path.strip() | |
print "Importing", path | |
date = date_from(path) | |
with open(path) as fh: | |
text = get_body(fh) | |
entry = dayone_entry(date, text) | |
outpath = "/tmp/export/" + entry["UUID"] + ".doentry" | |
plistlib.writePlist(entry, outpath) | |
print "...", outpath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment