-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
108 lines (90 loc) · 3.34 KB
/
index.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
'use strict';
const fs = require('fs');
const https = require('https');
const year = process.argv[2];
if (!year) {
console.error('Usage: node index.js <year>');
console.error(' example: node index.js 2016');
process.exit(1);
}
const mkdirp = path => {
try {
fs.mkdirSync(path);
} catch(e) {
if (e.code !== 'EEXIST') throw e;
}
};
const months = Array.apply(null, { length: 12 }).map(Number.call, Number).map(n => n + 1);
months.forEach(m => {
console.log(`Downloading monthly recap ${m}`);
const result = require('child_process').spawnSync('curl', [
`https://www.car2go.com/rest/api/statements/own/sorted/${year},${m},1,0,0,0,UTC?_=1453322332055`,
'--compressed',
'-b', 'cookies.txt'
]);
if (result.status !== 0) {
console.error(result.error);
return;
}
fs.writeFileSync(`${m}.json`, result.stdout);
console.log(`Downloaded file ${m}.json`);
});
const fileNames = months.map(i => `${i}.json`);
const downloadDir = 'invoices';
mkdirp(downloadDir);
const s = fileNames.reduce((sum, fileName) => {
const f = fs.readFileSync(fileName, 'utf8');
const json = JSON.parse(f);
if (!json.body.StatementHierarchyContainerRTO) {
return sum;
}
// payment via creditcard vs withdrawal
const rides = json.body.StatementHierarchyContainerRTO.paymentprofiles.creditcards ? json.body.StatementHierarchyContainerRTO.paymentprofiles.creditcards.paid : json.body.StatementHierarchyContainerRTO.paymentprofiles.withdrawal.open;
const s = rides.reduce((sum, p) => {
const x = p.amountGross.replace(/EUR (\d+.\d+)/, '$1')
return sum + parseFloat(x);
}, 0);
console.log(`On month ${fileName} you spent ${s.toFixed(2)} €`);
return sum + s;
}, 0);
console.log('\n-----------------------------------------------');
console.log(`In ${year} you spent a total of ${s.toFixed(2)}`);
console.log('-----------------------------------------------\n');
fileNames.forEach(fileName => {
const f = fs.readFileSync(fileName, 'utf8');
const json = JSON.parse(f);
if (!json.body.StatementHierarchyContainerRTO) {
return;
}
// payment via creditcard vs withdrawal
const paidInvoices = json.body.StatementHierarchyContainerRTO.paymentprofiles.creditcards ? json.body.StatementHierarchyContainerRTO.paymentprofiles.creditcards.paid : json.body.StatementHierarchyContainerRTO.paymentprofiles.withdrawal.open;
if (!paidInvoices) {
return;
}
paidInvoices.forEach(invoice => {
const date = invoice.statementDate;
const matches = date.match(/(\d+),(\d+),(\d+),\d+,\d+,\d+,UTC/);
const year = matches[1];
const month = matches[2];
const day = matches[3];
const fileName = `car2go-${year}-${month}-${day}.pdf`;
const output = fs.createWriteStream(`${downloadDir}/${fileName}`)
console.log(`Processing invoice ${invoice.statementId}`);
const curl = require('child_process').spawn('curl', [
`https://www.car2go.com/rest/api/statements/own/pdf/${invoice.statementId}`,
'--compressed',
'-b', 'cookies.txt'
]);
curl.stdout.on('data', data => output.write(data));
curl.stdout.on('end', () => {
output.end();
console.log(`file ${fileName} downloaded to ${downloadDir}`);
});
curl.stderr.on('error', err => console.error(err));
curl.on('exit', code => {
if (code !== 0) {
console.error(`Failed with code ${code}`);
}
});
});
});