Skip to content

Instantly share code, notes, and snippets.

@kn0ll
Created October 30, 2010 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn0ll/654957 to your computer and use it in GitHub Desktop.
Save kn0ll/654957 to your computer and use it in GitHub Desktop.
google image search proxy for hi-res movie posters
require 'movies'
run Sinatra::Application
require 'sinatra'
require 'json'
require 'open-uri'
require 'cgi'
get '/:title/:size' do
props = {
:ratio => 26.5 / 40.0,
:size => params[:size],
:q => '%s+movie+poster' % [CGI.escape(params[:title])]
}
# return google search results
open('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s&rsz=8&imgsz=%s' % [props[:q], props[:size]]) do |r|
# get all images
imgs = JSON.parse(r.read)['responseData']['results']
# filter vertical images only
imgs = imgs.select { |i|
r = i['width'].to_f / i['height'].to_f
r < 1
}
# make sure the image has an appropriate ratio
if(props[:ratio])
ratio_threshold = props[:ratio] * 0.1
imgs = imgs.select { |i|
r = i['width'].to_f / i['height'].to_f
(r > (props[:ratio] - ratio_threshold)) && (r < (props[:ratio] + ratio_threshold))
}
end
# load and proxy image
open(imgs[0]['url']) do |i|
content_type i.content_type
i.read
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment