Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The script I used to import posts from my Wordpress blog into a new Jekyll one - modified to add tags and categories and NOT convert to Markdown
%w(rubygems pp sequel fileutils yaml active_support/inflector).each{|g| require g}
module WordPress
def self.import(database, user, password, table_prefix = "wp", host = 'localhost')
db = Sequel.mysql(database, :user => user, :password => password, :host => host, :encoding => 'utf8')
%w(_posts _drafts images/posts/featured).each{|folder| FileUtils.mkdir_p folder}
query = <<-EOS
SELECT post_title, post_name, post_date, post_content, post_excerpt, ID, guid, post_status, post_type, post_status,
( SELECT guid
FROM #{table_prefix}_posts
WHERE ID = ( SELECT meta_value
FROM #{table_prefix}_postmeta
WHERE post_id = post.ID AND meta_key = "_thumbnail_id") ) AS post_image
FROM #{table_prefix}_posts AS post
WHERE post_type = 'post'
EOS
categories_and_tags_query = <<-EOS
SELECT t.taxonomy, term.name
FROM #{table_prefix}_term_relationships AS tr
INNER JOIN #{table_prefix}_term_taxonomy AS t ON t.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN #{table_prefix}_terms AS term ON term.term_id = t.term_id
WHERE tr.object_id = %d
ORDER BY tr.term_order
EOS
db[query].each do |post|
title = post[:post_title]
slug = post[:post_name]
date = post[:post_date]
content = post[:post_content]
status = post[:post_status]
name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, slug]
image = File.basename(post[:post_image]) rescue ""
categories = []
post_tags = []
puts title
`wget -O "images/posts/featured/#{image}" "#{post[:post_image]}"` unless File::exists?("images/posts/featured/#{image}") || post[:post_image].nil?
db[categories_and_tags_query % post[:ID]].each do |category_or_tag|
if category_or_tag[:taxonomy] == 'post_tag'
post_tags << category_or_tag[:name]
else
categories << category_or_tag[:name]
end
end
data = {
'date' => date.to_s,
'layout' => 'post',
'title' => title.to_s,
'excerpt' => post[:post_excerpt].to_s,
'image' => image,
'wordpress_id' => post[:ID],
'wordpress_url' => post[:guid],
'categories' => categories,
'tags' => post_tags
}.delete_if { |k,v| v.nil? || v == '' || (v.respond_to?(:empty?) && v.empty?) }.to_yaml
File.open("#{status == 'publish' ? '_posts' : '_drafts'}/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment