Skip to content

Instantly share code, notes, and snippets.

@sorah
Forked from negipo/animegif.rb
Last active August 29, 2015 14:02
Show Gist options
  • Save sorah/eab34e86ffab2e1d75d6 to your computer and use it in GitHub Desktop.
Save sorah/eab34e86ffab2e1d75d6 to your computer and use it in GitHub Desktop.
#!/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