Created
September 26, 2009 17:51
-
-
Save webcracy/194339 to your computer and use it in GitHub Desktop.
A script to import your Wordpress posts on Tumblr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Export your Wordpress posts to Tumblr (also allows to delete some.) | |
# Author: Alexandre Solleiro <[email protected]> | |
# How-to: | |
# 1. Export your Wordpress posts (http://en.blog.wordpress.com/2006/06/12/xml-import-export/) | |
# 2. Edit the information below to match your settings | |
# 3. To import, type "ruby wordpress2tumblr.rb --run" in the command line | |
# 4. To delete some posts, type "ruby wordpress2tumblr.rb --delete 1,2,3,4" where 1,2,3,4 are post numbers. (Be careful not to use spaces between IDs) | |
# Edit these | |
WP_EXPORT_PATH = "./wordpress.2009-09-14.xml" # path to the Wordpress Export file | |
TUMBLR_BLOG_URL = "http://yourblog.tumblr.com" # without a trailing slash! | |
TUMBLR_EMAIL = "[email protected]" # the email of your Tumblr account | |
TUMBLR_PASSWORD = "password" # your Tumblr account password | |
TUMBLR_DRAFT = 1 # 1 saves your posts as drafts on Tumblr; 0 publishes them | |
# Dependencies: the following gems | |
require 'rubygems' | |
require 'open-uri' | |
require 'rest_client' | |
require 'hpricot' | |
require 'json' | |
require 'time' | |
# No need to touch here | |
TUMBLR_WRITE_API = "http://www.tumblr.com/api/write" | |
TUMBLR_DELETE_API = "http://www.tumblr.com/api/delete" | |
# This was borrowed from http://choonkeat.svnrepository.com/svn/rails-plugins/misc/hpricot/rss.rb | |
# It trims the CDATA[] declaration in WP's RSS output | |
def trim_cdata(text) | |
text.gsub!(/\<\!\[CDATA\[(.*)\]\]\>/m, '\1') if text =~ /<\!\[CDATA\[/ && text =~ /\]\]>/ | |
end | |
# Post a Wordpress Post to Tumblr | |
def post_to_tumblr(wp_post) | |
tumblr_post = RestClient.post(TUMBLR_WRITE_API, | |
:email => TUMBLR_EMAIL, | |
:password => TUMBLR_PASSWORD, | |
:generator => "imported with wordpress2tumblr.rb", | |
:type => "regular", | |
:format => "html", # can also be markdown, c.f. Tumblr's API | |
:title => wp_post[:title], | |
:body => wp_post[:body], | |
:date => wp_post[:date], | |
:tags => wp_post[:tags], | |
:private => TUMBLR_DRAFT # saves as draft; 0 to publish | |
) | |
end | |
# Delete a Post from Tumblr | |
def delete_from_tumblr(tumblr_post_id) | |
begin | |
tumblr_deletion = RestClient.post(TUMBLR_DELETE_API, | |
:email => TUMBLR_EMAIL, | |
:password => TUMBLR_PASSWORD, | |
:"post-id" => tumblr_post_id | |
) | |
rescue => e | |
puts e | |
end | |
end | |
# The real action | |
def all_posts_to_tumblr | |
# Parsing the WP eXtended RSS for published posts that we want to export | |
doc = Hpricot.parse(File.read(WP_EXPORT_PATH)) | |
@posts_with_comments = [] | |
(doc/:item).each do |xml_item| | |
# Check if post is supposed to be published - these are the ones we want | |
if (xml_item/'wp:status').innerHTML == "publish" | |
wp_post = {} | |
wp_post[:title] = (xml_item/:title).innerHTML | |
wp_post[:date] = Time.parse((xml_item/:pubdate).inner_html).to_s | |
wp_post[:body] = trim_cdata((xml_item/'content:encoded').innerHTML) | |
# Now we work the tags | |
tags = [] | |
(xml_item/:category).each do |category| | |
if category.attributes['domain'] == 'tag' and category.attributes['nicename'] != nil | |
tags << category.attributes['nicename'] | |
end | |
end | |
wp_post[:tags] = tags.join(',') | |
# IMPORTANT line, actually publishes to Tumblr. Comment it if you want to dry run. | |
tumblr_post = post_to_tumblr(wp_post) | |
puts "Tumblr: " + tumblr_post.to_s | |
end | |
end | |
end # all_posts_to_tumblr | |
# Delete the Posts with given IDs from Tumblr | |
def delete_some_from_tumblr(tumblr_post_ids) | |
tumblr_post_ids.each do |tumblr_post| | |
puts delete_from_tumblr(tumblr_post.to_s) | |
end | |
end | |
# Execution | |
if ARGV[0] == '--run' | |
all_posts_to_tumblr | |
elsif ARGV[0] == '--delete' and ARGV[1] not nil | |
delete_some_from_tumblr(ARGV[1]) | |
else | |
puts "To import to Tumblr, type 'ruby wordpress2tumblr.rb --run'" | |
puts "To delete some posts from Tumblr, type 'ruby wordpress2tumblr.rb --delete 1,2,3,4' where 1,2,3,4 are Tumblr Post IDs - do not use spaces between IDs." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment