Skip to content

Instantly share code, notes, and snippets.

@monzou
Created November 19, 2011 15:37
Show Gist options
  • Save monzou/1378968 to your computer and use it in GitHub Desktop.
Save monzou/1378968 to your computer and use it in GitHub Desktop.
複数のはてなダイアリのデータをマージするスクリプト
# encoding: utf-8
require 'rubygems'
require 'nokogiri'
hash = Hash.new
names = [ "account1", "account2" ]
names.each do |name|
doc = Nokogiri::XML(File.open("#{name}.xml"))
doc.xpath("diary/day").each do |e|
date = e.attribute("date").text
title = e.attribute("title").text
text = e.text
if hash.key?(date)
puts "merge: #{date}"
hash[date][:text] << text
else
hash[date] = { :text => text, :title => title }
end
end
end
xml = Nokogiri::XML::Builder.new do |xml|
xml.diary do |diary|
hash.sort { |(k1, v1), (k2, v2)| k1 <=> k2 }.each do |date, h|
diary.day(:date => date, :title => h[:title]) do |day|
puts "#{date}: #{h[:title]}"
day.body(h[:text])
end
end
end
end
File.open("merged.xml", "w") do |f|
f.puts Nokogiri::XML(xml.to_xml, nil, 'utf-8').to_xml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment