-
-
Save balinterdi/261852 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
# Author: Bálint Érdi <[email protected]> | |
# forking the work of László Bácsi <[email protected]> | |
require 'rubygems' | |
require 'httparty' | |
require 'json' | |
require 'sequel' | |
class Disqus | |
include HTTParty | |
base_uri "http://disqus.com/api" | |
POST_METHODS = %w{create_post thread_by_identifier update_thread} | |
GET_METHODS = %w{get_forum_list get_forum_api_key get_thread_list | |
get_num_posts get_thread_by_url get_thread_posts} | |
class RequestUnsuccessful < RuntimeError; end | |
def initialize(api_key) | |
@api_key = api_key | |
end | |
def method_missing method, *args | |
query = args.last.is_a?(Hash) ? args.last : {} | |
unless query.has_key?(:forum_api_key) or query.has_key?('forum_api_key') | |
query.merge! :user_api_key => @api_key | |
end | |
if POST_METHODS.include?(method.to_s) | |
resp = self.class.post "/#{method}/", :body => query | |
elsif GET_METHODS.include?(method.to_s) | |
resp = self.class.get "/#{method}/", :query => query | |
else | |
return super | |
end | |
if resp['succeeded'] | |
resp['message'] | |
else | |
raise RequestUnsuccessful.new("#{resp['code']}: #{resp['message']}") | |
end | |
end | |
class Wordpress | |
SQL = <<-EOS | |
SELECT c.comment_author author, | |
c.comment_author_email email, | |
c.comment_date created_at, | |
c.comment_content body, | |
c.comment_author_url url, | |
c.comment_author_IP ip, | |
p.post_name post_slug, | |
p.post_title post_title, | |
p.post_date post_date | |
FROM wp_comments AS c | |
LEFT JOIN wp_posts AS p | |
ON c.comment_post_ID = p.ID | |
WHERE c.comment_approved=1; | |
EOS | |
class << self | |
private :new | |
def process db, disqus_api_key, forum_shortname | |
dq = Disqus.new(disqus_api_key) | |
forums = dq.get_forum_list | |
forum_id = forums.detect {|f| f['shortname'] == forum_shortname}['id'] | |
forum_key = dq.get_forum_api_key :forum_id => forum_id | |
db[SQL].each do |comment| | |
# TODO: Change this next line to match the uri pattern your blog will be using | |
post_uri = "http://codigoergosum.com/#{comment[:post_date].strftime("%Y/%m/%d")}/#{comment[:post_slug]}.html" | |
puts "---" | |
puts "POST: #{post_uri}" | |
puts comment.inspect | |
thread = dq.get_thread_by_url :forum_api_key => forum_key, :url => post_uri | |
# If a Disqus thread is not found with the current url, create a new thread and add the url. | |
if thread.nil? | |
# TODO: Change this next line to whatever title patter your blog posts will have | |
title = "codigoergosum.com : #{comment[:post_title]}" | |
# puts "creating new thread with title: #{title}" | |
thread = dq.thread_by_identifier(:forum_api_key => forum_key, | |
:identifier => title, :title => title)['thread'] | |
# Update the Disqus thread with the current post_uri | |
dq.update_thread :forum_api_key => forum_key, :thread_id => thread['id'], :url => post_uri | |
end | |
# Import comment | |
begin | |
comment_args = { | |
:message => comment[:body], | |
:author_name => comment[:author], | |
:author_email => comment[:email], | |
:created_at => comment[:created_at].utc.strftime("%Y-%m-%dT%H:%M") | |
} | |
unless comment[:url].nil? or comment[:url] == "" | |
comment_args.merge! :author_url => comment[:url] | |
end | |
unless comment[:ip].nil? or comment[:ip] == "" | |
comment_args.merge! :ip_address => comment[:ip] | |
end | |
# puts "creating comment with args: #{comment_args.inspect}" | |
comment = dq.create_post({:forum_api_key => forum_key, :thread_id => thread['id']}.merge(comment_args)) | |
rescue RequestUnsuccessful => e | |
puts "FAILED: #{e.message}" | |
else | |
puts "SUCCESS" | |
end | |
end | |
end | |
end | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
if ARGV.size >= 4 | |
api_key, forum, db_name, db_user, db_pass, db_host = ARGV | |
db = Sequel.mysql db_name, :user => db_user, :password => (db_pass || ''), :host => (db_host || "localhost") | |
Disqus::Wordpress.process(db, api_key, forum) | |
else | |
puts "Usage: #{__FILE__} api_key forum db_name db_user [db_pass] [db_host]" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment