I just migrated all my posts from Textpattern to Blogger with a bit of ruby. This should actually work for any MetaWeblog enabled blog.
First I grabbed some code from Marcus Crafter that I found after many many Google searches. Since that didn't really do what I needed, but was a really good start, I hacked it up to use the ruby-blogger client library. It saved me a TON of work. I did have to apply this small patch:
--- client.rb (revision 24)
+++ client.rb (working copy)
@@ -147,6 +147,7 @@
xmlFragment.instruct!
xmlFragment.tag!("entry","xmlns"=>"http://www.w3.org/2005/Atom","xmlns:app"=>'http://purl.org/atom/app#') do | doc |
doc.title(@title,:type=>'text')
+ doc.published(@published)
doc.content(@content,:type=>'html')
doc.tag!("author") do | author |
author.email @author_email
@@ -242,7 +243,7 @@
if links.nil?
- raise ArgumentError "links parameter is required!"
+ raise ArgumentError("links parameter is required!")
end
Then I ended up with this script:
require 'xmlrpc/client'
require 'rexml/document'
require 'rexml/xpath'
require 'time'
require 'rubygems'
require 'RedCloth'
require '/Users/kelp/checkouts/ruby-blogger/lib/client.rb'
POST_URI = 'http://www.blogger.com/feeds//posts/default'
EMAIL = 'username@gmail.com'
PASSWORD= 'password'
USER = 'username'
OLD_BLOG_URL = "http://example.com/"
OLD_BLOG_URI = "/rpc/"
OLD_BLOG_USER = 'username'
OLD_BLOG_PASS = 'password'
OLD_BLOG_ID = blogid
POSTS = number of posts
class Blog
def initialize(host, path, username, password, blog_id = 1, port = 80)
@server = XMLRPC::Client.new(host, path, port)
@blog_id = blog_id
@username = username
@password = password
end
def posts(count = 5)
puts "Getting Posts"
@server.call('metaWeblog.getRecentPosts', @blog_id, @username, @password, count)
end
end
oldblog = Blog.new(OLD_BLOG_URL, OLD_BLOG_URI, OLD_BLOG_USER, OLD_BLOG_PASS, OLD_BLOG_ID)
oldblog.posts(POSTS).reverse.each_with_index do |post, index|
blogger_post = GData::Post.new
puts "#{index}: migrating post: #{post['title']}"
puts ""
time = post["dateCreated"].to_time
blogger_post.published = Time.parse(time.to_s).xmlschema
blogger_post.title = post["title"]
body = RedCloth.new(post["description"]).to_html
blogger_post.body = body
blogger_post.author_name = USER
blogger_post.author_email = EMAIL
GData::Client.logger.level = Logger::DEBUG
client = GData::Client.new(EMAIL,PASSWORD,{"service.post"=>POST_URI})
client.save(blogger_post)
end
Several constants have to be set at the start of the script. Then it should go through and pull the posts out of your old blog and dump them into blogger. It does assume the old blog used
Textile for formating and converts every post body to html. Also it doesn't migrate any comments or post tags. Maybe I will get to that later. Thanks to
this handy page, I have a nice way to syntax highlight the code.
Technorati Tags: blogging, ruby