Skip to content

Instantly share code, notes, and snippets.

@foca
Created March 16, 2012 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save foca/2051591 to your computer and use it in GitHub Desktop.
Save foca/2051591 to your computer and use it in GitHub Desktop.
Scraper to get transactions out of my bank accounts
# encoding: UTF-8
require 'mechanize'
require 'money'
require 'ostruct'
module Itau
class Session < Struct.new(:ci, :pass)
def login
login_page = agent.get(itaurl)
login_form = login_page.form_with(:name => "form1")
login_form.set_fields(:nro_documento => ci, :password => pass)
Dashboard.new(login_form.submit)
end
def logout
agent.get(itaurl)
end
def agent
@agent ||= Mechanize.new do |agent|
agent.user_agent_alias = "Windows IE 7"
end
end
def itaurl
"https://www.itaulink.com.uy/appl/index.jsp"
end
end
class Dashboard < Struct.new(:page)
CURRENCIES = {
"Pesos" => "UYU",
"Dólares" => "USD",
"Euros" => "EUR"
}
def checking_accounts
account_links("Cuentas Corrientes") do |link|
Account.new(:checking, account_currency(link), link.text, link.click)
end
end
def savings_accounts
account_links("Cajas de Ahorro") do |link|
Account.new(:savings, account_currency(link), link.text, link.click)
end
end
private
def account_currency(link)
curr = link.node.search("../../following-sibling::*[./font[contains(.,'Pesos') or contains(.,'lares') or contains(.,'Euros')]]")
CURRENCIES.fetch(curr.text.strip)
end
def account_links(text, &block)
page.links.select do |link|
link.node.xpath("../../../..//font[contains(.,'#{text}')]").any?
end.map(&block)
end
end
class Account < Struct.new(:type, :currency, :number, :page)
def transactions
page.search(".//tr[./td[contains(.,'Concepto')]]/following-sibling::*").map do |tr|
cells = tr.search("td").map(&:text)
cells[2..-1].map! { |s| "#{s} #{currency}".to_money if s =~ /\d+/ }
Transaction.new(*cells)
end
end
end
class Transaction < Struct.new(:date, :description, :expense, :income, :balance)
@@date = lambda do |str|
months = %W(ENE FEB MAR ABR MAY JUN JUL AGO SET OCT NOV DIC)
matches = str.match /(?<day>\d{2})(?<month>\w{3})(?<year>\d{4})/
matches && Date.new(matches[:year].to_i, months.index(matches[:month]) + 1, matches[:day].to_i)
end
def initialize(date, *args)
super @@date[date], *args
end
end
end
@foca
Copy link
Author

foca commented Mar 16, 2012

Una forma de usarlo es

session = Itau::Session.new("3.628.628-4", "secret85")
dashboard = session.login
dashboard.checking_accounts.each do |account|
  p account.transactions
end
dashboard.savings_accounts.each do |account|
  p account.transactions
end

Tengo que seguir jugando con esto para llegar a sacar información útil, y agregar las tarjetas de crédito también :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment