Module: Pictate
- Defined in:
- lib/pictate.rb
Instance Method Summary collapse
-
#do_search(image_path) ⇒ Object
Carries out the search and retrieves the best match.
-
#pictate(image_path, options = {}) ⇒ Object
Starts the server, gets the best result for the image and stops server.
-
#start_server(server_path) ⇒ Object
Starts the server.
-
#stop_server ⇒ Object
Stops the server.
Instance Method Details
#do_search(image_path) ⇒ Object
Carries out the search and retrieves the best match
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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. 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 = {}) ⇒ Object
Starts the server, gets the best result for the image and stops server.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pictate.rb', line 12 def pictate(image_path, = {}) SpinningCursor.start do action do start_server [:server] SpinningCursor. "I'm thinking" SpinningCursor. (do_search image_path) stop_server end end end |
#start_server(server_path) ⇒ Object
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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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. "Downloading latest version of selenium " + "server standalone" end @selenium_path = Selenium::Server.download(selenium_latest) end SpinningCursor. "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 ⇒ Object
Stops the server
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pictate.rb', line 63 def stop_server @driver.quit @server.stop if @download SpinningCursor. "\#{SpinningCursor.set_message nil}\n\nSelenium was downloaded to \#{@selenium_path}.\nYou can remove it if you need, but note that pictate will download it again if you don't specify another path.\n" end end |