Created
March 16, 2012 18:14
-
-
Save foca/2051591 to your computer and use it in GitHub Desktop.
Scraper to get transactions out of my bank accounts
This file contains hidden or 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Una forma de usarlo es
Tengo que seguir jugando con esto para llegar a sacar información útil, y agregar las tarjetas de crédito también :)