Created
March 12, 2015 04:55
-
-
Save Resisty/06b76cb0b9b86145375e to your computer and use it in GitHub Desktop.
Google Api Client test trying to get random result from next pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/api_client' | |
def gsearch(searchstr, ftype: 'jpg', start: nil, depth: nil) | |
puts "Entering function with depth: \"#{depth}\"" | |
client = Google::APIClient.new(:application_name => 'IRC Bot Search Helper', | |
:application_version => '0.0.1', | |
:client_id => cid, | |
:client_secret => cs, | |
:authorization => nil) | |
search = client.discovered_api('customsearch') | |
parameters = { | |
'q' => searchstr, | |
'key' => key, | |
'cx' => cx, | |
'searchType' => 'image', | |
'fileType' => ftype | |
} | |
if start | |
parameters['start'] = start | |
parameters['max-results'] = 10 | |
end | |
response = client.execute( | |
:api_method => search.cse.list, | |
:parameters => parameters | |
) | |
body = JSON.parse(response.body) | |
#puts body['queries'] | |
if depth == nil | |
depth = rand(5) | |
end | |
if body['queries'].has_key?('nextPage') | |
tok = body['queries']['nextPage'][0]['startIndex'] | |
else | |
tok = nil | |
end | |
if depth == 0 or !tok | |
puts "Depth: #{depth}, returning body" | |
return body | |
end | |
puts "Made it to the end, recursing with depth: #{depth}" | |
return gsearch(search, start: tok, depth: depth - 1) | |
end | |
body = gsearch('quokka selfies') | |
#puts body.keys() | |
#puts | |
#body.keys().each() do |k| | |
# puts body[k] | |
# puts | |
#end | |
body['items'].each() do |i| | |
puts i['link'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This breaks on line 56:
': undefined methodgsearch.rb:56:in
each' for nil:NilClass (NoMethodError)
because the nextPage/next_page_token/parameters['start']/etc doesn't work.
If we force it to drop out before trying to get the next page, it works just fine.