Skip to content

Instantly share code, notes, and snippets.

@ramieblatt
Created April 14, 2011 20:26
Show Gist options
  • Save ramieblatt/920424 to your computer and use it in GitHub Desktop.
Save ramieblatt/920424 to your computer and use it in GitHub Desktop.
An importer of blogger xml to Ryan Stout's Blog Kit (https://github.com/ryanstout/blog_kit)
# run like so: (also see: http://blog.jayfields.com/2006/10/rake-tasks-with-parameters.html for tutorial on rake tasks, arguments etc.)
#
# with defaults:
# $ rake import_blogger_xml["db/blog_seed/blog-04-08-2011.xml"] # e.g. "db/blog_seed/blog-04-08-2011.xml
desc "Import Blogger xml into Blog Kit tables"
task :import_blogger_xml, :file_path, :needs => :environment do |t, args|
blog_kit_comment_array = []
BlogKit.instance.settings['akismet_key'] = nil # this is to turn off spam filtering because of throttling
# This will be the default blog poster user, in our case Party is our user model and Employee inherits from Party...
victoria_hale = Employee.find_by_full_name("Victoria Hale")
blogger_xml_file = args[:file_path]
blogger_xml = File.open(blogger_xml_file)
blogger_hash = Hash.from_xml(blogger_xml)
blogger_entry_array = blogger_hash['feed']['entry']
blogger_entry_array.each do |blogger_entry|
tags = []
if blogger_entry['category'].class == Hash
entry_kind = blogger_entry['category']['term'].split('kind#')[1]
else
entry_kind = blogger_entry['category'].first['term'].split('kind#')[1]
tags = blogger_entry['category'].find_all{|c| !c['scheme'].index("ns#").nil?}.map{|c| c['term']}
end
puts "Processing blogger entry, entry_kind: #{entry_kind}"
if entry_kind == 'post'
blog_kit_post = {}
blog_kit_post[:title] = blogger_entry["title"]
blog_kit_post[:body] = blogger_entry["content"]
blog_kit_post[:published_at] = DateTime.parse(blogger_entry["published"])
blog_kit_post[:created_at] = blog_kit_post[:published_at]
blog_kit_post[:updated_at] = DateTime.parse(blogger_entry["updated"])
blog_kit_post[:published] = true
# We try to link blog poster to our native user, in our case Employee, find from author name or login account from login/email
emp1 = Employee.find_by_full_name(blogger_entry["author"]["name"])
emp2 = Account.find_by_login(blogger_entry["email"].split("@")[0]).party rescue nil
blog_kit_post[:user_id] = (emp1 || emp2 || victoria_hale).id
# avoid creating multiple records for the same blogger post, so use find_or_initialize_by_blogger_id
blog_post = BlogPost.find_or_initialize_by_blogger_id(blogger_entry["id"].split("post-")[1])
ok = blog_post.update_attributes(blog_kit_post)
if ok
puts "Successfully created or updated blog post, ID: #{blog_post.id}, title: #{blog_post.title}"
else
puts "Failed to create or update blog post, BLOGGER ID: #{blog_post.blogger_id}, title: #{blog_post.title}, errors: #{blog_post.errors.full_messages}"
end
tags.each do |tag|
# avoid creating multiple records for the same blogger tag, so use find_or_create_by__blog_post_id_and_tag
ok = BlogTag.find_or_create_by_blog_post_id_and_tag(blog_post.id, tag)
if ok
puts "Successfully created or updated blog tag, blog post ID: #{blog_post.id}, tag: #{tag}"
else
puts "Failed to create or update blog tag, blog post ID: #{blog_post.id}, tag: #{tag}"
end
end
elsif entry_kind == 'comment'
blog_kit_comment = {}
blog_kit_comment[:blogger_id] = blogger_entry["id"].split("post-")[1]
blog_kit_comment[:body] = blogger_entry["content"]
blog_kit_comment[:created_at] = DateTime.parse(blogger_entry["published"])
blog_kit_comment[:user_id] = nil
blog_kit_comment[:user_ip] = nil
blog_kit_comment[:user_agent] = nil
blog_kit_comment[:referrer] = nil
blog_kit_comment[:name] = blogger_entry["author"]["name"]
blog_kit_comment[:site_url] = blogger_entry["author"]["uri"]
blog_kit_comment[:email] = blogger_entry["author"]["email"]
blog_kit_comment[:blog_post_blogger_id] = blogger_entry["in_reply_to"]["ref"].split("post-")[1]
blog_kit_comment_array << blog_kit_comment
end
end
blog_kit_comment_array.each do |comment|
comment[:blog_post_id] = BlogPost.find_by_blogger_id(comment.delete(:blog_post_blogger_id)).id
# avoid creating multiple records for the same blogger comment, so use find_or_initialize_by_blogger_id
blog_comment = BlogComment.find_or_initialize_by_blogger_id(comment.delete(:blogger_id))
ok = blog_comment.update_attributes(comment)
if ok
puts "Successfully created or updated blog comment, ID: #{blog_comment.id}, body: #{blog_comment.body[0..40]}..."
else
puts "Failed to create or update blog comment, BLOGGER ID: #{blog_comment.blogger_id}, body: #{blog_comment.body[0..40]}..., errors: #{blog_comment.errors.full_messages}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment