-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
175 lines (150 loc) · 9.89 KB
/
main.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
const axios = require("axios");
const fs = require('fs');
const Main = async () => {
try {
const provinces_sql = []
const regencies_sql = []
const districts_sql = []
const villages_sql = []
const provinces_json = []
const regencies_json = []
const districts_json = []
const villages_json = []
const provinces = await axios.get("https://sig.bps.go.id/rest-bridging-dagri/getwilayah?level=provinsi&parent=0")
for(let p = 0; p < provinces.data.length; p++){
provinces_sql.push(`(null, "${provinces.data[p].kode_dagri}", "${provinces.data[p].nama_dagri.replace('"', "'")}")`)
provinces_json.push({id: p+1, code: provinces.data[p].kode_dagri, name: provinces.data[p].nama_dagri.replace('"', "'")})
const province_dir = `docs/json-split/${provinces.data[p].kode_dagri}/`
if (!fs.existsSync(province_dir)){
fs.mkdirSync(province_dir);
}
fs.writeFile(province_dir+"index.json", JSON.stringify({
code: provinces.data[p].kode_dagri,
name: provinces.data[p].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
fs.writeFile(`docs/json-single/${provinces.data[p].kode_dagri}.json`, JSON.stringify({
code: provinces.data[p].kode_dagri,
name: provinces.data[p].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
const regencies = await axios.get(`https://sig.bps.go.id/rest-bridging/getwilayah?level=kabupaten&parent=${provinces.data[p].kode_bps}`)
for(let r = 0; r < regencies.data.length; r++){
regencies_sql.push(`(null, "${regencies.data[r].kode_dagri.replace(/\./g, "")}", "${provinces.data[p].kode_dagri}", "${regencies.data[r].nama_dagri.replace('"', "'")}")`)
regencies_json.push({id: r+1, code: regencies.data[r].kode_dagri.replace(/\./g, ""), province_code: provinces.data[p].kode_dagri, name: regencies.data[r].nama_dagri.replace('"', "'")})
const regency_dir = `${province_dir}/${regencies.data[r].kode_dagri.split(".")[1]}/`
if (!fs.existsSync(regency_dir)){
fs.mkdirSync(regency_dir);
}
fs.writeFile(regency_dir+"index.json", JSON.stringify({
code: regencies.data[r].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
name: regencies.data[r].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
fs.writeFile(`docs/json-single/${regencies.data[r].kode_dagri.replace(/\./g, "")}.json`, JSON.stringify({
code: regencies.data[r].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
name: regencies.data[r].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
const districts = await axios.get(`https://sig.bps.go.id/rest-bridging/getwilayah?level=kecamatan&parent=${regencies.data[r].kode_bps}`)
for(let d = 0; d < districts.data.length; d++){
districts_sql.push(`(null, "${districts.data[d].kode_dagri.replace(/\./g, "")}", "${provinces.data[p].kode_dagri}", "${regencies.data[r].kode_dagri.replace(/\./g, "")}", "${districts.data[d].nama_dagri.replace('"', "'")}")`)
districts_json.push({id: d+1, code: districts.data[d].kode_dagri.replace(/\./g, ""), province_code: provinces.data[p].kode_dagri, regency_code: regencies.data[r].kode_dagri.replace(/\./g, ""), name: districts.data[d].nama_dagri.replace('"', "'")})
const district_dir = `${regency_dir}/${districts.data[d].kode_dagri.split(".")[2]}/`
if (!fs.existsSync(district_dir)){
fs.mkdirSync(district_dir);
}
fs.writeFile(district_dir+"index.json", JSON.stringify({
code: districts.data[d].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
regency_code: regencies.data[r].kode_dagri.replace(/\./g, ""),
name: districts.data[d].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
fs.writeFile(`docs/json-single/${districts.data[d].kode_dagri.replace(/\./g, "")}.json`, JSON.stringify({
code: districts.data[d].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
regency_code: regencies.data[r].kode_dagri.replace(/\./g, ""),
name: districts.data[d].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
const villages = await axios.get(`https://sig.bps.go.id/rest-bridging/getwilayah?level=desa&parent=${districts.data[d].kode_bps}`)
for(let v = 0; v < villages.data.length; v++){
villages_sql.push(`(null, "${villages.data[v].kode_dagri.replace(/\./g, "")}", "${provinces.data[p].kode_dagri}", "${regencies.data[r].kode_dagri.replace(/\./g, "")}", "${districts.data[d].kode_dagri.replace(/\./g, "")}", "${villages.data[v].nama_dagri.replace('"', "'")}")`)
villages_json.push({id: v+1, code: villages.data[v].kode_dagri.replace(/\./g, ""), province_code: provinces.data[p].kode_dagri, regency_code:regencies.data[r].kode_dagri.replace(/\./g, ""), district_code: districts.data[d].kode_dagri.replace(/\./g, ""), name: villages.data[v].nama_dagri.replace('"', "'")})
fs.writeFile(district_dir+`${villages.data[v].kode_dagri.split(".")[3]}.json`, JSON.stringify({
code: villages.data[v].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
regency_code: regencies.data[r].kode_dagri.replace(/\./g, ""),
district_code: districts.data[d].kode_dagri.replace(/\./g, ""),
name: villages.data[v].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
fs.writeFile(`docs/json-single/${villages.data[v].kode_dagri.replace(/\./g, "")}.json`, JSON.stringify({
code: villages.data[v].kode_dagri.replace(/\./g, ""),
province_code: provinces.data[p].kode_dagri,
regency_code: regencies.data[r].kode_dagri.replace(/\./g, ""),
district_code: districts.data[d].kode_dagri.replace(/\./g, ""),
name: villages.data[v].nama_dagri.replace('"', "'")
}), function (err) {
if (err) throw err;
});
setTimeout(function() {
console.log(provinces.data[p].nama_dagri.replace('"', "'"), "|", regencies.data[r].nama_dagri.replace('"', "'"), "|", districts.data[d].nama_dagri.replace('"', "'"), "|", villages.data[v].nama_dagri.replace('"', "'"))
}, 500);
}
}
}
}
// SQL
const insert_province_sql = `INSERT INTO master_provinces (id, code, name) values ${provinces_sql.join(",")}`
fs.writeFile('docs/mysql/provinces.sql', insert_province_sql, function (err) {
if (err) throw err;
console.log('Provinces sql file is created successfully.');
});
const insert_regency_sql = `INSERT INTO master_regencies (id, code, province_code, name) values ${regencies_sql.join(",")}`
fs.writeFile('docs/mysql/regencies.sql', insert_regency_sql, function (err) {
if (err) throw err;
console.log('Regencies sql file is created successfully.');
});
const insert_district_sql = `INSERT INTO master_districts (id, code, province_code, regency_code, name) values ${districts_sql.join(",")}`
fs.writeFile('docs/mysql/districts.sql', insert_district_sql, function (err) {
if (err) throw err;
console.log('Districts sql file is created successfully.');
});
const insert_village_sql = `INSERT INTO master_villages (id, code, province_code, regency_code, district_code, name) values ${villages_sql.join(",")}`
fs.writeFile('docs/mysql/villages.sql', insert_village_sql, function (err) {
if (err) throw err;
console.log('Villages sql file is created successfully.');
});
// JSON
fs.writeFile('docs/json-full/provinces.json', JSON.stringify(provinces_json), function (err) {
if (err) throw err;
console.log('Provinces JSON file is created successfully.');
});
fs.writeFile('docs/json-full/regencies.json', JSON.stringify(regencies_json), function (err) {
if (err) throw err;
console.log('Regencies JSON file is created successfully.');
});
fs.writeFile('docs/json-full/districts.json', JSON.stringify(districts_json), function (err) {
if (err) throw err;
console.log('Districts JSON file is created successfully.');
});
fs.writeFile('docs/json-full/villages.json', JSON.stringify(villages_json), function (err) {
if (err) throw err;
console.log('Villages JSON file is created successfully.');
});
}catch (e) {
console.log(e)
}
}
Main();