Created
August 16, 2010 22:46
-
-
Save ecerulm/527898 to your computer and use it in GitHub Desktop.
fix urls and title in Disqus (after developer=1 misuse)
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 | |
# After setting disqus_developer = 1 http://disqus.com/docs/embed/ | |
# I got a lot of threads "corrupted". | |
# 1. Some of them had the wrong url, http://localhost:4000/xxxxxx. | |
# 2. Some of them had title == url. | |
# This script fixes both | |
# Replace http://localhost:4000 with http://rubenlaguna.com | |
# Disqus ruby api http://disqus.rubyforge.org/. Relays on my own fix to | |
# disqus ruby http://github.com/ecerulm/disqus/commit/91d5810c5fc6676f828a4fe17768e0e04b526bbe | |
# gem install disqus | |
require 'rubygems' | |
require 'disqus' | |
require 'disqus/api' | |
require 'yaml' | |
require 'curb' | |
require 'hpricot' | |
file = File.open("API-KEY", "rb") | |
apikey = file.read | |
p "Using API-KEY: #{apikey}" | |
Disqus::defaults[:api_key] = apikey | |
forum2 = Disqus::Forum.list.first | |
fak = forum2.key | |
threads = Disqus::Thread.list(forum2,:forum_api_key => fak,:limit => 300) | |
puts "Total number of threads: #{threads.size}" | |
wrongurl = threads.select { |t| t.url =~ /^http:\/\/localhost:4000/ } | |
puts "Number of threads with wrong url: #{wrongurl.size}" | |
wrongurl.each do |t| | |
new_url = t.url.gsub(/http:\/\/localhost:4000/,"http://rubenlaguna.com") | |
result = Disqus::Api.update_thread( | |
:forum_api_key => fak, | |
:thread_id => t.id, | |
:url => new_url) | |
puts "updated post with new url: #{new_url}" | |
p result | |
end | |
# Get threads again to be sure we have the latest changes | |
threads = Disqus::Thread.list(forum2,:forum_api_key => fak,:limit => 300) | |
tnotitle = threads.select { |t| t.url.nil? } | |
puts "Number of threads without url (never visited): #{tnotitle.size} " | |
tnotitle.each do | t| | |
puts ">>>>> no title post >>>>>" | |
p t | |
puts "<<<<< no title post <<<<<" | |
end | |
puts "Searching for posts with wrong title..." | |
threads.each do |t| | |
next unless t.url | |
c = Curl::Easy.perform(t.url) # Download the page | |
real_title = Hpricot(c.body_str).at("title").inner_html # Get html title | |
next if t.title == real_title | |
puts ">>>>>>" | |
puts "Fixing thread with url #{t.url} and title #{t.title}" | |
puts "real title: #{real_title}" | |
result = Disqus::Api.update_thread( | |
:forum_api_key => fak, | |
:thread_id => t.id, | |
:title => real_title) | |
p result | |
puts "<<<<<<" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment