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", "host" : "https://www.googleapis.com",
"path" : "/customsearch/v1element", "path" : "/customsearch/v1",
"key" : "Yourcustomsearchapikey", "key" : "Yourcustomsearchapikey",
"num" : "1", "num" : "1",
"cx" : "some-cx:id" "cx" : "some-cx:id"
} }
\ No newline at end of file
...@@ -21,11 +21,11 @@ class Search ...@@ -21,11 +21,11 @@ class Search
@googleapiconf = 'googlesearch.json' @googleapiconf = 'googlesearch.json'
@googleurl = '' @googleurl = ''
@isbndbapihost = "isbndb.com" @isbndbapihost = "isbndb.com"
@isbndbapipath = "/api/books.xml?access_key=%APIKEY%&index1=isbn&value1=" @isbndbapipath = "/api/books.xml?access_key=%APIKEY%&index1=isbn&value1="
@isbndbapifile = "isbndb.apikey" @isbndbapifile = "isbndb.apikey"
loadisbndbapikey loadisbndbapikey
loadgoogleconfig loadgoogleconfig
end end
...@@ -41,12 +41,12 @@ class Search ...@@ -41,12 +41,12 @@ class Search
arguments.gsub!( / /, "+" ) # Sanitize search request arguments.gsub!( / /, "+" ) # Sanitize search request
# Retreive JSON # Retreive JSON
agent = Mechanize.new agent = Mechanize.new
page = agent.get( "#{ @googleurl }#{ CGI::escape( arguments ) }" )
json = page.body
res = JSON.parse( json )
puts res.inspect
result = "#{res['items'][0]['link']} - #{res['items'][0]['title']}: #{res['items'][0]['snippet']}"
page = agent.get( "#{ @googleurl }#{ CGI::escape( arguments ) }" )
json = page.body
res = JSON.parse( json )
result = "#{ res[ 'results' ][ 0 ][ 'unescapedUrl' ] } - #{ res[ 'results' ][ 0 ][ 'titleNoFormatting' ] }"
if( result.empty? ) if( result.empty? )
result = "Error: No result." result = "Error: No result."
end end
...@@ -60,23 +60,23 @@ class Search ...@@ -60,23 +60,23 @@ class Search
@irc.message( from, result ) @irc.message( from, result )
end end
end end
# Function to search book titles by ISBN number # Function to search book titles by ISBN number
def isbn( nick, user, host, from, msg, arguments, con ) def isbn( nick, user, host, from, msg, arguments, con )
if( !arguments.nil? && !arguments.empty? ) if( !arguments.nil? && !arguments.empty? )
arguments.gsub!( /[^0-9\-]/, "" ) # Sanitize ISBN number arguments.gsub!( /[^0-9\-]/, "" ) # Sanitize ISBN number
# Retreive XML # Retreive XML
xml = Net::HTTP.get( @isbndbapihost, @isbndbapipath + arguments ) xml = Net::HTTP.get( @isbndbapihost, @isbndbapipath + arguments )
result = isbndbxmlparse( xml ) result = isbndbxmlparse( xml )
if( result.empty? ) if( result.empty? )
result = "Error: No result." result = "Error: No result."
end end
else else
result = "Invalid input. Expecting search query." result = "Invalid input. Expecting search query."
end end
if( con ) if( con )
@output.c( result + "\n" ) @output.c( result + "\n" )
else else
...@@ -107,54 +107,47 @@ class Search ...@@ -107,54 +107,47 @@ class Search
# XML parser for ISBNdb results # XML parser for ISBNdb results
def isbndbxmlparse( xmldoc ) def isbndbxmlparse( xmldoc )
xmldoc = Nokogiri::XML( xmldoc ) xmldoc = Nokogiri::XML( xmldoc )
title = xmldoc.at_xpath( "//ISBNdb/BookList/BookData/Title" ) title = xmldoc.at_xpath( "//ISBNdb/BookList/BookData/Title" )
if( title.instance_of? NilClass ) if( title.instance_of? NilClass )
return "" return ""
else else
return "Book title: #{title.text}" return "Book title: #{title.text}"
end end
end end
# Load API key for ISBNdb # Load API key for ISBNdb
def loadisbndbapikey() def loadisbndbapikey()
# Check if a appid is stored on disk # Check if a appid is stored on disk
if File.exist?( @config.datadir + '/' + @isbndbapifile ) if File.exist?( @config.datadir + '/' + @isbndbapifile )
# Make sure there's a variable instance # Make sure there's a variable instance
apikey = "" apikey = ""
# Read database from file # Read database from file
file = File.open( @config.datadir + '/' + @isbndbapifile ) file = File.open( @config.datadir + '/' + @isbndbapifile )
file.each do |line| file.each do |line|
apikey << line apikey << line
end end
apikey.gsub!( /[:blank:\n\r]/, "" ) apikey.gsub!( /[:blank:\n\r]/, "" )
file.close file.close
# Replace string token with actual key # Replace string token with actual key
@isbndbapipath.gsub!( /%APIKEY%/, apikey ) @isbndbapipath.gsub!( /%APIKEY%/, apikey )
else 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
end end
# Load google custom search api config # Load google custom search api config
def loadgoogleconfig def loadgoogleconfig
if File.exist?( @config.datadir + '/' + @googleapiconf ) 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=" @googleurl = "#{ cnf[ 'host' ] }#{ cnf[ 'path' ] }?key=#{ cnf[ 'key' ] }&num=#{ cnf[ 'num' ] }&cx=#{ cnf[ 'cx' ] }&q="
else 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 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