newbamboo (owner)

Revisions

gist: 65380 Download_button fork
public
Public Clone URL: git://gist.github.com/65380.git
mephisto_comments_to_disqus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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