Skip to content

Instantly share code, notes, and snippets.

@lstoll
Forked from wezm/get_nab_account_balances.rb
Created February 6, 2012 13:50
Show Gist options
  • Save lstoll/1752228 to your computer and use it in GitHub Desktop.
Save lstoll/1752228 to your computer and use it in GitHub Desktop.
Update for current NAB IB
require 'rubygems'
require 'nokogiri'
require 'mechanize'
require 'logger'
module BankBalance
class NabFetcher
def fetch(client_number, password)
agent = Mechanize.new() do |a|
# a.log = Logger.new("mech.log")
a.user_agent_alias = 'Mac FireFox'
# http://rubyforge.org/tracker/index.php?func=detail&aid=24950&group_id=1453&atid=5709
a.keep_alive = false # For slow site
end
login_page = agent.get('https://ib.nab.com.au/nabib/index.jsp')
login_form = login_page.form('loginForm')
key = login_page.root.css('#webKey').first.attr('value')
alpha = login_page.root.css('#webAlpha').first.attr('value')
login_form.userid = client_number
login_form.password = check(password, key, alpha)
balances_page = agent.submit(login_form)
accounts = []
balances_page.root.css('table#accountBalances_nonprimary_subaccounts tbody tr').each do |elem|
accounts << {
:name => elem.css('span.accountNickname').text.strip,
:curr_balance => elem.css('td')[1].text.strip.gsub(',', '').to_f,
:avail_balance => elem.css('td')[2].text.strip.gsub(',', '').to_f
}
end
# Log out
agent.get('https://ib.nab.com.au/nabib/logoutToWCM.ctl?bw=100&bh=100')
accounts
end
protected
def check(p, k, a)
# Implementation of the following javascript function
# function check(p, k, a) {
# for (var i=a.length-1;i>0;i--) {
# if (i!=a.indexOf(a.charAt(i))) {
# a=a.substring(0,i)+a.substring(i+1);
# }
# }
# var r=new Array(p.length);
# for (var i=0;i<p.length;i++) {
# r[i]=p.charAt(i);
# var pi=a.indexOf(p.charAt(i));
# if (pi>=0 && i<k.length) {
# var ki=a.indexOf(k.charAt(i));
# if (ki>=0) {
# pi-=ki;
# if (pi<0) pi+=a.length;
# r[i]=a.charAt(pi);
# }
# }
# }
# return r.join("");
# }
# puts "check(password, #{k})"
# 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# Generate this above as an array of chars
r = []
last_index = p.length - 1
# TODO: Use the passed alphabet instead
alphabet = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
(0..last_index).each do |i|
r[i] = p[i,1]
pi = alphabet.index(r[i])
unless pi.nil? or i >= k.length
ki = alphabet.index(k[i,1])
unless ki.nil?
pi -= ki
pi += alphabet.size if pi < 0
r[i] = alphabet[pi]
end
end
end
r.join('')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment