lib/travian_bot/application.rb in travian_bot-0.1.1 vs lib/travian_bot/application.rb in travian_bot-0.1.2

- old
+ new

@@ -1,38 +1,66 @@ require 'selenium-webdriver' -require 'travian_bot/tools' +require 'chronic' +require 'yaml' class TravianBot class Application class << self - include Tools def run!(*arguments) + login + building_queue + close + + return 1 + end + + private + + # Load the credentials from ENV['HOME']/.travian_bot + def get_credentials + credentials = YAML::load(File.open("#{ENV['HOME']}/.travian_bot")) + + [credentials['travian_bot']['url'], credentials['travian_bot']['usr'], credentials['travian_bot']['pwd']] + end + + # Login in to travian page + def login url, user, password = get_credentials - driver = Selenium::WebDriver.for :firefox - driver.navigate.to(url) + @driver = Selenium::WebDriver.for :firefox + @driver.navigate.to(url) + name_input = @driver.find_element(:name, 'name') + password_input = @driver.find_element(:name, 'password') + login_button = @driver.find_element(:id, 's1') - # Login - name_input = driver.find_element(:name, 'name') - password_input = driver.find_element(:name, 'password') - login_button = driver.find_element(:id, 's1') - name_input.send_keys(user) password_input.send_keys(password) login_button.submit - - # Check if building something + end + + # Close the selenium connection + def close + @driver.quit + end + + # Check if building something + def building_queue begin - building_pipe = driver.find_element(:id, 'building_contract') - building_time = driver.find_element(:class, 'buildingTime').text - puts wait_till(building_time) + building_pipe = @driver.find_elements(:xpath, "//table[@id='building_contract']/tbody/tr") + + building_pipe.each do |entry| + puts entry.text + end rescue Selenium::WebDriver::Error::NoSuchElementError - puts 'Nothing to build' + puts 'Nothing in building queue' end - - driver.quit + end - return 1 + # Get the ending time of a string + def wait_till(input) + time = input.to_s.match(/(\d*):(\d*)$/) + + Chronic.parse("today #{time[0]}:#{time[1]}") end end end end