Skip to content

Instantly share code, notes, and snippets.

@newbamboo
Created February 16, 2009 21:27
Show Gist options
  • Save newbamboo/65380 to your computer and use it in GitHub Desktop.
Save newbamboo/65380 to your computer and use it in GitHub Desktop.
# 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'
user_api_key = ''
forum_shortname = 'myblog'
current_blog_base_url = 'http://myblog'
db = 'mephisto_production'
db_user = 'root'
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')
query = "SELECT comments.body, comments.author, comments.author_email, comments.created_at comment_created_at, articles.title, articles.permalink, articles.published_at article_published_at FROM contents AS comments LEFT JOIN contents AS articles ON comments.article_id = articles.id WHERE comments.type = 'Comment' AND comments.approved = 1 AND articles.published_at IS NOT NULL ORDER BY comments.created_at DESC"
db[query].each do |comment|
article_url = "#{current_blog_base_url}/#{comment[:article_published_at].year}/#{comment[:article_published_at].month}/#{comment[:article_published_at].day}/#{comment[:permalink]}"
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
if resource['/create_post'].post(:forum_api_key => forum_api_key, :thread_id => thread["id"], :message => comment[:body], :author_name => comment[:author], :author_email => comment[:author_email], :created_at => comment[:comment_created_at].strftime("%Y-%m-%dT%H:%M"))
puts "Success: #{comment[:author]} on #{comment[:title]}"
else
puts "FAIL: #{comment[:author]} on #{comment[:title]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment