source code

	$KCODE = "u"
	require 'net/http'
	require 'uri'
	require 'rss'
	require 'xmlrpc/client'
	
	# ---------------------------------------
	# --- define
	# ---------------------------------------
	HATENA_XMLRPC_SERVER = 'd.hatena.ne.jp'
	HATENA_XMLRPC_ADDRESS = '/xmlrpc'
	HATENA_KEYWORD_URL = 'search.hatena.ne.jp'
	
	
	# ---------------------------------------
	# --- class
	# ---------------------------------------
	class HatenaKeywordCheck
	
	  def initialize( word )
	    @check_word = word
	    @cache = false
	    @word_items = []
	  end
	
	  def build_query(query_hash)
	    query_hash.map do |key,value|
	      "#{URI.encode(key)}=#{URI.encode(value)}"
	    end.join("&")
	  end
	
	  def request_keyword( keyword )
	    result = nil
	    Net::HTTP.version_1_2
	    Net::HTTP.start(HATENA_KEYWORD_URL, 80) do |http|
	      response = http.get('/keyword?' + 
	                          build_query("word" => keyword,
	                                      "mode" => "rss",
	                                      "ie" => "utf8",
	                                      "page" => "1"))
	      #puts response.body
	      result = RSS::Parser.parse(response.body, false)
	    end
	    @cache = true
	    @word_items = result.items
	  end
	
	  # --- class method
	  def arrival?
	    request_keyword(@check_word) if @cache == false
	    # return
	    @word_items.size > 0
	  end
	
	  def size
	    request_keyword(@check_word) if @cache == false
	    # return
	    @word_items.size
	  end
	
	  def get
	    request_keyword(@check_word) if @cache == false
	    # return
	    @word_items
	  end
	
	  def get_similar
	    similer_list = []
	    server = XMLRPC::Client.new(HATENA_XMLRPC_SERVER, HATENA_XMLRPC_ADDRESS)
	    result = server.call("hatena.getSimilarWord", {
	                           "wordlist" => @check_word
	                         })
	    result['wordlist'].map do |v|
	      similer_list << v['word']
	    end
	    # return
	    similer_list
	  end
	
	end

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-11-06 (月) 01:22:22 (759d)