Created
March 1, 2010 02:33
-
-
Save dcadenas/318030 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
#!/usr/local/bin/ruby | |
require 'rubygems' | |
gem 'mechanize' | |
gem 'main' | |
gem 'highline' | |
require 'mechanize' | |
require 'main' | |
require 'highline/import' | |
class WorkingHours | |
def initialize(harvest_email, harvest_pass, tt_login, tt_pass) | |
@harvest_email = harvest_email | |
@harvest_pass = harvest_pass | |
@tt_login = tt_login | |
@tt_pass = tt_pass | |
end | |
def hours | |
tt_hours = if tt_dashboard.nil? | |
nil | |
else | |
tt_dashboard.search(".//span[@class='main-num']").first.text.strip.to_f | |
end | |
harvest_hours = if harvest_dashboard.nil? | |
nil | |
else | |
harvest_dashboard.search(".//span[contains(.,'This Month')]/following-sibling::*").first.text.strip.to_f | |
end | |
[tt_hours, harvest_hours] | |
end | |
def logout | |
agent.get(tt_logout) | |
agent.get(harvest_logout) | |
end | |
private | |
def harvest_url | |
"https://cubox.harvestapp.com/account/login" | |
end | |
def harvest_logout | |
"https://cubox.harvestapp.com/account/logout" | |
end | |
def tt_url | |
"http://tt.entp.com/login?to=/" | |
end | |
def tt_logout | |
"http://tt.entp.com/logout" | |
end | |
def agent | |
return @agent if @agent | |
@agent = WWW::Mechanize.new do |agent| | |
agent.user_agent_alias = "Windows IE 7" | |
end | |
end | |
def tt_dashboard | |
return @tt_dashboard if @tt_dashboard | |
login_page = agent.get(tt_url) | |
login_form = login_page.form_with(:action => "/session") | |
return nil if login_form.nil? | |
login_form.set_fields(:login => @tt_login, :password => @tt_pass) | |
dashboard = login_form.submit | |
#we do this to be tolerant for future logins through openid | |
user_name = dashboard.links.detect{|l| l.href =~ /^\/users\/(.*)\/edit$/}.text | |
@tt_dashboard = agent.get("http://tt.entp.com/users/#{user_name}/monthly") | |
end | |
def harvest_dashboard | |
return @harvest_dashboard if @harvest_dashboard | |
login_page = agent.get(harvest_url) | |
login_form = login_page.form_with(:action => "/account/create_session") | |
return nil if login_form.nil? | |
login_form.set_fields("user[email]" => @harvest_email, "user[password]" => @harvest_pass) | |
dashboard = login_form.submit | |
@harvest_dashboard = agent.get("https://cubox.harvestapp.com/overview") | |
end | |
end | |
def with_progress(message = nil, done_message = nil, &work) | |
print "#{message}" if message | |
finished = false | |
progress_thread = Thread.new { until finished; print "."; $stdout.flush; sleep 0.5; end; } | |
result = nil | |
work_thread = Thread.new(binding()) do |b| | |
result = work.call | |
finished = true | |
end | |
work_thread.join | |
progress_thread.join | |
say "\n#{done_message}" | |
result | |
end | |
HighLine.track_eof = false | |
Main do | |
keyword('harvest_email') do | |
description "Login for harvest" | |
end | |
keyword('tt_login') do | |
description "Login for tt" | |
end | |
def run | |
harvest_email = params['harvest_email'].value || ask("Harvest email?: ") | |
harvest_pass = ask("Contraseña Harvest? (no se muestra): ") do |q| | |
q.echo = false | |
end | |
tt_login = params['tt_login'].value || ask("TT login?: ") | |
tt_login = harvest_email if tt_login == "" | |
tt_pass = ask("Contraseña TT? (no se muestra, enter para usar la misma que en harvest): ") do |q| | |
q.echo = false | |
end | |
tt_pass = harvest_pass if tt_pass == "" | |
working_hours = nil | |
hours = with_progress("Scraping") do | |
working_hours = WorkingHours.new(harvest_email, harvest_pass, tt_login, tt_pass) | |
working_hours.hours | |
end | |
puts | |
if hours.nil? | |
say("Error!!") | |
else | |
say(" Harvest: <%= color('#{hours.last}', GREEN, BOLD)%>") if hours.last | |
say(" TT: <%= color('#{hours.first}', GREEN, BOLD)%>") if hours.first | |
say(" ------------------------") | |
say(" Total: #{hours.first + hours.last}") | |
puts | |
with_progress("Cerrando sesión"){ working_hours.logout } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment