Skip to content

Instantly share code, notes, and snippets.

@jasonrudolph
Forked from vitaly/disqus_import.rb
Created April 2, 2010 20:13
Show Gist options
  • Save jasonrudolph/353645 to your computer and use it in GitHub Desktop.
Save jasonrudolph/353645 to your computer and use it in GitHub Desktop.
#vim: ruby
#
# This script will import all your Mephisto comments into Disqus (http:/disqus.com)
# parts of it came from http://www.locomotivation.com/blog/2008/12/01/disqus-sinatra-importer.html
# but instead of a blog-engine independent sinatra app we needed a mephisto-specific script that
# will just do the job :)
unless defined?(RAILS_ROOT)
puts "This is a Rails 'runner' script. Please run './script/runner path/to/disqus_import' inside your Mephisto directory"
exit
end
require 'rest_client'
require 'json'
require 'ruby-debug'
require 'pp'
############### EDIT THOSE CONSTANTS BEFORE YOUR RUN THIS SCRIPT, THEN REMOVE THE 'exit' line ############
BLOG_URL = "http://blog.astrails.com"
DISQUS_URL = 'http://disqus.com/api'
USER_API_KEY = 'XmBa46cbI23JzAQlC7lGNn0UIBnw1IF6bXMFeN8xZepVk7VjB88lbfPrZpF3cuGe'
FORUM_SHORTNAME = 'astrails'
##########################################################################################################
def find_or_create_thread(resource, article)
article_url = BLOG_URL + article.full_permalink
unless thread = JSON.parse(resource['/get_thread_by_url?forum_api_key='+FORUM_API_KEY+'&url='+article_url].get.body)["message"]
response = resource['/thread_by_identifier/'].post(:api_version => '1.1', :forum_api_key => FORUM_API_KEY, :identifier => article.id, :title => article.title)
thread = JSON.parse(response.body)["message"]["thread"]
end
resource['/update_thread/'].post(:api_version => '1.1', :forum_api_key => FORUM_API_KEY, :thread_id => thread["id"], :url => article_url)
thread["id"]
end
def import_comment(resource, comment)
article = comment.article
thread_id = find_or_create_thread(resource, article)
ep = resource['/create_post/']
email = comment.author_email
# disqus requires SOME email :)
email = "unknown@example.com" if email.blank?
args = {
:forum_api_key => FORUM_API_KEY,
:thread_id => thread_id,
:message => comment.body,
:author_name => comment.author,
:author_email => email,
:author_url => comment.author_url,
:created_at => comment.created_at.strftime("%Y-%m-%dT%H:%M"),
# DISQUS crashed with error 500 when we passed IPs. no idea why...
#:ip_address => comment.author_ip
}
puts pp(args)
res = ep.post(args)
true
rescue => e
puts e
puts e.backtrace[0..10]
false
end
def import_all(resource)
successful_imports = []
failed_imports = []
Article.find(:all, :conditions => 'published_at is not null').each do |article|
article.comments.each do |comment|
if import_comment(resource, comment)
successful_imports << comment
else
failed_imports << comment
end
end
end
end
resource = RestClient::Resource.new DISQUS_URL
forums = JSON.parse(resource['/get_forum_list?user_api_key='+USER_API_KEY].get.body)
if forums["message"].empty?
puts "No forums found!"
exit
end
forum_id = forums["message"].select {|forum| forum["shortname"]==FORUM_SHORTNAME}[0]["id"]
FORUM_API_KEY = JSON.parse(resource['/get_forum_api_key?user_api_key='+USER_API_KEY+'&forum_id='+forum_id].get.body)["message"]
import_all(resource)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment