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
/
barclayscrape.js
executable file
·301 lines (273 loc) · 8.95 KB
/
barclayscrape.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env node
const util = require('util');
const path = require('path');
const fs = require('fs');
const fs_writeFile = util.promisify(fs.writeFile);
const program = require('commander');
const Configstore = require('configstore');
const prompt = require('syncprompt');
const pkg = require('./package.json');
const session = require('./session.js');
const services = require('./services.js');
const conf = new Configstore(pkg.name);
program
.version(pkg.version)
.description('Programmatic access to Barclays online banking.')
.option('--otp [pin]', 'PINSentry code')
.option('--motp [pin]', 'Mobile PINSentry code')
.option('--plogin', 'Login using passcode and password')
.option('--no-headless', 'Show browser window when interacting');
program
.command('list')
.option('-j, --json', 'Output account list as a JSON object')
.description('List all available accounts')
.action(async options => {
var sess;
try {
sess = await auth();
} catch (err) {
console.error(err);
return;
}
try {
const accounts = await sess.accounts();
if (options.json) {
let account_list = accounts.map( function(acc) { return {'number': acc.number, 'alias': exportLabel(acc), 'name': acc.label, 'balance': acc.balance} });
console.log(JSON.stringify(account_list));
}
else {
console.table(accounts.map(acc => [acc.number, exportLabel(acc), acc.label, acc.balance]));
}
} catch (err) {
console.error(err);
} finally {
await sess.close();
}
});
program
.command('get_ofx_combined [out_path]')
.description('Fetch a combined .ofx file containing all accounts into out_path directory')
.action(async (out_path, options) => {
// if out_path is undefined, default to cwd
if (typeof(out_path) == "undefined") {
out_path = '.';
}
var sess;
try {
sess = await auth();
try {
var serv = new services(sess);
await serv.get_ofx_combined(out_path)
} catch (err) {
console.error(err);
}
} catch (err) {
console.error(err);
return;
} finally {
await sess.close();
}
});
program
.command('get_ofx <out_path>')
.description('Fetch individual ofx files for each account, into out_path directory')
.action(async (out_path, options) => {
var sess;
try {
sess = await auth();
} catch (err) {
console.error(err);
return;
}
try {
const accounts = await sess.accounts();
for (let account of accounts) {
const ofx = await account.statementOFX();
if (ofx) {
await fs_writeFile(path.join(out_path, exportLabel(account)) + '.ofx', ofx);
}
}
} catch (err) {
console.error(err);
} finally {
await sess.close();
}
});
program
.command('csv')
.option('-p, --path <path>', 'Export path. defaults to ./export')
.description('Fetch .csv files for accounts')
.option('-f, --from <dd/mm/yyyy>', 'From date')
.option('-t, --to <dd/mm/yyyy>', 'To date')
.action(async (options) => {
var sess;
try {
sess = await auth();
} catch (err) {
console.error(err);
return;
}
try {
const accounts = await sess.accounts();
for (let account of accounts) {
const csvLines = await account.statementCSV(options.from, options.to);
if (csvLines) {
var label = exportLabel(account);
var extraLog = '';
if (label != account.number) {
extraLog = ' (' + account.number + ')'
}
let csv = [].join.call(csvLines, '\n');
let out_path = options.path || 'export';
console.log("Exporting " + label + extraLog + " (" + (csvLines.length - 1) + " rows)");
await fs_writeFile(path.join(out_path, label) + '.csv', csv);
}
}
} catch (err) {
console.error(err);
} finally {
await sess.close();
}
});
program
.command('config')
.description('Set up login details')
.action(options => {
var surname = prompt('Enter your surname: ');
conf.set('surname', surname);
do {
var num = prompt('Enter your online banking membership number: ');
if (num.length != 12) {
console.log('Membership number should be 12 digits');
}
} while (num.length != 12);
conf.set('membershipno', num);
console.log(
"\nIf you're going to be logging in using PinSentry or Passcode, please enter the last few\n" +
"(usually four) digits of your card number, which you're prompted for on login.\n" +
"If you're using Mobile PinSentry, you can leave this blank.\n",
);
var digits = prompt('Enter the last digits of your card number: ');
conf.set('card_digits', digits);
console.log(
"\nSome accounts allow logging in via a memorable passcode and password.\n" +
"It is recommended you leave this blank, unless you understand the security implications.\n",
);
do {
var passcode = prompt('Enter your 5 digit memorable passcode, or leave blank (recommended): ');
if ((passcode !== '') && (passcode.length != 5)) {
console.log('Memorable passcode must be 5 digits');
}
} while ((passcode !== '') && (passcode.length != 5));
var password = '';
if (passcode !== '') {
console.log(
"\nEnter your memorable password (Barclays will request 2 random characters from it when logging in via passcode).\n"
);
password = prompt('Enter your memorable password: ');
}
var card_cvv = '';
if (passcode !== '') {
console.log(
"\nWhen logging in via passcode, Barclays will occasionally prompt for your card CVV number as an additional security measure.\n"
);
do {
card_cvv = prompt('Enter the 3 digit CVV number (on the back of your card), or leave blank to abort: ');
if ((card_cvv !== '') && (card_cvv.length != 3)) {
console.log('CVV be exactly 3 digits, or leave blank to abort');
}
} while ((card_cvv !== '') && (card_cvv.length != 3));
if (card_cvv == '') {
// exit with error message
console.log("Error: configuration was aborted due to blank CVV digits");
return;
}
// defer saving passcode login details, until all fields are valid
conf.set('passcode', passcode);
conf.set('password', password);
conf.set('card_cvv', card_cvv);
}
console.log(
"\nIf you want to export statements with a friendly name instead of the account\n" +
"number, you can add aliases here.\n" +
"Press enter to continue if you don't need this or once you're finished.\n",
);
var account, alias;
var aliases = {};
while (true) {
account = prompt('Enter an account number: ');
if (!account) {
break;
}
alias = prompt('Enter friendly label: ');
if (!alias) {
break;
}
aliases[account] = alias;
}
conf.set('aliases', aliases);
console.log('\nBarclayscrape is now configured.');
console.log('Credentials were saved to: ' + conf.path);
});
program.parse(process.argv);
function exportLabel(account) {
let aliases = conf.get('aliases') || {};
return aliases[account.number] || account.number;
}
async function auth() {
if (!(conf.has('surname') && conf.has('membershipno'))) {
console.error(
'Barclayscrape has not been configured. Please run `barclayscrape config`',
);
program.help();
}
if (!(program.otp || program.motp || program.plogin)) {
console.error('Must specify either --otp, --motp or --plogin');
program.help();
}
if (program.otp && program.otp.length != 8) {
console.error('OTP should be 8 characters long');
program.help();
}
if (program.motp && program.motp.length != 8) {
program.motp = prompt('Enter your 8 digit mobile PIN sentry code: ');
}
// The --no-sandbox argument is required here for this to run on certain kernels
// and containerised setups. My understanding is that disabling sandboxing shouldn't
// cause a security issue as we're only using one tab anyway.
const sess = await session.launch({
headless: program.headless,
args: ['--no-sandbox'],
});
try {
if (program.otp) {
await sess.loginOTP({
surname: conf.get('surname'),
membershipno: conf.get('membershipno'),
card_digits: conf.get('card_digits'),
otp: program.otp,
});
} else if (program.motp) {
await sess.loginMOTP({
surname: conf.get('surname'),
membershipno: conf.get('membershipno'),
motp: program.motp,
});
} else if (program.plogin) {
await sess.loginPasscode({
surname: conf.get('surname'),
membershipno: conf.get('membershipno'),
passcode: conf.get('passcode'),
password: conf.get('password'),
card_digits: conf.get('card_digits'),
card_cvv: conf.get('card_cvv'),
});
}
} catch (err) {
try {
await sess.close();
} catch (e) {}
throw err;
}
return sess;
}