Created
January 21, 2012 01:28
-
-
Save coffeejunk/1650653 to your computer and use it in GitHub Desktop.
google reverse image homemade 'api'
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 'rest_client' | |
require 'selenium-webdriver' | |
# to run this script headless install xvfb | |
# Xvfb :99 -ac & | |
# export DISPLAY=:99 | |
post '/' do | |
begin | |
puts params.inspect | |
rc = RestClient.post( | |
'http://images.google.com/searchbyimage/upload', | |
:image_url => '', | |
:encoded_image => params['file'][:tempfile], | |
:filename => params['file'][:filename], | |
:image_content => '', | |
:hl => 'de', | |
:biw => 1916, | |
:bih => 437 | |
) {|response, request, result| response } | |
url = rc.match(/(?<=HREF=\")http.*(?=\")/)[0] | |
image_name = get_image_name "#{url}&hl=de" | |
puts image_name | |
image_name | |
rescue | |
puts ':(' | |
'FFFFFF-' | |
end | |
end | |
def get_image_name url | |
driver = Selenium::WebDriver.for :firefox | |
driver.navigate.to url | |
element = driver.find_element :id => 'topstuff' | |
txt = element.text | |
driver.quit | |
txt.match(/(?<=dieses\sBild\:\s).*/)[0] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment