-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (62 loc) · 2.17 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
const fs = require('fs');
const path = require('path');
const csv = require('csvtojson');
const download = require('download');
const mkdirp = require('mkdirp-promise');
const { sourceUrl, csvPath, jsonPath } = require('./package').config;
function parseCsv(source) {
const mapData = [];
const listData = [];
const toStr = (input, space = 2) => JSON.stringify(input, null, space);
return csv()
.fromFile(source)
.on('json', json => {
mapData.push(`${toStr(json['car_park_no'])}: ${toStr(json)}`);
listData.push(toStr(json));
})
.on('done', error => {
if (error) return console.error(error);
mkdirp(jsonPath)
.then(() => {
const dataset = records =>
toStr({
total: listData.length,
records: JSON.parse(records)
});
const mapDataset = dataset(`\{${mapData.join(',')}\}`);
const listDataset = dataset(`\[${listData.join(',')}\]`);
const sourceName = path.basename(source, path.extname(source));
const mapDataFile = `${jsonPath}/${sourceName}-map.json`;
const listDataFile = `${jsonPath}/${sourceName}-list.json`;
fs.writeFileSync(mapDataFile, mapDataset, 'utf8');
console.log('Generated file: ', mapDataFile);
fs.writeFileSync(listDataFile, listDataset, 'utf8');
console.log('Generated file: ', listDataFile);
})
.catch(console.error);
});
}
function downloadCsv() {
const options = {
headers: { accept: 'application/zip' },
mode: '755',
strip: 1,
extract: true
};
console.log('Downloding...');
return download(sourceUrl, csvPath, options)
.then(data => {
console.log('Downloded and extracted CSV.');
const files = (data || []).filter(file => /\.csv$/i.test(file.path));
if (!files.length) throw new Error('HDB Carpark CSV not found.');
// note: assumed there is always one csv file in zip folder
const csvFile = `${csvPath}/${files[0].path}`;
console.log('CSV file: ', csvFile);
return parseCsv(csvFile);
})
.catch(error => {
console.error(error);
process.exit(1);
});
}
downloadCsv();