æå®ã¨ã³ããªã®ã¯ã¦ãªã¹ã¿ã¼æ°ãä¸æ¬åå¾ãã
æå®ã¨ã³ããªã®ã¯ã¦ãªã¹ã¿ã¼æ°ãä¸æ¬åå¾ããã¯ã©ã¹ãæ¸ãã¾ããã
require 'rubygems' require 'httpclient' require "json/lexer" require 'uri' module HatenaStar BASE_URI = "http://s.hatena.ne.jp/" # javascriptã®encodeURIComponentã®æåã«ãããã¦#ã追å ã UNSAFE = /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]#]/n class Service def initialize( proxy=nil ) @client = HTTPClient.new( proxy, "Client") end def entries ( uris ) url = "#{BASE_URI}entries.json" body = uris.inject("") {|buf, uri| buf << "uri=" << URI.encode( uri, UNSAFE ) << "&" } result = @client.post( url, body ) JSON::Lexer.new(result.content).nextvalue end end end
使ãæ¹ã以前に設置したサンプルページã®ã¯ã¦ãªã¹ã¿ã¼æ°ãåå¾ãã¾ãã
s = HatenaStar::Service.new # s = HatenaStar::Service.new http://proxyhost:port # ãããã·ãå¿ è¦ãªãã # ã¨ã³ããªãåå¾ entries = s.entries([ "http://unageanu.sakura.ne.jp/blog-samples/080204/#star_1", "http://unageanu.sakura.ne.jp/blog-samples/080204/#star_2", "http://unageanu.sakura.ne.jp/blog-samples/080204/#star_3" ]) # URIã¨â ã®æ°ã表示 # ã¿ã¤ãã«ã¯ã¨ããªãã entries["entries"].each { |e| # entries.jsonã使ãå ´åãä¸å·¥å¤«å¿ è¦ã if ( e["stars"] != nil && e["stars"].length > 1 \ && e["stars"][1].kind_of?(Numeric) ) count = e["stars"][1] + 2 else count = e["stars"].length end puts e["uri"] + " : " + count.to_s }
å®è¡çµæã§ãã
http://unageanu.sakura.ne.jp/blog-samples/080204/#star_1 : 19 http://unageanu.sakura.ne.jp/blog-samples/080204/#star_2 : 6 http://unageanu.sakura.ne.jp/blog-samples/080204/#star_3 : 20
- HatenaStar.jsã§ã¯GETã§ã¢ã¯ã»ã¹ãã¦ãããã©(JSONPã®ããããª?)ãPOSTãåãä»ãã¦ãããã
- ã¬ã¹ãã³ã¹ã®å½¢å¼ãã¡ãã£ã¨å¤ãªã®ã§ãâ æ°ãæ°ããå ´åãä¸å·¥å¤«å¿ è¦ã
- "http://s.hatena.ne.jp/entries.json"ã§ã¯ãªã"http://s.hatena.ne.jp/entry.json"ã¨ããã®ããã£ã¦ã
- "http://s.hatena.ne.jp/entry.json"ã ã¨ã/entries/starsãã®ä¸ã«ãã¹ã¦ã®â æ å ±ã並ã¶ãâ æ°ã¯ã/entries/starsãã®é åã®é·ããåãã°OK
- "http://s.hatena.ne.jp/entries.json"ã ã¨ã/entries/starsãã¯ã<æåã®â æ å ±>,<ç·â æ°-2>,<æå¾ã®â æ å ±>ãã®é åã«ãªããâ æ°ã¯ã/entries/starsãã®é åã®2çªç®+2ã¨ãªãããã ããâ æ°ãå°ãªãå ´åã"http://s.hatena.ne.jp/entry.json"ã¨åãåä½ã«ãªãã®ã§æ³¨æã
- "http://s.hatena.ne.jp/entry.json"ã ã¨â æ°ãå¤ãå ´åã«ãã¬ã¹ãã³ã¹ãé·ããªãâãã¼ã¿è»¢éã«æéãããã&&JSONã®è§£æã«æéãããããæ°ãããã®ã§ãæ°ã欲ããã ããªã"http://s.hatena.ne.jp/entries.json"ã使ãæ¹ããããã
URIã¯å¼æ°ã§æå®ããªãã¨ãããªãã¿ãããä»»æã®ãµã¤ã以ä¸ã®ã¨ã³ããªå¥â æ°ä¸è¦§ãåå¾ã¨ãã§ããªãã®ããª?
ä¾åã¢ã¸ã¥ã¼ã«ã«ã¤ãã¦
以ä¸ã®ä¾åã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¦ãã¾ããã©ã¡ããgemçµç±ã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
gem install ruby-json gem install httpclient --source http://dev.ctor.org/download/