Skip to content

Instantly share code, notes, and snippets.

@monzou
Created February 12, 2012 06:35
Show Gist options
  • Save monzou/1806794 to your computer and use it in GitHub Desktop.
Save monzou/1806794 to your computer and use it in GitHub Desktop.
Day One Backuper
require 'fssm'
require 'time'
require 'nokogiri'
USER_DIR = "/Users/monzou"
BACKUP_DIR = "#{USER_DIR}/Dropbox/DayOne"
BACKUP_EXTENSION = "txt"
DAYONE_ENTRIES_PATH = "#{USER_DIR}/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone/entries"
def monitor
monitor = FSSM::Monitor.new
monitor.path DAYONE_ENTRIES_PATH do
glob '**/*.doentry'
create do |base, relative|
puts "created: #{Time.now} - #{relative}"
backup(base, relative)
end
update do |base, relative|
puts "updated: #{Time.now} - #{relative}"
backup(base, relative)
end
end
monitor.run
end
def backup_all
Dir::glob("#{DAYONE_ENTRIES_PATH}/*.doentry").each do |f|
backup(File.dirname(f), File.basename(f))
end
end
def backup(base, relative)
write(read(base, relative))
end
def write(entry)
p entry
time = Time.parse(entry["Creation Date"]).localtime.strftime("%Y-%m-%d-%H%M%S")
File.open(File.join(BACKUP_DIR, "#{time}.#{BACKUP_EXTENSION}"), 'w') do |f|
f.puts entry["Entry Text"]
end
end
def read(base, relative)
doc = Nokogiri::XML(File.open(File.join(base, relative), 'r'))
entry = Hash.new
key = nil
doc.xpath("plist/dict/*").each do |e|
if e.name == "key"
key = e.text
else
entry[key] = e.text
end
end
entry
end
if ARGV[0] == "backup"
backup_all
else
monitor
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment