Skip to content

Instantly share code, notes, and snippets.

@dcadenas
Created May 31, 2009 23:06
Show Gist options
  • Save dcadenas/121070 to your computer and use it in GitHub Desktop.
Save dcadenas/121070 to your computer and use it in GitHub Desktop.
Script para ver el estado de una cuenta del banco Itaú Uruguay
#!/usr/local/bin/ruby
# encoding: UTF-8
require 'rubygems'
gem 'mechanize'
gem 'main'
gem 'highline'
gem 'money'
gem 'change_watcher'
require 'mechanize'
require 'main'
require 'highline/import'
require 'money'
require 'ostruct'
require 'change_watcher'
class ItauScraper
def initialize(ci, pass)
@ci = ci
@pass = pass
end
def balance
return nil if dashboard.nil?
pesos = dashboard.search(".//td[./font[contains(.,'Pesos')]]/following-sibling::*").first.text.strip
dollars = dashboard.search(".//td[./font[contains(.,'lares')]]/following-sibling::*").first.text.strip
pesos_as_money = "#{pesos} UYU".to_money
dollars_as_money = "#{dollars} USD".to_money
OpenStruct.new pesos_as_money.currency.to_s.downcase.to_sym => pesos_as_money, dollars_as_money.currency.to_s.downcase.to_sym => dollars_as_money
end
def logout
agent.get(itaurl)
end
private
def itaurl
"https://www.itaulink.com.uy/appl/index.jsp"
end
def agent
return @agent if @agent
@agent = ::Mechanize.new do |agent|
agent.user_agent_alias = "Windows IE 7"
end
end
def dashboard
return @dashboard if @dashboard
login_page = agent.get(itaurl)
login_form = login_page.form_with(:name => "form1")
return nil if login_form.nil?
login_form.set_fields(:nro_documento => @ci, :password => @pass)
@dashboard = login_form.submit
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
CI_REGEXP = /\d+/
keyword('ci') do
validate{|foo| foo =~ CI_REGEXP}
description "Cédula de identidad con guión incluido y sin puntos (1234567-8)"
end
def run
ci = params['ci'].value || ask("Cédula?: "){|q| q.validate = CI_REGEXP}
pass = ask("Contraseña? (no se muestra): ") do |q|
q.echo = false
q.validate {|c| c.size <= 8 }
end
itau = ItauScraper.new(ci, pass)
balance = with_progress("Iniciando sesión"){ itau.balance }
if balance.nil?
say("No se pudo acceder a su cuenta. Probablemente la página esté fuera de servicio")
else
say(" Pesos: <%= color('#{balance.uyu}', #{balance.uyu.cents < 0 ? 'RED' : 'GREEN'}, BOLD)%>") if balance.uyu
say(" Dólares: <%= color('#{balance.usd}', #{balance.usd.cents < 0 ? 'RED' : 'GREEN'}, BOLD)%>") if balance.usd
with_progress("Cerrando sesión"){ itau.logout }
ChangeWatcher.new do
data_to_watch do
balance.usd.cents
end
store_changes_in "~/.balance_dolares"
end.check_for_change
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment