This file contains 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
require 'open-uri' | |
require 'xmlrpc/client' | |
require 'rexml/document' | |
require 'digest/md5' | |
require 'rubygems' | |
require 'json' | |
class SBM | |
# 初期設定 | |
@@sbms = { | |
:hatena => { | |
:name => 'はてなブックマーク', | |
:proxy => 'http://b.hatena.ne.jp/xmlrpc', | |
:entry => 'http://b.hatena.ne.jp/entry/', | |
:method => 'bookmark.getCount', | |
}, | |
:livedoor => { | |
:name => 'livedoorクリップ', | |
:proxy => 'http://rpc.clip.livedoor.com/count', | |
:entry => 'http://clip.livedoor.com/page/', | |
:method => 'clip.getCount', | |
}, | |
:yahoo => { | |
:name => 'Yahoo!ブックマーク', | |
:proxy => 'http://num.bookmarks.yahoo.co.jp/yjnostb.php?urls=', | |
:xpath => '//SAVE_COUNT/@ct', | |
:entry => 'http://bookmarks.yahoo.co.jp/url?url=', | |
}, | |
:delicious => { | |
:name => 'del.icio.us', | |
:proxy => 'http://badges.del.icio.us/feeds/json/url/data?url=', | |
:entry => 'http://del.icio.us/url/', | |
:key => 'total_posts', | |
}, | |
:buzzurl => { | |
:name => 'Buzzurl', | |
:proxy => 'http://api.buzzurl.jp/api/counter/v1/json?url=', | |
:entry => 'http://buzzurl.jp/entry/', | |
:key => 'users', | |
}, | |
:fc2 => { | |
:name => 'FC2ブックマーク', | |
:proxy => 'http://bookmark.fc2.com/image/users/', | |
:regexp => /(\d+)\.png$/, | |
:entry => 'http://bookmark.fc2.com/search/detail?url=', | |
}, | |
:pookmark => { | |
:name => 'POOKMARK Airlines', | |
:proxy => 'http://pookmark.jp/count/', | |
:regexp => /(\d+)$/, | |
:entry => 'http://pookmark.jp/url/', | |
} | |
} | |
attr_accessor :url | |
def initialize(url) | |
@url = url | |
end | |
# 一覧の出力 | |
def result | |
self.get_all.each do |sbm, val| | |
puts @@sbms[sbm][:name] | |
puts "\t count:" + val[:count].to_s + "\t Entry:" + val[:entry] | |
end | |
end | |
# すべてのSBMからブックマーク件数とSBMのURL取得 | |
def get_all | |
sbm_counts = {} | |
i = 0 | |
thread = [] | |
@@sbms.each do |sbm, etc| | |
thread[i] = Thread.start do | |
sbm_counts[sbm] = self.get(sbm) | |
end | |
i += 1 | |
end | |
thread.each{|t| t.join} | |
return sbm_counts | |
end | |
# 指定したSBMからブックマーク件数 | |
def get(sbm) | |
case sbm | |
when :hatena | |
self.hatena | |
when :livedoor | |
self.livedoor | |
when :yahoo | |
self.yahoo | |
when :delicious | |
self.delicious | |
when :buzzurl | |
self.buzzurl | |
when :fc2 | |
self.fc2 | |
when :pookmark | |
self.pookmark | |
else | |
puts "Sorry, #{sbm} is not support." | |
end | |
end | |
# SBMそれぞれ出力 | |
def hatena | |
{ :count => get_sbm_xmlrpc(:hatena), | |
:entry => get_sbm_entry(:hatena) } | |
end | |
def livedoor | |
{ :count => get_sbm_xmlrpc(:livedoor), | |
:entry => get_sbm_entry(:livedoor) } | |
end | |
def yahoo | |
{ :count => get_sbm_rest(:yahoo), | |
:entry => get_sbm_entry(:yahoo) } | |
end | |
def delicious | |
{ :count => get_sbm_json(:delicious), | |
:entry => get_sbm_entry(:delicious) } | |
end | |
def buzzurl | |
{ :count => get_sbm_json(:buzzurl), | |
:entry => get_sbm_entry(:buzzurl )} | |
end | |
def fc2 | |
{ :count => get_sbm_imageicon(:fc2), | |
:entry => get_sbm_entry(:fc2) } | |
end | |
def pookmark | |
{ :count => get_sbm_imageicon(:pookmark), | |
:entry => get_sbm_entry(:pookmark) } | |
end | |
private | |
# XMLRPCによるブックマーク件数取得(hatena,livedoor) | |
def get_sbm_xmlrpc(sbm) | |
count = 0 | |
client = XMLRPC::Client.new2(@@sbms[sbm][:proxy]) | |
res = client.call2(@@sbms[sbm][:method], @url) | |
res[1].each{|url, value| count = value } if res[0] | |
return count | |
end | |
# REST(XML)によるブックマーク件数取得(Yahoo) | |
def get_sbm_rest(sbm) | |
count = 0 | |
open(@@sbms[sbm][:proxy] + @url) do |xml| | |
doc = REXML::Document.new(xml.read) | |
count = REXML::XPath.first(doc, @@sbms[sbm][:xpath]).value.to_i | |
end | |
return count | |
end | |
# JSONによるブックマーク件数取得(delicious, buzzurl) | |
def get_sbm_json(sbm) | |
count = 0 | |
open(@@sbms[sbm][:proxy] + @url) do |json| | |
data = JSON.parse(json.read) | |
if data[0] != nil | |
count = data[0][@@sbms[sbm][:key]].to_i | |
end | |
end | |
return count | |
end | |
# 画像のURLからブックマーク件数取得(fc2, pookmark) | |
def get_sbm_imageicon(sbm) | |
count = 0 | |
open(@@sbms[sbm][:proxy] + @url) do |image| | |
path = image.base_uri.path | |
if path =~ @@sbms[sbm][:regexp] | |
count = $1.to_i | |
end | |
end | |
return count | |
end | |
# 入力されたURLに対応したSBMのURL表示 | |
def get_sbm_entry(sbm) | |
url = @url | |
url = Digest::MD5.hexdigest(@url) if sbm == :delicious | |
return @@sbms[sbm][:entry] + url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment