Skip to content

Instantly share code, notes, and snippets.

@shapeshed
Created October 6, 2009 06:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shapeshed/202802 to your computer and use it in GitHub Desktop.
Save shapeshed/202802 to your computer and use it in GitHub Desktop.
Import ExpressionEngine comments into Disqus
# Copyright 2009 Michael Ivey, released to public domain
# Disqus guts lifted from http://github.com/squeejee/disqus-sinatra-importer/tree/master
# I wanted it to run from MySQL and command line, instead of a Sinatra app
require 'rubygems'
require 'rest_client'
require 'json'
require 'sequel'
disqus_url = 'http://disqus.com/api'
# Set these as per your site
user_api_key = ''
forum_shortname = ''
current_blog_base_url = ''
db = ''
db_user = ''
resource = RestClient::Resource.new disqus_url
forums = JSON.parse(resource['/get_forum_list?user_api_key='+user_api_key].get)
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"]
db = Sequel.mysql(db, :user => db_user, :host => 'localhost', :password => 'yourpass')
query = "SELECT comments.comment_id, comments.comment, comments.name, comments.email, comments.comment_date, titles.title, titles.url_title, titles.entry_date FROM exp_comments AS comments LEFT JOIN exp_weblog_titles AS titles ON comments.entry_id = titles.entry_id WHERE comments.status = 'o' AND titles.status = 'open'"
db[query].each do |comment|
article_url = "#{current_blog_base_url}/#{comment[:url_title]}/"
thread = JSON.parse(resource['/get_thread_by_url/?forum_api_key='+forum_api_key+'&url='+article_url].get)["message"]
# If a Disqus thread is not found with the current url, create a new thread and add the url.
if thread.nil?
thread = JSON.parse(resource['/thread_by_identifier/'].post(:forum_api_key => forum_api_key, :identifier => comment[:title], :title => comment[:title]))["message"]["thread"]
# Update the Disqus thread with the current article url
resource['/update_thread/'].post(:forum_api_key => forum_api_key, :thread_id => thread["id"], :url => article_url)
end
# # Import posts here
t = Time.at(comment[:comment_date])
if resource['/create_post/'].post(:forum_api_key => forum_api_key, :thread_id => thread["id"], :message => comment[:comment], :author_name => comment[:name], :author_email => comment[:email], :created_at => t.strftime("%Y-%m-%dT%H:%M"))
puts "Success: #{comment[:name]} on #{comment[:title]}"
else
puts "FAIL: #{comment[:name]} on #{comment[:title]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment