Skip to content

Instantly share code, notes, and snippets.

@jonmagic
Created February 21, 2009 21:42
Show Gist options
  • Save jonmagic/68198 to your computer and use it in GitHub Desktop.
Save jonmagic/68198 to your computer and use it in GitHub Desktop.
require 'feed_tools'
require 'rest_client'
require 'json'
# Fill these in. See disqus-sinatra-importer for details on what they do
# if they're not obvious
user_api_key = 'disqus_api_key_goes_here'
forum_shortname = 'your_disqus_shortname'
current_blog_rss = 'url_to_your_site_feed'
disqus_url = 'http://disqus.com/api'
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"]
# Get all of the articles from the current blog site
articles = FeedTools::Feed.open(current_blog_rss)
Comment.find(:all).each do |comment|
comment_article_title = comment.title
# Get the blog article for the current comment thread
article = articles.items.select {|a| a.title.downcase == comment_article_title.downcase}[0]
if article
article_url = article.link
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[:created_at].strftime("%Y-%m-%dT%H:%M"))
puts "Success: #{comment.author} on #{comment.title}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment