Skip to content

Instantly share code, notes, and snippets.

@arika
Created November 12, 2008 09:53
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 arika/24122 to your computer and use it in GitHub Desktop.
Save arika/24122 to your computer and use it in GitHub Desktop.
imports data generated by tddump.rb to typo.
#!/usr/bin/ruby
#
# this script imports dump data of tDiary 2.2.x to Typo 5.1.x.
# (ref: http://gist.github.com/24118)
#
# usage: /path/to/typo-base-dir/script/runner tdiary2typo.rb /path/to/tdiary.dat
#
# Author: akira at arika.org
# License: Ruby's
require 'cgi'
require 'nkf'
def utf8(str)
NKF.nkf('-Ew', str)
end
author = User.find_by_login('your username of typo')
text_filter_none = TextFilter.find_by_name('none')
blog = Blog.default
url_base = blog.base_url
diary = Marshal.load(ARGF)
n_articles = n_comments = n_trackbacks = 0
diary.each do |diary_hash|
info = nil
diary_title = diary_hash[:title]
diary_hash[:article].each_with_index do |article, idx|
n_articles += 1
a_title = article[:title]
a_title = "#{diary_title} #{idx + 1}" if a_title.nil? || a_title.empty?
info = {
:title => CGI.unescapeHTML(utf8(a_title)),
:body => utf8(article[:body]),
:user => author,
:created_at => diary_hash[:date],
:updated_at => diary_hash[:last_modified],
:allow_comments => true,
:allow_pings => true,
:text_filter => text_filter_none,
:keywords => article[:category].map {|c| %Q!"#{utf8(c)}"!}.join(' '),
}
if diary_hash[:visible]
info[:state] = :published
else
info[:state] = :draft
end
begin
a = Article.create(info)
a.published_at = diary_hash[:date] + 60*idx
a.save!
rescue Exception
require 'pp'; pp info
raise
end
end
next if diary_hash[:comment].empty? && diary_hash[:trackback].empty?
pub_time = diary_hash[:date] + 60*diary_hash[:article].size
info[:title] = "#{diary_hash[:date].strftime('%Y-%m-%d')}のツッコミ・トラックバック"
info[:body] = %Q!<p>(<typo:tdiarycompat method="my" url="#{diary_hash[:date].strftime('%Y%m%d')}">#{diary_hash[:date].strftime('%Y-%m-%d')}の記事を読む</typo:tdiarycompat>)</p>!
info[:allow_comments] = true
info[:allow_pings] = true
info[:state] = :published
info[:keywords] = '' # '"コメントホルダー"'
a = Article.create!(info)
a.published_at = pub_time
a.save!
comment_updated_at = Time.at(0)
[:comment, :trackback].each do |to|
diary_hash[to].each do |comment|
cinfo = {
:author => utf8(comment[:author] || '(anonymous)'),
:email => comment[:mail] || '',
:created_at => comment[:time],
:updated_at => comment[:time],
:ip => '127.0.0.1',
#:published => comment[:visible],
}
c_body = utf8(comment[:body] || '(blank)')
if to == :comment
n_comments += 1
cinfo[:article] = a
cinfo[:body] = c_body
cinfo[:text_filter] = text_filter_none
to_klass = Comment
else
n_trackbacks += 1
cinfo[:blog_name] = utf8(comment[:blog_name].blank? ? '(blank)' : comment[:blog_name])
cinfo[:title] = utf8(comment[:title].blank? ? '(blank)' : comment[:title])
cinfo[:url] = utf8(comment[:url])
cinfo[:excerpt] = c_body
to_klass = a.trackbacks
end
begin
c = to_klass.create!(cinfo)
c.state = comment[:visible] ? :presumed_ham : :presumed_spam
c.save!
raise unless c.published? == comment[:visible]
rescue Exception
require 'pp'; pp cinfo; pp c
raise
end
if comment[:visible]
comment_updated_at = comment[:time] if comment[:time] > comment_updated_at
end
end
end
a.state = comment_updated_at > Time.at(0) ? :published : :draft
a.published_at = pub_time
a.save!
end
puts "#{n_articles} articles / #{n_comments} comments / #{n_trackbacks} trackbacks"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment