Skip to content

Instantly share code, notes, and snippets.

@toland
Created March 15, 2010 03:03
Show Gist options
  • Save toland/332449 to your computer and use it in GitHub Desktop.
Save toland/332449 to your computer and use it in GitHub Desktop.
Script to import Wordpress content into Tumblr
require 'rubygems'
require 'net/http'
require 'nokogiri'
TUMBLR_USER = 'your@email.com'
TUMBLR_PASS = 'yourpassword'
url = URI.parse('http://www.tumblr.com/api/write')
# Read the WP export file
counter = 0
doc = Nokogiri::XML(File.read("wordpress.xml"))
doc.xpath('//item').each do |item|
status = item.at_xpath('wp:status').text
title = item.at_xpath('title').text
if %w(publish draft).include?(status)
puts "publishing item #{title}..."
counter += 1
res = Net::HTTP.post_form(url, {
'email' => TUMBLR_USER,
'password' => TUMBLR_PASS,
'type' => 'regular',
'format' => 'markdown',
'title' => title,
'date' => item.at_xpath('wp:post_date').text,
'slug' => item.at_xpath('wp:post_name').text,
'body' => item.at_xpath('content:encoded').text.gsub(/<!--more-->/, ''),
'tags' => item.xpath('category[@nicename]').map(&:text).join(','),
'state' => (status == 'publish') ? 'published' : 'draft'
})
puts res.body
sleep(1)
if counter % 5 == 0
# sleep for a longer amount of time every 5 items
sleep(30)
end
else
puts "Ignoring item #{title}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment