Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active August 29, 2015 13:56
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 craigeley/9311401 to your computer and use it in GitHub Desktop.
Save craigeley/9311401 to your computer and use it in GitHub Desktop.
Takes a comma separated text file and imports all of the lines into individual day one entries. For more information, see:
#!/usr/bin/ruby
# Reads an hourly log file with comma separated values
# Change the path to your Day One folder on line 43
# Change the path to your text file in line 45
require 'time'
require 'erb'
require 'date'
starred = false
template = ERB.new <<-XMLTEMPLATE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Creation Date</key>
<date><%= time %></date>
<key>Entry Text</key>
<string><%= entry %></string>
<key>Location</key>
<dict>
<key>Administrative Area</key>
<string>United States</string>
<key>Latitude</key>
<real><%= lat %></real>
<key>Longitude</key>
<real><%= long %></real>
</dict>
<key>Starred</key>
<<%= starred %>/>
<key>Tags</key>
<array>
<string><%= tag %></string>
</array>
<key>UUID</key>
<string><%= uuid %></string>
</dict>
</plist>
XMLTEMPLATE
dayonepath = "/Users/USERNAME/Desktop/hourly.dayone/entries/"
f = File.open("/Users/USERNAME/Dropbox/Data/hourly.txt", encoding: 'UTF-8')
lines = f.read
f.close
lines.each_line do |line|
if line =~ /&/
line.gsub!(/[&]/, 'and')
end
if /(?<t>.*),(?<lat>.*),(?<long>.*),(?<entry>.*),(?<tag>.*)/ =~ line
time = Time.parse(t).utc
time = time.to_s
time = time.gsub(/\sUTC/,"Z").gsub(/\s/,"T")
uuid = %x{uuidgen}.gsub(/-/,'').strip
file = "#{dayonepath}#{uuid}.doentry"
fh = File.new(File.expand_path(file),'w+')
fh.puts template.result(binding)
fh.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment