plugin/search-bing.rb in tdiary-contrib-4.0.2.1 vs plugin/search-bing.rb in tdiary-contrib-4.0.3
- old
+ new
@@ -7,10 +7,11 @@
#
# Needed these options below:
#
# @options['search-bing.appid'] : Your Bing AppId
# @options['search.result_filter'] : your dialy's URL format of DAY mode into Regexp.
+# @options['search-bing.base'] : Base URI of your diary (for debugging)
#
require 'timeout'
require 'json'
require 'open-uri'
@@ -30,15 +31,20 @@
end
def search_bing_api( q, start = 0 )
appid = @conf['search-bing.appid']
- u = 'https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web'
+ u = 'https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web'
u << "?Query=%27#{q}%27&Options=%27EnableHighlighting%27&$top=50&$skip=#{start}&$format=Json"
uri = URI( u )
- open( uri, {:http_basic_authentication => [appid, appid]} ).read
+ begin
+ open( uri, {:http_basic_authentication => [appid, appid]} ).read
+ rescue SecurityError
+ ### FIX ME: mysterious error at 1st access to the API
+ open( uri, {:http_basic_authentication => [appid, appid]} ).read
+ end
### FIX ME: this code failed on Timeout error, temporary using open-uri above.
# px_host, px_port = (@conf['proxy'] || '').split( /:/ )
# px_port = 8080 if px_host and !px_port
# res = Net::HTTP::Proxy( px_host, px_port ).start( uri.host, uri.port ) do |http|
@@ -56,15 +62,13 @@
def search_result
query = CGI::unescape( @cgi.params['q'][0] )
start = CGI::unescape( @cgi.params['start'][0] || '0' ).to_i
begin
- uri = URI::parse( @conf.base_url )
- #uri = URI::parse( 'http://sho.tdiary.net/' ) ### FOR DEBUGGING ###
+ uri = URI::parse( @conf['search-bing.base'] || @conf.base_url )
q = "#{query} site:#{uri.host}"
q << %Q| inurl:"#{uri.path}"| unless uri.path == '/'
- xml = search_bing_api( u( q.untaint ), start )
- json = JSON::parse( xml )
+ json = JSON::parse(search_bing_api(u(q.untaint), start))
rescue Net::HTTPError
return %Q|<p class="message">#$!</p>|
end
r = search_input_form( query )