-
-
Save pstinnett/266251 to your computer and use it in GitHub Desktop.
ExpressionEngine comments to Disqus ruby script
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
# 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 = '' # Find this by logging into Disqus and visiting: http://disqus.com/api/get_my_key/ | |
forum_shortname = '' # The username used in your community page URL: http://[shortname].disqus.com/ | |
current_blog_base_url = '' # The base URL for your blog details page, without the trailing slash: http://www.myurl.com/blog/details | |
db = '' # The name of your ExpressionEngine database from which you want to export comments | |
db_user = '' # The username of your ExpressionEngine database from which you want to export comments | |
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"] | |
# Below is where the database connection is made. Change the :host to the address of your ExpressionEngine MySQL database if you would like to use the script remotely. Change the :password to the password of your database user | |
db = Sequel.mysql(db, :user => db_user, :host => 'localhost', :password => 'l3ftc1rcle') | |
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