Skip to content

Instantly share code, notes, and snippets.

@trevorturk
Forked from akitaonrails/load_discus.rake
Created April 22, 2010 14:56
Show Gist options
  • Save trevorturk/375330 to your computer and use it in GitHub Desktop.
Save trevorturk/375330 to your computer and use it in GitHub Desktop.
require 'disqus'
require 'disqus/api'
require 'disqus/author'
require 'disqus/forum'
require 'disqus/post'
require 'disqus/thread'
require 'disqus/version'
desc "Load Blog comments to Disqus"
task :load_disqus => :environment do
Disqus::defaults[:account] = 'akitaonrails'
Disqus::defaults[:api_key] = '...'
site_url = Enki::Config.default[:url]
forum = Disqus::Forum.list.first
Post.find_in_batches(:batch_size => 100, :include => :comments) do |batch|
batch.each do |post|
# try to retrieve the thread
permalink = site_url + post.permalink
thread = Disqus::Api.get_thread_by_url(
:forum_api_key => forum.key,
:url => permalink)
thread_id = if thread["message"].nil?
# not found, so create a new one
title = "#{post.title} - #{Enki::Config.default[:title]}"
thread = Disqus::Api.thread_by_identifier(
:forum_api_key => forum.key,
:identifier => title,
:title => title)
# some info needs to be updated after creation
begin
Disqus::Api.update_thread(
:forum_api_key => forum.key,
:thread_id => thread["message"]["thread"]['id'],
:url => permalink,
:slug => post.slug)
rescue => e
puts permalink
puts e
end
thread["message"]["thread"]["id"]
else
thread["message"]["id"]
end
next unless thread["succeeded"]
post.comments.each do |comment|
next if comment.akismet == 'spam' # skip marked spam
opts = {
:forum_api_key => forum.key,
:thread_id => thread_id,
:author_name => comment.author,
:message => comment.body,
:created_at => comment.created_at.utc.strftime("%Y-%m-%dT%H:%M")
}
opts[:author_email] = comment.author_email unless comment.author_email.empty?
opts[:author_url] = comment.author_url unless comment.author_url.empty?
opts[:ip_address] = comment.user_ip if comment.user_ip
begin
new_comment = Disqus::Api.create_post(opts)
rescue => e
puts "\n#{e.inspect}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment