Created
May 20, 2009 23:06
-
-
Save vitaly/115146 to your computer and use it in GitHub Desktop.
simple script/runner script to import mephisto comments into disqus
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
#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)["message"] | |
thread = JSON.parse(resource['/thread_by_identifier'].post(:forum_api_key => FORUM_API_KEY, :identifier => article.id, :title => article.title))["message"]["thread"] | |
end | |
resource['/update_thread'].post(: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 = "[email protected]" 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) | |
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)["message"] | |
import_all(resource) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!
I had to make a few tweaks for the latest rest-client gem and the latest disqus API. You can see those tweaks on this fork: http://gist.github.com/353645