-
Notifications
You must be signed in to change notification settings - Fork 0
/
fineco.js
73 lines (66 loc) · 1.87 KB
/
fineco.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
export async function getFinecoMovements() {
return login(process.env.FINECO_USER_ID, process.env.FINECO_PASSWORD)
.then(extractSessionID)
.then(getMovements)
.then(filterCardMovements);
}
async function login(userId, password) {
return fetch(
'https://public-api.finecobank.com/v1/public/authentications/web/login?sca=true',
{
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
},
body: JSON.stringify({ userId, password }),
},
);
}
function extractSessionID(res) {
const cookies = res.headers.getSetCookie();
return cookies
.find((c) => c.startsWith('gsessionid'))
?.split(';')?.[0]
?.split('=')?.[1];
}
async function getMovements(gsessionid) {
const now = new Date();
const dateFrom =
process.env.FINECO_DATE_FROM ||
toISO(new Date(now.setDate(now.getDate() - 30)));
const dateTo = process.env.FINECO_DATE_TO || toISO(new Date());
console.log(`Import transaction from ${dateFrom} to ${dateTo}`);
return fetch(
'https://private-api.finecobank.com/v2/private/accounts-and-cards/movements',
{
method: 'POST',
headers: {
cookie: `gsessionid=${gsessionid};`,
'content-type': 'application/json',
'x-account-index': '0',
'x-dossier-index': '0',
},
body: JSON.stringify({
dateFrom,
dateTo,
offset: 0,
limit: 999,
keyword: '',
type: ['MOVIMENTO_CARTE', 'TRADING'],
}),
},
)
.then((res) => res.json())
.then((m) => m.movimenti);
}
function toISO(date) {
return date.toISOString().split('T')[0];
}
function filterCardMovements(movements) {
const card = movements.filter((m) => m.tipoMovimento === 'MOVIMENTO_CARTE');
console.log(
`Found ${movements.length} movements, of which ${card.length} are from card`,
);
return card;
}