Commit 3ada1018 authored by Cool Fire's avatar Cool Fire
Browse files

Use Google custom search instead of deprecated search api

parent 030a6b15
Pipeline #23 passed with stage
in 2 minutes and 13 seconds
{
"host" : "https://www.googleapis.com",
"path" : "/customsearch/v1element",
"key" : "Yourcustomsearchapikey",
"num" : "1",
"cx" : "some-cx:id"
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
#Search plugin using googleapis
require 'nokogiri'
require 'mechanize'
require 'json'
require 'cgi'
......@@ -17,15 +18,16 @@ class Search
@output = output
@irc = irc
@timer = timer
@googleapiconf = 'googlesearch.json'
@googleurl = ''
@googleapihost = "ajax.googleapis.com"
@goolgeapipath = "/ajax/services/search/web?v=1.0&q="
@isbndbapihost = "isbndb.com"
@isbndbapipath = "/api/books.xml?access_key=%APIKEY%&index1=isbn&value1="
@isbndbapifile = "isbndb.apikey"
@isbndbapihost = "isbndb.com"
@isbndbapipath = "/api/books.xml?access_key=%APIKEY%&index1=isbn&value1="
@isbndbapifile = "isbndb.apikey"
loadisbndbapikey
loadgoogleconfig
end
#default method
......@@ -38,8 +40,12 @@ class Search
if( !arguments.nil? && !arguments.empty? )
arguments.gsub!( / /, "+" ) # Sanitize search request
# Retreive JSON
json = Net::HTTP.get( @googleapihost, @goolgeapipath + CGI::escape( arguments ) )
result = googlejsonparse( json )
agent = Mechanize.new
page = agent.get( "#{ @googleurl }#{ CGI::escape( arguments ) }" )
json = page.body
res = JSON.parse( json )
result = "#{ res[ 'results' ][ 0 ][ 'url' ] } - #{ res[ 'results' ][ 0 ][ 'titleNoFormatting' ] }"
if( result.empty? )
result = "Error: No result."
......@@ -97,27 +103,7 @@ class Search
end
end
private
# JSON parser routine for google results
def googlejsonparse( doc )
doc = JSON.parse( doc )
doc = doc[ "responseData" ][ "results" ]
if( doc.instance_of? NilClass or doc.empty? )
return ""
else
title = doc[ 0 ] [ "titleNoFormatting" ]
url = CGI::unescape( doc[ 0 ] [ "url" ])
result = "Result: #{title} - #{url}"
return result
end
# Mark object for garbage collection
doc = nil
end
private
# XML parser for ISBNdb results
def isbndbxmlparse( xmldoc )
xmldoc = Nokogiri::XML( xmldoc )
......@@ -157,4 +143,21 @@ class Search
@output.bad( "Could not load API key from #{@config.datadir}/#{@isbndbapifile} for ISBNdb search." )
end
end
# Load google custom search api config
def loadgoogleconfig
if File.exists?( @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." )
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