Last active
August 29, 2015 14:09
-
-
Save morygonzalez/4e149fa2cbb8e35aaecb 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
#!/usr/bin/env ruby | |
require 'csv' | |
require 'active_support/all' | |
require 'rb-dayone' | |
class DiaryFromDayOne | |
attr_reader :target_date, :title, :content, :created_at | |
def initialize(row) | |
@target_date = Date.parse(row[0]) | |
@title = row[3] | |
@content = row[9].gsub(/<br>/, "\n") | |
@entry_text = entry_text | |
@created_at = Time.parse(row[10]) | |
end | |
def entry_text | |
<<-ENTRY.strip_heredoc | |
#{title_for_entry}#{content} | |
#{created_at} | |
ENTRY | |
end | |
def title_for_entry | |
if title.present? | |
"#{title}\n\n" | |
end | |
end | |
def save | |
entry = DayOne::Entry.new self.entry_text, creation_date: self.target_date.to_time | |
entry.create! | |
end | |
end | |
file_count = 0 | |
files = Dir.glob(File.join(File.dirname(__FILE__), 'notelog/[0-9]*.txt')) | |
files.each do |path| | |
begin | |
File.open(path, encoding: 'cp932:utf-8') do |file| | |
while l = file.gets | |
row = l.split(',') | |
converter = DiaryFromDayOne.new(row) | |
converter.save | |
file_count += 1 | |
end | |
end | |
rescue => e | |
puts "Error!" | |
puts path | |
puts e.inspect | |
end | |
end | |
puts file_count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment