This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathaccount.js
178 lines (159 loc) · 6.06 KB
/
account.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const u = require('./utils.js');
// Class for dealing with the Barclays account page.
module.exports = class Account {
constructor(session, href, number, label, balance) {
this.session = session;
this.page = session.page;
this.href = href;
this.number = number;
this.label = label;
this.balance = balance;
}
async select() {
// Switch the page to this account.
// Call `await this.session.home()` to reset state when you're done.
console.log('Selecting account ' + this.number);
await this.page.$eval('[href="'+u.cssEsc(this.href)+'"]', el => { el.click() });
// waitForNavigation seems to stall indefinitely here (?!) so we don't use u.click
await u.wait(this.page, '.transaction-list-container-header');
}
async statementOFX() {
// Return an OFX-formatted string of the most recent account statement.
await this.select();
// waitFor is required here as of 12/2020
await this.page.waitForTimeout(1000);
if (!(await this.page.$('a.export'))) {
console.log(
'No export option (probably no transactions) for account ' +
this.number,
);
await this.session.home();
return null;
}
const ofx = await this.page.evaluate(() => {
let hashTag = document.querySelector('#trans-hashTag').value;
let data = JSON.stringify({
"hashTag": hashTag
});
let url = "https://bank.barclays.co.uk/olb/trans/transdecouple/ControllerExportTransaction.do?hashTag=" + hashTag + "¶m=" + data + "&downloadFormat=ofx";
return fetch(url, {method: 'GET', credentials: 'include'}).then(r =>
r.text(),
);
});
console.log('Fetched OFX for account ' + this.number);
await this.session.home();
return ofx;
}
async statement(from, to) {
await this.select();
if ((await this.page.$('#no-trans-msg'))) {
console.log(
'No transactions for account ' +
this.number,
);
await this.session.home();
return [];
}
await u.wait(this.page, '#search');
if (from) {
let fromSelector = '[label="From date"] [name=datepicker]';
await u.wait(this.page, fromSelector);
await this.page.$eval(fromSelector, (el, f) => {
el.value = f;
angular.element(el).triggerHandler('input');
}, from);
}
if (to) {
let toSelector = '[label="To date"] [name=datepicker]';
await u.wait(this.page, toSelector);
await this.page.$eval(toSelector, (el, t) => {
el.value = t;
angular.element(el).triggerHandler('input');
}, to);
}
await this.page.$eval('#search', el => { el.click() });
await this.page.waitForFunction(() => {
return document.querySelector('#trans-spinner').style.display === 'none';
});
// Parse the transactions in the context of the page.
let transactions = await this.page.evaluate(() => {
let txns = {};
let txn_list = [];
let rows = document.querySelectorAll('#filterable-trans .tbody-trans .tr');
if (rows.length) {
[].forEach.call(rows, function (row) {
if (row.id) {
let row_id = row.id.replace(/transaction_(\d+).*/, '$1');
let txd;
txd = {};
txn_list.push(txd);
txns[row_id] = txd;
txd['amount'] = row.querySelector('.money-in').innerText.trim()
|| row.querySelector('.money-out').innerText.trim();
txd['description'] = row.querySelector('.description span').innerText.trim();
let date = new Date(Date.parse(row.querySelector('.date').innerText.trim()));
let day = (date.getDate() + '').padStart(2, '0');
let month = (date.getMonth() + 1 + '').padStart(2, '0');
let year = date.getFullYear();
txd['date'] = day + '/' + month + '/' + year;
txd['balance'] = row.querySelector('.balance').innerText.trim();
let transType = row.querySelector('[headers=header-description] .additional-data-content:not(.ng-scope)');
txd['trans-type'] = transType.innerText.trim();
let refs = [];
let ref1 = row.querySelector('[data-ng-if="entry.narrativeLine2"]');
if (ref1) {
refs.push(ref1.textContent);
}
let extraRefs = row.querySelector('[data-ng-if="entry.narrativeLine3to15"]');
if (extraRefs) {
refs.push(extraRefs.textContent.replace(/\s\s+/g, '\n').split('\n').join(' '));
}
let refParts = [];
refs.forEach(function (ref) {
let refTrim = ref.trim();
if (refTrim) {
refParts.push(refTrim);
}
});
txd['ref'] = refParts[0];
txd['ref2'] = refParts[1];
}
});
}
return txn_list;
});
let statement = [].slice.call(transactions);
let logLine = 'Fetched statement for account ' + this.number;
if (from) {
logLine += ' from=' + from;
}
if (to) {
logLine += ' to=' + to;
}
console.log(logLine);
await this.session.home();
return statement;
}
async statementCSV(from, to) {
// Return a CSV-formatted string of the most recent account statement.
let statement = await this.statement(from, to);
return this.csvLines(statement);
}
csvLines(statement) {
var csvLines = statement.map(function (d) {
var ref = '';
if (d.ref) {
ref = "-" + d.ref;
if (d.ref2) {
ref = ref + '-' + d.ref2;
}
}
return d['date'] + ',' + d['trans-type'].replace(/,/g, ';') + '-' + d['description'].replace(/,/g, ';') + ref + ',' + d['amount'].replace(/[£, A-Z]/g, '');
});
csvLines.unshift('Date,Reference,Amount');
return csvLines;
}
toString() {
return '[Account ' + this.number + ']';
}
};