forked from russss/barclayscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
67 lines (58 loc) · 1.94 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
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() {
// navigate to account-specific page
try {
await this.page.$eval('[href="'+u.cssEsc(this.href)+'"]', el => { el.click() });
const found = await u.wait(this.page, '.transaction-list-container-header');
if (!found) {
throw new Error("Could not find .transaction-list-container-header");
}
}
catch(err) {
console.warn("Warning: Could not retrieve account [" + this.number + "]. - " + err.toString());
throw err;
}
}
async statementOFX() {
try {
// 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.warn("Warning: StatementOFX clicking a.export failed");
await this.session.home();
return null;
}
} catch(err) {
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('Exported OFX for account [' + this.number + ']');
await this.session.home();
return ofx;
}
toString() {
return '[Account ' + this.number + ']';
}
};