Skip to content

Instantly share code, notes, and snippets.

@trevorturk
Forked from mwunsch/wp2tumblr
Created April 28, 2010 23:53
Show Gist options
  • Save trevorturk/382913 to your computer and use it in GitHub Desktop.
Save trevorturk/382913 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment