Skip to content

Instantly share code, notes, and snippets.

@szsk
Created August 5, 2008 05:51
Show Gist options
  • Select an option

  • Save szsk/4033 to your computer and use it in GitHub Desktop.

Select an option

Save szsk/4033 to your computer and use it in GitHub Desktop.
sbmcomments
# 以下のスクリプトを元に作成しています
# SBM - コメントを取得 | Diaspar Journal
# http://diaspar.jp/node/145
require 'net/http'
require 'open-uri'
require 'rubygems'
require 'json/pure'
require 'digest/md5'
require 'simple-rss'
require 'mechanize'
require 'time'
require 'cgi'
Net::HTTP.version_1_2
site_url = 'http://bmky.net/'
api_url = "http://www.enjoyxstudy.com/urls/get.cgi?url=" + CGI.escape( site_url )
Save_dir = "comments/"
post_reg = %r|^http://bmky\.net/diary/log/(\d+)\.html|
def get_hatena_comments( url )
posts = []
host = 'b.hatena.ne.jp'
path = '/entry/json/' + url
body = Net::HTTP.start( host, 80 ).get( path ).body
unless body =~ /^\(null\)/
json = JSON.parse( body.sub!( %r!^\((.*)\)$! ) { $1 } )
json['bookmarks'].each do |item|
if item['comment'] != ''
post = {
:date => Time.parse( item['timestamp'][0..9].gsub( %r!/!, '-' ) ),
:user => item['user'],
:cmnt => item['comment'],
:site => "hatena"
}
posts << post
end
end
end
return posts
end
def get_delicious_comments( url )
posts = []
host = 'feeds.delicious.com'
path = '/v2/rss/url/' + Digest::MD5.hexdigest( url )
body = Net::HTTP.start( host, 80 ).get( path ).body
rss = SimpleRSS.new( body )
if rss.items != nil
rss.items.each do |item|
next unless item.description
post = {
:date => item.pubDate,
:user => item.dc_creator,
:cmnt => item.description,
:site => "del.icio.us"
}
posts << post
end
end
return posts
end
def create_comments_html( posts, id )
return if posts.empty?
comments = []
html = []
html << '<dl id="sbmcomments">'
posts.each { |item|
if item[:site] == "hatena"
icon = '<img src="http://www.hatena.ne.jp/users/Ri/' + item[:user] + '/profile_s.gif" width="16" height="16" />'
user = '<a href="http://b.hatena.ne.jp/' + item[:user] + '/' + item[:date].strftime( "%Y%m%d" ) + '">' + item[:user] + '</a>'
else
icon = '<img src="/images/icon_add_del.gif" width="16" height="16" />'
user = '<a href="http://del.icio.us/' + item[:user] + '">' + item[:user] + '</a>'
end
html << '<dt class="name">' + icon + " " + user + "</dt>"
unless comments.include?( item[:cmnt] )
html << '<dd class="desc">' + CGI.escapeHTML( item[:cmnt] ) + "</dd>"
comments << item[:cmnt]
end
}
html << "</dl>"
File.open( Save_dir + id.to_s + ".html", "w" ) do |f|
f.puts html.join( "\n" )
end
end
# Get Urls
agent = WWW::Mechanize.new
urls = agent.get_file( api_url ).gsub( /\r/, "" )
# コメントファイルを全て削除
# (Wordpressからファイルの有無で条件分岐させているため)
Dir.foreach( Save_dir ) do |file|
File.unlink( Save_dir + file ) if file =~ /\.html/
end
urls.gsub( post_reg ) do
posts = []
url = $&
id = $1
posts += get_hatena_comments( url )
posts += get_delicious_comments( url )
posts.sort! do |a,b|
a[:date] <=> b[:date]
end
posts.reverse!
create_comments_html( posts, id )
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment