-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (60 loc) · 3.09 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
var fs = require('fs');
var parse = require('csv-parse');
var utils = require('./utils');
var DateTime = require('./DateTime');
const _ = require('underscore');
let provinces = [], districts = [], wards = [];
console.log(`Start running . . . `);
fs.createReadStream('./data/Danh sách cấp tỉnh kèm theo quận huyện, phường xã ___11_07_2021.csv')
.pipe(parse({ delimiter: ',' }))
.on('data', function (row) {
if (row[1] != 'Mã TP') {
// ['Tỉnh Thành Phố,Mã TP,Quận Huyện,Mã QH,Phường Xã,Mã PX,Cấp,Tên Tiếng Anh']
// ['Thành phố Hà Nội,01,Quận Ba Đình,001,Phường Phúc Xá,00001,Phường,']
// console.log(row);
//do something with row
if (_.isUndefined(_.find(provinces, (item) => { return item.code == row[1] }))) {
provinces.push({
"name": utils.extractName(row[0]).name,
"slug": utils.getSlug(utils.extractName(row[0]).name),
"type": utils.getSlug(utils.extractName(row[0]).type),
"name_with_type": row[0],
"code": row[1],
"isDeleted": false
})
}
if (_.isUndefined(_.find(districts, (item) => { return item.code == row[3] }))) {
districts.push({
"name": utils.extractName(row[2]).name,
"type": utils.getSlug(utils.extractName(row[2]).type),
"slug": utils.getSlug(utils.extractName(row[2]).name),
"name_with_type": row[2],
"path": utils.removeHCType(`${row[2]}, ${row[0]}`),
"path_with_type": `${row[2]}, ${row[0]}`,
"code": row[3],
"parent_code": row[1],
"isDeleted": false
})
}
if (row[4].trim() != '') {
wards.push({
"name": utils.extractName(row[4]).name,
"type": utils.getSlug(utils.extractName(row[4]).type),
"slug": utils.getSlug(utils.extractName(row[4]).name),
"name_with_type": row[4],
"path": utils.removeHCType(`${row[4]}, ${row[2]}, ${row[0]}`),
"path_with_type": `${row[4]}, ${row[2]}, ${row[0]}`,
"code": row[5],
"parent_code": row[3],
"isDeleted": false
})
}
}
})
.on('end', function () {
//do something with csvData
fs.writeFileSync('./data/loc_provinces.json', JSON.stringify(provinces, null, 4))
fs.writeFileSync('./data/loc_districts.json', JSON.stringify(districts, null, 4))
fs.writeFileSync('./data/loc_wards.json', JSON.stringify(wards, null, 4))
console.log(`[INFO] Crawl data successful`);
});