Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hatena blog to Day One exporter
require 'rb-dayone'
module HatenaBlog
class Entry
def initialize(entry)
@entry = entry
end
def meta_data
@entry.split("-----\n")[0]
end
def method_missing(name)
return nil unless %i|title date status|.include?(name)
meta_data.match(/#{name.upcase}:\s(.+?)$/)[1] rescue nil
end
def date_parsed
date_splitted, time = date.split(' ')
date_splitted = date_splitted.gsub(/(\d{2})\/(\d{2})\/(\d{4})/) { "#{$3}-#{$1}-#{$2}" }
Time.parse("#{date_splitted} #{time}")
end
def body
@entry.split("-----\n")[1].delete("BODY:\n")
end
def unified_body
return body if title.nil?
"#{title}\n\n#{body}"
end
def published?
status == 'Publish'
end
def daraft?
!published?
end
end
end
file = File.open("path/to/exported/log").read
entries = file.split("--------\n")
entries.each do |entry|
hb_entry = HatenaBlog::Entry.new(entry)
next if hb_entry.draft?
begin
do_entry = DayOne::Entry.new hb_entry.unified_body, creation_date: hb_entry.date_parsed
do_entry.create!
puts "#{hb_entry.title || hb_entry.body.gsub(/<.+?\/?>/, '')[0..12].concat('...')} has been successfully saved!"
rescue => e
puts entry.split("\n")[0..6].join("\n")
puts e.message
puts e.backtrace.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment