Skip to content

Instantly share code, notes, and snippets.

@apiarian
Created September 23, 2013 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apiarian/6665199 to your computer and use it in GitHub Desktop.
Save apiarian/6665199 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# Aleksandr Pasechnik
# Looks through the Day One journal and finds the latest entry that starts with
# the "Entry Start Text" provided on the command line. The script then asks for
# input, reading lines until it comes to one that contains just a single full
# stop "." . The script asks for confirmation and appends a new line followed
# by the entered text to the end of the entry.
#
# NOTE: the user is responsible for creating new entries as necessary,
# presumably once per day.
import os
import sys
import plistlib
import subprocess
import datetime
import dateutil.tz
if len(sys.argv) != 2:
print 'usage: %s "Entry Start Text"'%(sys.argv[0],)
sys.exit()
search_string = sys.argv[1]
base_dir = '~/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone/'
base_dir = os.path.expanduser(base_dir)
entries_dir = os.path.join(base_dir,'entries')
files = os.listdir(entries_dir)
files[:] = [file for file in files if file.endswith('.doentry')]
entries = []
for file in files:
filename = os.path.join(entries_dir,file)
entry = plistlib.readPlist(filename)
entry['@@FILENAME@@'] = filename
entries.append(entry)
entries = sorted(entries, key=lambda entry:entry['Creation Date'])
entries[:] = [e for e in entries if e['Entry Text'].splitlines()[0].startswith(search_string)]
if len(entries) == 0:
print 'no entries found'
sys.exit()
last_entry = entries[-1]
print 'found entry %s created %s'%(last_entry['UUID'],last_entry['Creation Date'].replace(tzinfo=dateutil.tz.tzutc()).astimezone(dateutil.tz.tzlocal()),)
print 'please enter new text, followed by a line with a "."'
new_text = ''
l = sys.stdin.readline()
while l != '.\n':
new_text = new_text + l
l = sys.stdin.readline()
confirmation = raw_input('append the text? [y]/n : ')
if confirmation.lower() == 'n':
print 'aborting'
sys.exit()
last_entry['Entry Text'] = last_entry['Entry Text']+'\n'+new_text
filename = last_entry['@@FILENAME@@']
del last_entry['@@FILENAME@@']
plistlib.writePlist(last_entry,filename)
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment