-
-
Save sorah/eab34e86ffab2e1d75d6 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# https://developers.google.com/image-search/v1/jsondevguide?hl=ja | |
require 'open-uri' | |
require 'uri' | |
require 'json' | |
def main | |
keyword = ARGV.shift | |
count = (ARGV.shift || 1).to_i | |
puts search(keyword).sample(count).map{|r| r['url'] } | |
end | |
def search(keyword, page=1) | |
per_page = 8 | |
options = { | |
q: URI.encode_www_form_component("#{keyword} gif"), | |
rsz: per_page, | |
safe: 'off', | |
v: '1.0', | |
as_filetype: 'gif', | |
imgsz: 'large', | |
start: (page - 1) * per_page + 1, | |
as_sitesearch: 'tumblr.com' | |
} | |
params = options.map {|key, val| "#{key}=#{val}" }.join('&') | |
url = "http://ajax.googleapis.com/ajax/services/search/images?#{params}" | |
JSON.parse(open(url, &:read))['responseData']['results'] | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment