Last active
August 29, 2015 13:56
-
-
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:
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
#!/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