module Pictate
Public Instance Methods
do_search(image_path)
click to toggle source
Carries out the search and retrieves the best match
# File lib/pictate.rb, line 79 def do_search(image_path) @driver.navigate.to "http://www.google.co.uk/searchbyimage" # show the upload dialog @driver.execute_script("google.qb.ti(true)") # upload image and search upload = @driver.find_element(:name, 'encoded_image') upload.send_keys(File.expand_path image_path) # best result wait = Selenium::WebDriver::Wait.new(:timeout => 100) wait.until { @driver.find_element(:id => 'topstuff') } topstuff = @driver.find_element(:id => 'topstuff') best = topstuff.text.lines.to_a.last if not best.match /Best(.*)/ result = "Can't figure it out for the life of me, sorry!" else best.gsub!("Best guess for this image:", "") result = "I believe this is a picture of: #{best}." end end
pictate(image_path, options = {})
click to toggle source
Starts the server, gets the best result for the image and stops server.
# File lib/pictate.rb, line 12 def pictate(image_path, options = {}) SpinningCursor.start do action do start_server options[:server] SpinningCursor.set_banner "I'm thinking" SpinningCursor.set_message (do_search image_path) stop_server end end end
start_server(server_path)
click to toggle source
Starts the server. Checks to see if you’ve given it a server path, if not it will check the ext directory of the gem library. If it’s not there, it will download it to that directory.
- Note
-
perhaps a better directory is needed as it is overwritten when the gem is reinstalled.
# File lib/pictate.rb, line 31 def start_server(server_path) if server_path selenium_path = server_path else previous_wd = FileUtils.pwd begin FileUtils.cd(File.join ENV['HOME'], '.selenium') rescue FileUtils.mkdir(File.join ENV['HOME'], '.selenium') FileUtils.cd(File.join ENV['HOME'], '.selenium') end selenium_latest = Selenium::Server.latest if not File.exists? "selenium-server-standalone-#{selenium_latest}.jar" @download = true SpinningCursor.set_banner "Downloading latest version of selenium " + "server standalone" end @selenium_path = Selenium::Server.download(selenium_latest) end SpinningCursor.set_banner "Starting selenium standalone server" @server = Selenium::Server.new(@selenium_path, :background => true) @server.start caps = Selenium::WebDriver::Remote::Capabilities.htmlunit( :javascript_enabled => true) @driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps) FileUtils.cd(previous_wd) end
stop_server()
click to toggle source
Stops the server
# File lib/pictate.rb, line 63 def stop_server @driver.quit @server.stop if @download SpinningCursor.set_message <<-EOF #{SpinningCursor.set_message nil} Selenium was downloaded to #{@selenium_path}. You can remove it if you need, but note that pictate will download it again if you don't specify another path. EOF end end