forked from fabi125-zz/AIB-Transaction-Download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aib.rb
164 lines (139 loc) · 5.98 KB
/
aib.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require "rubygems"
require "mechanize"
require "logger"
require 'csv'
require 'date'
require 'optparse'
$agent = Mechanize.new {|a| a.log = Logger.new("mech.log") }
# setup the cmd-line parser
options = {}
OptionParser.new do |opts|
opts.banner = "This script will download your AIB transactions for all accounts into a CSV between the specified date and yesterday"
opts.on('-p', '--pac PAC', "Your 5-digit Personal Access Code") do |pac|
options[:pac] = pac.scan(/./)
end
opts.on('-n','--regnum REGNUM', 'Registration Number') do | reg|
options[:reg] = reg
end
opts.on('-h', '--home HOME', 'Last 4 digits of your home phone number') do |h|
options[:home] = h
end
opts.on('-w', '--work WORK', 'Last 4 digits of your work phone number') do |w|
options[:work] = w
end
opts.on('-v', '--visa VISA', 'Last 4 digits of your primary AIB visa card') do |v|
options[:visa] = v
end
opts.on('-d', '--date DATE', 'The start date of the transactions dd/mm/yyyy format') do |d|
pp d
options[:date] = Date.strptime(d, '%d/%m/%Y')
end
opts.on_tail("--help", "Show this message") do
puts opts
exit
end
end.parse!
def login(regnum, pac, home, work, visa)
########### Login Page 1
puts "###################################### Step 1"
$agent.get("https://aibinternetbanking.aib.ie/inet/roi/login.htm") do |page|
page2 = page.form('form1') do |form|
form.regNumber = regnum
end.submit
############ Login Page 2
puts "###################################### Step 2"
page3 = page2.form('loginstep2') do |pac_form|
i=1
# PAC code
digits = page2.search("//input[contains(@id, 'digit')]/../label/strong/text()")
digits.each{ |d|
pac_index = d.text.match(/Digit ([12345])/)[1].to_i
pac_form["pacDetails.pacDigit#{i}"] = pac[pac_index-1]
#puts "#{i}: Digit #{pac_index} => #{PAC[pac_index-1]}"
i+=1
}
# Challenge: Visa, Work or Home phone
challenge = page2.search("//label[@for='challenge']/strong[2]/.").text()
if challenge.index('Visa')
code = visa
elsif challenge.index('work')
code = work
elsif challenge.index('home')
code = home
end
pac_form['challengeDetails.challengeEntered'] = code
#puts "#{challenge} = #{code}"
end.submit
end
end
def update_transactions(from, to)
############# Navigate to transaction search page
puts "###################################### Step 3"
if $agent.current_page.search("//h1/.").text() == 'You are securely logged in.'
puts "#################################### Success logging in!"
statements = $agent.current_page.form_with(:action => 'statement.htm').submit
numAccounts = statements.search("//select[@id='index']/option").length
statements.form_with(:action => 'searchtransactions.htm').submit
for account in 1..numAccounts
statementSearch = $agent.current_page
# get the account name
account_name = statementSearch.search("//select[@id='dsAccountListIndex']/option[#{account+1}]").text()
puts account_name
# fill out the transaction search form
results = statementSearch.form_with(:action => 'searchtransactions.htm') do |statementForm|
statementForm.startDateDD = from.strftime('%d')
statementForm.startDateMM = from.strftime('%m')
statementForm.startDateYYYY = from.strftime('%Y')
statementForm.endDateDD = to.strftime('%d')
statementForm.endDateMM = to.strftime('%m')
statementForm.endDateYYYY = to.strftime('%Y')
statementForm.field_with(:name => 'dsAccountListIndex').options[account].select
pp statementForm
end.submit
# grab the transactions table
transactions = []
for row in results.search("//table//th[text()='Date:']/../../../tbody/tr")
date = row.search('td[1]').text.gsub('/', '.')
desc = row.search('td[2]').text
debit = row.search('td[3]').text.gsub(',', '')
credit = row.search('td[4]').text.gsub(',', '')
balance = row.search('td[5]').text.gsub(',', '')
if debit.length != 0
amount = "-" + debit
elsif credit.length != 0
amount = credit
else
amount = ''
end
puts "#{date} #{desc} #{amount} #{balance}"
transactions << [date, desc, amount, balance]
end
pp transactions
transactions_clean = []
i = 0
while i < transactions.length do
new_i = i
j = i + 1
while j < transactions.length && transactions[j][2].length == 0 do
transactions[i][j - i + 3] = transactions[j][1]
if transactions[i][3].length == 0 && transactions[j][3].length != 0
transactions[i][3] = transactions[j][3]
end
new_i += 1
j += 1
end
transactions_clean << transactions[i]
i = new_i + 1
end
pp transactions_clean
CSV.open("#{account_name}_#{from.to_s}-#{to.to_s}.csv", "w") do |csv|
csv << ["Date", "Description", "Amount", "Balance", "Description2", "Description3"]
transactions_clean.each { |t| csv << t }
end
end
end
end
pp options
to_date = Date.today-1
login options[:reg], options[:pac], options[:home], options[:work], options[:visa]
update_transactions options[:date], to_date