Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
## WP2TUMBLR: WordPress 2 Tumblr Importing tool
##
## Usage: wp2tumblr [OPTION] WORDPRESS_XML_FILE
##
## Import the WORDPRESS_XML_FILE to a Tumblr blog.
## Provide `--group` option to publish to a group.
## This could take a long time...
##
## To install, download the script and chmod to 755.
begin
require 'optparse'
require 'rubygems'
require 'tumblr'
require 'highline/import'
rescue LoadError
abort "Error: #{$!}. Make sure you `gem install tumblr-rb`"
end
email = nil
password = nil
group = nil
ARGV.options do |option|
option.banner = "Usage: wp2tumblr [options] [file]"
option.separator ""
option.separator "Options"
auth_text = 'Email Address and Password, separated by a colon'
option.on('-a EMAIL:PASSWORD', '--auth EMAIL:PASSWORD', auth_text) do |auth|
email,password = auth.split(':')
end
option.on('--group=GROUP', 'Publish to a group blog') do |value|
group = value
end
option.separator ""
option.parse!
end
if ARGV.empty? && STDIN.tty?
puts ARGV.options
exit
end
if STDIN.tty?
email = ask("Email Address: ") if !email
password = ask("Password: ") { |q| q.echo = false } if !password
end
abort 'You need to provide an e-mail address.' if email.blank?
abort 'You need to provide a password.' if password.blank?
puts 'Authenticating to Tumblr...'
Tumblr.new(email, password).authenticate.perform do |response|
if !response.success?
abort %Q(Oh no! Something went wrong. Tumblr said #{response.code}: "#{response.message}")
end
end
puts 'Parsing WordPress XML document...'
wp_xml = Crack::XML.parse(ARGF.read)
items = wp_xml['rss']['channel']['item']
puts 'Generating Posts...'
posts = items.collect do |item|
post = Tumblr::Post::Regular.new
post.title = item['title']
post.body = item['content:encoded']
post.slug = item['wp:post_name']
post.date = item['wp:post_date']
post.tags item['category'].uniq if item['category']
post.state = :draft if !item['wp:status'].eql?('publish')
post.group = group if group
post.generator = "wp2tumblr"
post
end
abort 'No posts found.' if posts.blank?
published_count = 0
requests = posts.collect do |post|
request = post.write(email,password)
request.on_complete do |response|
if response.success?
published_count += 1
else
puts %Q(Error publishing post: "#{post.title}". Tumblr says #{response.code}: "#{response.message}")
end
end
request
end
puts "Publishing #{requests.count} posts..."
if requests.count <= 60
Weary.batch(requests).perform
else
# If there are too many requests, throttle appropriately.
# Make yourself a cup of tea.
requests.each {|req| req.perform; sleep 1 }
end
puts "Successfully published #{published_count} posts to Tumblr."
exit
@gyaresu
Copy link

gyaresu commented Apr 14, 2011

@judsond Worked here for self hosted Wordpress import.

Thanks.

@allirense
Copy link

allirense commented Jul 30, 2012

Does this still work? I'm getting this when I try to run the script:

/Library/Ruby/Gems/1.8/gems/tumblr-rb-1.3.0/lib/tumblr/post.rb:13:in parameters': undefined methodblank?' for [:title, :body]:Array (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/tumblr-rb-1.3.0/lib/tumblr/post/regular.rb:10
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:inrequire'
from /Library/Ruby/Gems/1.8/gems/tumblr-rb-1.3.0/lib/tumblr/post.rb:184
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:inrequire'
from /Library/Ruby/Gems/1.8/gems/tumblr-rb-1.3.0/lib/tumblr.rb:8
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in gem_original_require' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from ./wp2tumblr:16

@nz
Copy link

nz commented Dec 28, 2012

Looks like this gist may be out of date with its dependencies. @mwunsch, any chance of an update?

@allisonburtch
Copy link

allisonburtch commented Mar 23, 2013

I'd love an update as well. Thanks! @mwunsch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment