ããããªã½ã¼ã·ã£ã«ããã¯ãã¼ã¯ãµã¼ãã¹ã®ããã¯ãã¼ã¯ä»¶æ°ãæ°å¤ã§åå¾ããRubyã®ã¯ã©ã¹æ¸ãã
SBMã¯ãã£ã±ãããã¾ããã©ãããã¯ãã¼ã¯ä»¶æ°ãåå¾ããã«ã¯ã
XMLRPCããJSONããRESTããã§ããããéããã§ã
ç°¡åã«åå¾ã§ããã¯ã©ã¹ã欲ããã¨æã£ã¦ä½ãã¾ããã
Perlã§æ¸ããã¦ãこちらãããªãåèã«ããã¦ããã£ã¦ã¾ãã
ã¨ãããããã®Rubyçã¨ããæãã«ãªãã¾ãã
ã¡ãªã¿ã«ãSBMã¯ãã¯ã¦ãªããã¯ãã¼ã¯ãlivedoorã¯ãªãããYahoo!ããã¯ãã¼ã¯ã
del.icio.usãBuzzurlãFC2ããã¯ãã¼ã¯ãPOOKMARK Airlinesã®7ã¤å©ç¨ã§ããããã«ãã¦ãã¾ãã
JSONã©ã¤ãã©ãªã®ã¤ã³ã¹ãã¼ã«
ããã°ã©ã å
ã§JSON解æã®ããã«gemããã©ã¤ãã©ãªãæã£ã¦ãã¦ãã®ã§ã
以ä¸ã®ããã«ãã¦ã¤ã³ã¹ãã¼ã«ãã¦ãã ããã
# gem install json
ããã¯ãã¼ã¯ä»¶æ°åå¾ããã°ã©ã
ããã¯ãã¼ã¯ä»¶æ°åå¾ç¨ã©ã¤ãã©ãªã®ããã°ã©ã ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
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
使ãæ¹
ä¸ã§ç¤ºããããã°ã©ã ããsbm.rbãã¨ãããã¡ã¤ã«ã§ä¿åããã¨ãã¦ãirbã§è©¦ãããã®ã以ä¸ã«ç¤ºãã¾ãã
対象ã®URLã¯é©å½ã«Googleã§è©¦ãã¦ã¾ãã
irb(main):001:0> require 'sbm.rb' irb(main):002:0> sbm = SBM.new('http://www.google.co.jp/') irb(main):003:0> pp sbm.get_all {:fc2=> {:count=>405, :entry=> "http://bookmark.fc2.com/search/detail?url=http://www.google.co.jp/"}, :livedoor=> {:count=>293, :entry=>"http://clip.livedoor.com/page/http://www.google.co.jp/"}, :pookmark=> {:count=>202, :entry=>"http://pookmark.jp/url/http://www.google.co.jp/"}, :yahoo=> {:count=>62646, :entry=>"http://bookmarks.yahoo.co.jp/url?url=http://www.google.co.jp/"}, :hatena=> {:count=>2133, :entry=>"http://b.hatena.ne.jp/entry/http://www.google.co.jp/"}, :delicious=> {:count=>947, :entry=>"http://del.icio.us/url/9d0f4061beb6ae41f64eb124665e0768"}, :buzzurl=> {:count=>62, :entry=>"http://buzzurl.jp/entry/http://www.google.co.jp/"}}
ã¨ããæãã§get_allã§ããããã®ããã¯ãã¼ã¯æ°ã¨SBMã®URLã®ããã·ã¥ãåå¾ã§ãã¾ãã
ããã¦ã
irb(main):004:0> pp sbm.hatena {:count=>2133, :entry=>"http://b.hatena.ne.jp/entry/http://www.google.co.jp/"} irb(main):005:0> pp sbm.delicious {:count=>947, :entry=>"http://del.icio.us/url/9d0f4061beb6ae41f64eb124665e0768"}
ã¨ããæãã§ãã¯ã¦ããdeliciouãªã©ã«ãåå¥ã«ã¢ã¯ã»ã¹ã§ãã¾ãã
ãã¨ãã¡ãªã¿ã«ãresultã¡ã½ãããå®è¡ããã¨
irb(main):006:0> pp sbm.result FC2ããã¯ãã¼ã¯ count:405 Entry:http://bookmark.fc2.com/search/detail?url=http://www.google.co.jp/ livedoorã¯ãªãã count:293 Entry:http://clip.livedoor.com/page/http://www.google.co.jp/ POOKMARK Airlines count:202 Entry:http://pookmark.jp/url/http://www.google.co.jp/ Yahoo!ããã¯ãã¼ã¯ count:62646 Entry:http://bookmarks.yahoo.co.jp/url?url=http://www.google.co.jp/ ã¯ã¦ãªããã¯ãã¼ã¯ count:2133 Entry:http://b.hatena.ne.jp/entry/http://www.google.co.jp/ del.icio.us count:947 Entry:http://del.icio.us/url/9d0f4061beb6ae41f64eb124665e0768 Buzzurl count:62 Entry:http://buzzurl.jp/entry/http://www.google.co.jp/
ã¨ããæãã§ä¸è¦§ãåºåããããã«ãã¦ãã¾ãã
以ä¸
ãã使ãããæ¹ãããã°ãgithubã«ç»é²ãã¦ããã§èªç±ã«ä½¿ã£ã¦ããã£ã¦ãã¾ãã¾ããã
gist:27407 · GitHub
ãã¨ãã©ããããããã¨ããã¨ãããã°æãã¦ãããã¨ãããããã§ãã