Created
March 13, 2013 20:56
-
-
Save nclsjstnn/5156091 to your computer and use it in GitHub Desktop.
EXPERIMENTAL WAY TO IMPORT A WORDPRESS BLOG INTO A SIMPLE RAIL SCAFFOLD
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
def import | |
require 'xmlrpc/client' | |
if params[:number_posts] | |
number_posts=params[:number_posts].to_i | |
loops=number_posts / 100 | |
(1..loops.to_i).each do |i| | |
offset=100*i | |
connection = XMLRPC::Client.new2('http://www.nostalgic.cl/xmlrpc.php') | |
result = connection.call('wp.getPosts', 1, 'MEGO', 'mego123', {:number => 100, :offset => offset}) | |
#imported_posts = result.length | |
result.each do |post| | |
unless Post.exists?(:wp_id=>post["post_id"]) | |
date = Time.new(post["post_date"].year, post["post_date"].month, post["post_date"].day, post["post_date"].hour, post["post_date"].min, post["post_date"].sec) | |
if post["post_thumbnail"].present? | |
thumb=post["post_thumbnail"]["metadata"]["file"] | |
end | |
link=post["link"].gsub("http://www.nostalgic.cl/blog", "") | |
content=post["post_content"].gsub("POR TEAMNOSTALGIC ", "") | |
post = Post.new( | |
:title=>post["post_title"], | |
:content=>content, | |
:link=>link, | |
:wp_id=>post["post_id"], | |
:created_at=>date, | |
:category_list=>post["terms"].map { |cat| cat["name"] }.join(", "), | |
:thumb=>thumb | |
) | |
puts "[NEW POST] "+post.inspect if post.save | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment