-
-
Save hnanon/de4801e460a31d93bbdc to your computer and use it in GitHub Desktop.
This file contains 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 'watir-webdriver' | |
require 'date' | |
require 'json' | |
require 'nokogiri' | |
browser = Watir::Browser.start 'https://devops:123456@mycrm.cellularmanager.com/amana/reportautomation/ViewReport.aspx' | |
browser.link(:text, 'Combined Employee Performance Report').click | |
(Date.new(2013, 01, 01)..Date.new(2013, 01, 01)).each do |date| | |
page = Nokogiri::HTML.parse(browser.html) | |
hours_table = page.at('table#UC255_tblSummary') | |
t = browser.text_field :id => 'UC255_txtStart' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
t = browser.text_field :id => 'UC255_txtEnd' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
btn = browser.button :value, 'Run Report' | |
btn.exists? | |
btn.click | |
page = Nokogiri::HTML.parse(browser.html) | |
wage_table = page.at('table#UC252_tblSummary') | |
p "Waiting to scrape employee hours." | |
employee_hours = {} | |
hours_table.search('tr').to_a[1...-2].each do |tr| | |
tds = tr.search('td') | |
hours = tds[1].text.to_f + tds[2].text.to_f | |
employee_hours[ tds[0].text.gsub(/\(.+/, '').gsub(/\u00a0/,'').gsub(/Employee :/, '') ] = {"Hours" => hours.round(2)} | |
p employee_hours [ tds[0].text.gsub(/\(.+/, '').gsub(/\u00a0/,'').gsub(/Employee :/, '') ] = {"Hours" => hours.round(2)} | |
end | |
#=> {"Employee 1"=>{"Reg Hours"=>"10", "OT Hours"=>"20"}, "Employee 2"=>{"Reg Hours"=>"5", "OT Hours"=>"10"}} | |
p "Finished scraping employee hours." | |
t = browser.text_field :id => 'UC252_txtStart' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
t = browser.text_field :id => 'UC252_txtEnd' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
btn = browser.button :value, 'Run Report' | |
btn.exists? | |
btn.click | |
page = Nokogiri::HTML.parse(browser.html) | |
activations_table = page.at('table#UC253_tblSummary') | |
p "Waiting to scrape employee accessory revenue." | |
employee_wage = {} | |
wage_table.search('tr').to_a[1...-2].each do |tr| | |
tds = tr.search('td') | |
key = tds[0].text.gsub(/\(.+/, '').gsub(/\u00a0/,'').gsub(/Employee :/, '') | |
first, last = key.split(', ') | |
key = [last, first].join(', ') | |
employee_wage[ key ] = {"Revenue" => tds[1].text.to_f} | |
p employee_wage[ key ] = {"Revenue" => tds[1].text.to_f} | |
end | |
#=> {"Employee 2"=>{"Revenue"=>"$10"}, "Employee 1"=>{"Revenue"=>"$50"}} | |
p "Finished scraping employee revenue." | |
t = browser.text_field :id => 'UC253_txtStart' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
t = browser.text_field :id => 'UC253_txtEnd' | |
t.set sprintf '%02d%02d%d', date.month, date.day, date.year | |
btn = browser.button :value, 'Run Report' | |
btn.exists? | |
btn.click | |
page = Nokogiri::HTML.parse(browser.html) | |
p "Waiting to scrape employee activations." | |
employee_activations = {} | |
activations_table.search('tr').to_a[1...-2].each do |tr| | |
tds = tr.search('td') | |
activations = tds[1].text.to_i + tds[2].text.to_i + tds[3].text.to_i | |
employee_activations[ tds[0].text.gsub(/\(.+/, '').gsub(/\u00a0/,'').gsub(/Employee :/, '') ] = {"Activations" => activations} | |
p employee_activations[ tds[0].text.gsub(/\(.+/, '').gsub(/\u00a0/,'').gsub(/Employee :/, '') ] = {"Activations" => activations} | |
end | |
#=> {"Employee 2"=>{"Revenue"=>"$10"}, "Employee 1"=>{"Revenue"=>"$50"}} | |
p "Finished scraping employee activations" | |
employee = employee_hours.merge(employee_wage){ |key, old, new| new.merge(old) } | |
employee_final = employee_activations.merge(employee){ |key, old, new| new.merge(old) } | |
employee_final.to_json | |
#Add default empty values | |
employee_final.each do |key, value| | |
if value["Hours"].nil? | |
value["Hours"]=0.00 | |
end | |
if value["Revenue"].nil? | |
value["Revenue"]=0.00 | |
end | |
if value["Activations"].nil? | |
value["Activations"]=0.00 | |
end | |
end | |
p employee_final | |
File.open("data-#{date.strftime('%Y%m%d')}.json", 'w') do |d| | |
# use "\n" for two lines of text | |
d.puts JSON.pretty_generate(employee_final) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment