Bing search

打算写一个programable web系列....
http://gist.github.com/137237

require'open-uri'
require'cgi'
module Bing
 #doc:http://msdn.microsoft.com/zh-cn/library/dd251020(en-us).aspx
 APP_ID = '0DDB7DBE7BABC4505C688F4E954FBBB4458403D8'
 API_HOST ='http://api.bing.net/json.aspx'
 def search query, option = {}
   base_option ={'AppId' => APP_ID, 'Market' => 'zh-cn', 'Sources' =>'web','Version' => '2.0', 'Web.Count'=>'10'}
   option['Query'] = query
   option = base_option.merge! option
   url = API_HOST << '?' << option.map{|key,value| "#{key}=#{CGI.escape(value)}"}.join("&")
   open(url).read   
 end
end
include Bing
#搜网页
puts(search "ruby", "sources" => "web")
#搜图片
puts(search "ruby", "sources" => "image")
#搜视频
puts(search "ruby", "sources" => "video")

你可能感兴趣的:(json,Web,cgi,Ruby,bing)