Created
October 4, 2014 01:28
-
-
Save apiarian/7b2dd7de2091397af0fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# move_entries.py | |
# Aleksandr Pasechnik | |
# | |
# Goes through the Day One journal and moves entries that start with text which | |
# matches the regular expresion search string to a new journal location. It | |
# also moves any associated photos. Day One doesn't currently support multiple | |
# journals, so this is currently only useful for archiving a specific subset of | |
# entries. | |
# | |
# NOTE: base_dir may need to be adjusted to the correct Journal_dayone location | |
# NOTE: It is probably a good idea to have a full journal backup just in case | |
# something goes wrong | |
import os | |
import plistlib | |
import shutil | |
import re | |
dry_run = True | |
base_dir = '~/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone/' | |
new_base_dir = '~/Desktop/newjournal/' | |
entry_title_re = re.compile(r"^FIRST LINE SEARCH$") | |
base_dir = os.path.expanduser(base_dir) | |
entries_dir = os.path.join(base_dir,'entries') | |
photos_dir = os.path.join(base_dir,'photos') | |
new_base_dir = os.path.expanduser(new_base_dir) | |
new_entries_dir = os.path.join(new_base_dir,'entries') | |
new_photos_dir = os.path.join(new_base_dir,'photos') | |
if not dry_run: | |
try: | |
os.mkdir(new_base_dir) | |
except OSError: | |
pass | |
try: | |
os.mkdir(new_entries_dir) | |
except OSError: | |
pass | |
try: | |
os.mkdir(new_photos_dir) | |
except OSError: | |
pass | |
files = os.listdir(entries_dir) | |
files[:] = [file for file in files if file.endswith('.doentry')] | |
for file in files: | |
filename = os.path.join(entries_dir,file) | |
entry = plistlib.readPlist(filename) | |
text = entry['Entry Text'] | |
firstline = text.splitlines()[0] | |
if entry_title_re.match(firstline) is not None: | |
new_filename = os.path.join(new_entries_dir,file) | |
print 'moving "%s" to "%s"'%(filename,new_filename) | |
if not dry_run: | |
shutil.move(filename, new_filename) | |
photofilename = filename.replace('entries','photos').replace('.doentry','.jpg') | |
if os.path.isfile(photofilename): | |
new_photofilename = os.path.join(new_photos_dir,'%s.jpg'%(entry['UUID'],)) | |
print 'moving "%s" to %s"'%(photofilename,new_photofilename) | |
if not dry_run: | |
shutil.move(photofilename, new_photofilename) | |
print 'Done.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment