-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
348 lines (294 loc) · 7.8 KB
/
api.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var request = require("request");
var baseUrl = "https://app.caixadirecta.cgd.pt/apps/r/";
var appHeaders = {
"X-CGD-APP-Device": "w8",
"X-CGD-APP-Version": "3.2"
};
function handle_error(handler, cb) {
return function (err, response, body) {
if(err) return cb(err);
if(response.statusCode != 200) {
console.warn(body);
return cb("HTTP status code " + response.statusCode, null);
}
handler(response, body);
};
}
function pad_number(val, length, padding) {
padding = padding || "0";
val = parseInt(val);
var isNegative = val < 0;
if(isNegative) val = -val;
val = val.toString();
while(val.length < length) {
val = padding + val;
}
return isNegative ? "-" + val : val;
}
function format_date(date, hour) {
hour = hour != null ? hour : "00:00:00";
var year = pad_number(date.getFullYear(), 4);
var month = pad_number(date.getMonth() + 1, 2);
var day = pad_number(date.getDate(), 2);
return year + "-" + month + "-" + day + " " + hour;
}
function parse_date(val) {
return val.match(/^(\d{4}-\d{2}-\d{2})/)[1];
}
function string_to_date(val) {
var match = val.match(/^(\d{4})-(\d{2})-(\d{2})/);
return new Date(parseInt(match[1]), parseInt(match[2]) - 1, parseInt(match[3]));
}
function compare_dates(first, second) {
var diff = first.getFullYear() - second.getFullYear();
if(diff == 0) {
diff = first.getMonth() - second.getMonth();
if(diff == 0) {
diff = first.getDate() - second.getDate();
}
}
return diff;
}
function parse_money(val) {
return pad_number(val, 3).replace(/^(-?\d*)(\d\d)$/, "$1,$2");
}
exports.authenticate = function(username, password, cb) {
console.warn("Sending authentication request");
var auth = request.jar();
request({
url: baseUrl + "co/li?u=" + username,
headers: appHeaders,
jar: auth,
json: {},
auth: {
user: username,
pass: password
}
}, handle_error(authentication_complete, cb));
function authentication_complete(response, body) {
console.warn("Successfully authenticated");
cb(null, auth);
}
}
exports.get_accounts = function(auth, cb) {
var pendingRequests = 2;
var accounts = [];
console.warn("Requesting account list");
request({
url: baseUrl + "pg/l",
headers: appHeaders,
jar: auth,
json: {}
}, handle_error(accounts_retrieved, cb));
console.warn("Requesting card list");
request({
url: baseUrl + "c/dc",
headers: appHeaders,
jar: auth,
json: {}
}, handle_error(cards_retrieved, cb));
function accounts_retrieved(response, body) {
console.warn("Account list retrieved");
for(var name in body) {
body[name].forEach(function(e) {
e.ttl.forEach(function(l) {
l.cttl.forEach(function(a) {
accounts.push({
id: a.cntk,
name: a.cnt,
type: a.tc,
balance: parse_money(a.scnt)
});
});
});
});
}
if(--pendingRequests == 0) cb(null, accounts);
}
function cards_retrieved(response, body) {
console.warn("Card list retrieved");
body.cl.forEach(function(c) {
accounts.push({
id: c.ccc,
name: c.dcc,
type: c.tc,
balance: null
});
});
if(--pendingRequests == 0) cb(null, accounts);
}
}
exports.get_movements = function(auth, account, startDate, endDate, chunkCb, cb) {
if(/^PT /.test(account)) {
get_account_movements(auth, account, startDate, endDate, chunkCb, cb);
} else {
get_card_movements(auth, account, startDate, endDate, chunkCb, cb);
}
}
function get_account_movements(auth, account, startDate, endDate, chunkCb, cb) {
console.warn("Requesting movements of account " + account + " between " + format_date(startDate, "") + "and " + format_date(endDate, ""));
var query = {
cnt: account,
dti: format_date(startDate, "00:00:00"),
dtf: format_date(endDate, "23:59:59")
};
retrieve_next_page();
function retrieve_next_page() {
request({
url: baseUrl + "cnt/dc/m",
headers: appHeaders,
jar: auth,
method: "POST",
json: query
}, handle_error(movements_retrieved, cb));
}
function movements_retrieved(response, body) {
console.warn("Movement chunk retrieved");
var movements = null;
if(body.lmovdpe != null) {
movements = [];
var today = new Date();
body.lmovdpe.forEach(function(m) {
/*
{ moe: 'EUR',
tj: 330,
dta: '2012-07-05 00:00:00',
nsd: 6,
mcnt: 50000,
dtpv: '2014-07-05 00:00:00',
dtij: null,
mit: null,
dst: null,
dspe: null,
dsp: '730 dias',
dspr: 'CaixaNet24M Nº 6',
dsr: null,
dsttc: null,
msgb1: null,
msgb2: null,
msgh: null,
dt: '2012-07-05 00:00:00',
mon: 50000,
des: null,
tpm: null,
saps: 50000 }
*/
movements.push({
description: m.dspr,
date: parse_date(m.dt),
value_date: parse_date(m.dtpv),
number: m.nsd,
debit: "0,00",
credit: parse_money(m.mon),
balance: parse_money(m.saps)
});
var expirationDate = string_to_date(m.dtpv);
if(compare_dates(today, expirationDate) >= 0) {
movements.push({
description: m.dspr,
date: parse_date(m.dt),
value_date: parse_date(m.dtpv),
number: m.nsd,
debit: parse_money(m.mon),
credit: "0,00",
balance: parse_money(m.saps)
});
}
});
} else if(body.lmov != null) {
movements = body.lmov.map(function(m) {
/*
{ tj: 0,
moeo: 'EUR',
monmo: 1180,
dtv: '2013-01-01 00:00:00',
sdaps: 47659,
ndc: 0,
nmv: 732,
estor: '',
apor: 'VR',
dt: '2013-01-01 00:00:00',
mon: 1180,
des: 'Compras LIDL 29 12',
tpm: 'D',
saps: 47659 }
*/
return {
description: m.des,
date: parse_date(m.dt),
value_date: parse_date(m.dtv),
number: m.nmv,
debit: m.tpm == "D" ? parse_money(m.mon) : "0,00",
credit: m.tpm == "C" ? parse_money(m.mon) : "0,00",
balance: parse_money(m.saps)
};
});
} else {
console.error(body);
return cb("Unexpected response");
}
chunkCb(movements);
if(body.lp) {
console.warn("All movements have been retrieved");
cb(null);
} else {
query.pkl = body.pkl;
retrieve_next_page();
}
}
}
function get_card_movements(auth, account, startDate, endDate, chunkCb, cb) {
var monthDate = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
retrieve_next_page();
function retrieve_next_page() {
console.warn("Requesting movements of card " + account + " for month " + format_date(monthDate).substr(0, 7));
request({
url: baseUrl + "c/mc/" + encodeURIComponent(account) + "/" + encodeURIComponent(format_date(monthDate, "00:00:00")),
headers: appHeaders,
jar: auth,
json: {}
}, handle_error(movements_retrieved, cb));
}
function movements_retrieved(response, body) {
console.warn("Movement chunk retrieved");
var movements = [];
if(body.lmov.cl == null) {
console.error(body);
return cb("Unexpected response");
}
body.lmov.cl.push({ mcl: body.lmov.movccl });
body.lmov.cl.forEach(function(g) {
g.mcl.forEach(function(m) {
/*
{
"dt":"2013-04-16 00:00:00",
"cred":321,
"deb":null,
"pnp":null,
"numc":"",
"dtv":"2013-04-16 00:00:00",
"crtd":"10057469750 - EUR - Leve",
"movd":"CASHBACK PARA PPR CARTAO LEVE"
}
*/
movements.push({
description: m.movd,
date: parse_date(m.dt),
value_date: parse_date(m.dtv),
number: null,
debit: m.deb != null ? parse_money(m.deb) : "0,00",
credit: m.cred != null ? parse_money(m.cred) : "0,00",
balance: null
});
});
});
chunkCb(movements);
monthDate = new Date(monthDate.getFullYear(), monthDate.getMonth() + 1, 1);
if(compare_dates(monthDate, endDate) <= 0) {
retrieve_next_page();
} else {
console.warn("All movements have been retrieved");
cb(null);
}
}
}