-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (120 loc) · 5.25 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
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
import fs from "fs";
import { parse } from "node-html-parser";
import axios from "axios";
let HOST = "https://id.wikipedia.org";
const URL = `${HOST}/wiki/Daftar_kabupaten_dan_kota_di_Indonesia`;
let provinceList = [];
let provinceNow = 0;
let cityList = [];
let outputData = [];
let getProvinces = fs.readFileSync("./data/provinces.json", 'utf-8');
let provinces = JSON.parse(getProvinces);
let getCities = fs.readFileSync("./data/cities.json", 'utf-8');
let cities = JSON.parse(getCities);
if(!fs.existsSync('./images')) {
fs.mkdirSync('images')
}
function ext(url) {
return url.split(/[#?]/)[0].split('.').pop().trim().toLowerCase();
}
function getProvinceCodeByName(name) {
if(!name) return null;
let province = provinces.find(x => x.name.toLowerCase() === name.replace("Daerah Khusus Ibukota", "DKI").toLowerCase());
return province ? province.code : null;
}
function getCityCodeByName(name) {
if(!name) return null;
let city = cities.find(
(x) =>
x.name.toLowerCase() ===
name
.replace("Tanjungbalai", "Tanjung Balai")
.replace("Pagaralam", "Pagar Alam")
.replace("Mukomuko", "Muko Muko")
.replace("Pangkalpinang", "Pangkal Pinang")
.replace("Tanjungpinang", "Tanjung Pinang")
.replace("Administrasi Kepulauan", "ADM. KEP.")
.replace("Administrasi", "ADM.")
.replace("Kabupaten Timor Tengah Selatan", "Kab Timor Tengah Selatan")
.replace("Palangka Raya", "Palangkaraya")
.replace("Una-Una", "Una Una")
.replace("Tolitoli", "Toli Toli")
.replace("Pangkajene dan Kepulauan", "Pangkajene Kepulauan")
.replace("Parepare", "Pare Pare")
.replace("Baubau", "Bau Bau")
.replace("Fakfak", "Fak Fak")
.toLowerCase()
);
return city ? city.code : null;
}
axios.get(URL).then((response) => {
let document = parse(response.data);
document.querySelectorAll("h3 > span.mw-headline").forEach((m, idm) => {
provinceList.push(m.textContent)
});
document.querySelectorAll("table.wikitable.sortable").forEach((x, idx) => {
if (idx === 0) return;
x.querySelectorAll("tbody > tr:nth-child(1)").forEach(async(m, idm) => {
let name = provinceList[provinceNow];
let code = getProvinceCodeByName(name);
if(code) {
axios.get(HOST + m.querySelector("th:nth-child(9) > figure > a").getAttribute("href")).then((x) => {
let berkasDoc = parse(x.data);
let pngUrl = berkasDoc.querySelector("#file > div > span > a:last-child").getAttribute("href");
axios.get("https:" + pngUrl, { responseType: 'arraybuffer' })
.then((response) => {
fs.writeFileSync(`./images/${code}.png`, response.data);
outputData.push({ code, path: `/images/${code}.png` });
console.log(`[PROVINCE] [${code}] ${name} done.`);
})
.catch((error) => {
console.error('Error downloading PNG file:', error.message);
});
})
}
provinceNow++
});
x.querySelectorAll("tbody > tr").forEach((m, idm) => {
if(idm === 0) return;
let name;
m.querySelectorAll("td").forEach((j, idj) => {
if(idj === 1) {
name = j.querySelector("a").textContent;
}
if(idj === 8) {
let cityCode = getCityCodeByName(name);
let ahref = j.querySelector("figure > a");
if(cityCode && ahref) {
cityList.push({ name, code: cityCode, url: ahref.getAttribute("href") });
}
}
})
})
});
}).then(() => {
const downloadCityLogo = (idx) => {
let city = cityList[idx];
if(city) {
axios.get(HOST + city.url).then((x) => {
let berkasDoc = parse(x.data);
let pngEl = berkasDoc.querySelector("#file > div > span > a:last-child");
if(!pngEl) pngEl = berkasDoc.querySelector("#mw-content-text > div.mw-content-ltr.fullMedia > p > a");
let pngUrl = pngEl.getAttribute("href")
axios.get("https:" + pngUrl, { responseType: 'arraybuffer' })
.then((response) => {
fs.writeFileSync(`./images/${city.code}.${ext(pngUrl)}`, response.data);
outputData.push({ code: city.code, path: `/images/${city.code}.${ext(pngUrl)}` });
console.log(`[CITY] [${city.code}] ${city.name} done.`);
downloadCityLogo(idx + 1);
})
.catch((error) => {
console.error('Error downloading PNG file:', error.message);
});
})
} else {
fs.writeFileSync('./data/output.json', JSON.stringify(outputData));
return;
}
}
downloadCityLogo(0);
})