Commit dba5a075 authored by Cool Fire's avatar Cool Fire

Updated for new API

parent dff82be8
Pipeline #534 failed with stages
in 4 minutes and 55 seconds
{
"host" : "https://www.googleapis.com",
"path" : "/customsearch/v1element",
"path" : "/customsearch/v1",
"key" : "Yourcustomsearchapikey",
"num" : "1",
"cx" : "some-cx:id"
......
......@@ -41,11 +41,11 @@ class Search
arguments.gsub!( / /, "+" ) # Sanitize search request
# Retreive JSON
agent = Mechanize.new
page = agent.get( "#{ @googleurl }#{ CGI::escape( arguments ) }" )
json = page.body
res = JSON.parse( json )
result = "#{ res[ 'results' ][ 0 ][ 'unescapedUrl' ] } - #{ res[ 'results' ][ 0 ][ 'titleNoFormatting' ] }"
puts res.inspect
result = "#{res['items'][0]['link']} - #{res['items'][0]['title']}: #{res['items'][0]['snippet']}"
if( result.empty? )
result = "Error: No result."
......@@ -137,24 +137,17 @@ class Search
# Replace string token with actual key
@isbndbapipath.gsub!( /%APIKEY%/, apikey )
else
@output.bad( "Could not load API key from #{@config.datadir}/#{@isbndbapifile} for ISBNdb search." )
@output.bad( "Could not load API key from #{@config.datadir}/#{@isbndbapifile} for ISBNdb search.\n" )
end
end
# Load google custom search api config
def loadgoogleconfig
if File.exist?( @config.datadir + '/' + @googleapiconf )
cnf = JSON.parse File.read( "#{@config.datadir}/#{@googleapiconf}" )
jsonline = ""
File.open( @config.datadir + '/' + @googleapiconf ) do |file|
file.each do |line|
jsonline << line
end
end
cnf = JSON.parse( jsonline )
@googleurl = "#{ cnf[ 'host' ] }#{ cnf[ 'path' ] }?key=#{ cnf[ 'key' ] }&num=#{ cnf[ 'num' ] }&cx=#{ cnf[ 'cx' ] }&q="
else
@output.bad( "Could not load API data from #{@config.datadir}/#{@googleapiconf} for Google custom search." )
@output.bad( "Could not load API data from #{@config.datadir}/#{@googleapiconf} for Google custom search.\n" )
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment