Created
October 30, 2010 04:46
-
-
Save kn0ll/654957 to your computer and use it in GitHub Desktop.
google image search proxy for hi-res movie posters
This file contains hidden or 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 'movies' | |
run Sinatra::Application |
This file contains hidden or 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 '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