Skip to content

Instantly share code, notes, and snippets.

@izak
Created June 18, 2015 14:03
Show Gist options
  • Save izak/091607b0e79052102e26 to your computer and use it in GitHub Desktop.
Save izak/091607b0e79052102e26 to your computer and use it in GitHub Desktop.
CSV downloader for Absa, using phantomjs
var page = require('webpage').create(),
access = '4XXXXXXXXX',
pin = '1234',
userno = '7',
password = 'password22',
accountno = '0000001111';
function waitFor(selector, callback, count){
count = count || 0;
// TODO make this more flexibile than just aborting after 30 seconds.
if (count > 30){
console.error('Timeout waiting for ' + selector);
phantom.exit();
}
console.log('Waiting for ' + selector);
var notthere = page.evaluate(
function(selector){
return document.querySelectorAll(selector).length == 0
}, selector)
if(notthere){
console.log('waiting...');
setTimeout(function(){ waitFor(selector, callback, count+1); }, 1000);
} else {
console.log('continuing.');
callback();
}
}
function step1(){
// Fill in the login details
page.evaluate(function(access, pin, userno){
document.querySelector('input[name="AccessAccount"]').value = access;
document.querySelector('input[name="PIN"]').value = pin;
document.querySelector('input[name="Operator"]').value = userno;
document.querySelector('*[type="submit"]').click();
}, access, pin, userno);
waitFor('form input.pf', step2);
}
function step2(){
// Solve the password Challenge
page.evaluate(function(password){
Array.prototype.forEach.bind(document.querySelectorAll('form input.pf'))
function(ob, idx){
if (ob.type == 'password'){
ob.value = password[idx];
}
});
document.querySelector('*[type="submit"]').click();
}, password);
waitFor('.ap-accountbar', function(){
var fs = require('fs'),
result = page.evaluate(downloadStatement, accountno);
fs.write('csv/' + (new Date()).getTime() + '.csv', result, 'w');
logout();
waitFor('body', function(){ phantom.exit(); });
});
}
function logout(){
// Logout
page.evaluate(function(){
Array.prototype.filter.bind(
document.getElementsByTagName('a'))(
function(el, idx){
return el.textContent.trim() == 'Logoff'
})[0].click();
});
}
// API for statement download
// https://ib.absa.co.za/absa-online/report/transactionHistory.csv
// ?isCreditCard=false
// &fromAccount=12345678
// &fromDate=2015-06-11
// &toDate=2015-06-18
// &sortField=lineNumber
// &sortOrder=DESC
// &cBal=true
// &query=Search%20transactions
// &uniq=new Date().getTime()
// &nonce=absa.NONCE
function downloadStatement(account){
var xmlhttp = new XMLHttpRequest(),
url = 'https://ib.absa.co.za/absa-online/report/transactionHistory.csv?isCreditCard=false',
now = new Date(),
lastweek = new Date(now.getTime()-(7*1000*86400));
url += '&fromAccount=' + account;
url += '&fromDate=' + lastweek.getFullYear() + '-' + (lastweek.getMonth()+1) + '-' + lastweek.getDate();
url += '&toDate=' + now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate();
url += '&sortField=lineNumber&sortOrder=DESC&cBal=true&query=Search%20transactions';
url += '&uniq=' + now.getTime();
url += '&nonce=' + window.absa.NONCE;
xmlhttp.withCredentials = true;
xmlhttp.open("GET", url, false); // synchronous
xmlhttp.send();
if (xmlhttp.status == 200) {
return xmlhttp.responseText;
}
return null;
}
// Topple the first domino
page.open('https://ib.absa.co.za/absa-online/login.jsp', function(stat){
step1();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment