-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
94 lines (81 loc) · 2.8 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
const cheerio = require("cheerio");
const puppeteer = require("puppeteer");
const util = require("util");
let countItem = async (page, whatToCount) => {
return new Promise(async (resolve, reject) => {
try {
let x = {};
let outerElement = await page.evaluate((whatToCount) => {
// let whatToCount = this.whatToCount;
return document.querySelector(whatToCount).outerHTML;
}, whatToCount);
const $ = cheerio.load(outerElement);
$("option").each(async (i, el) => {
if ($(el).attr("value") !== "") {
x = {
...x,
[$(el).attr("value")]: $(el).text(),
};
}
});
resolve(x);
} catch (error) {
console.log(error.message);
reject(error);
}
});
};
(async function () {
let data = {};
const browser = await puppeteer.launch({
executablePath: "/usr/bin/google-chrome-stable",
headless: false,
});
const page = await browser.newPage();
await page.goto("https://voterlist.election.gov.np/bbvrs1/index_2.php", {
waitUntil: "domcontentloaded",
});
await page.setViewport({ width: 946, height: 957 });
// #state
// #district
// #vdc_mun
// #ward
await page.waitForSelector("#state");
let states = await countItem(page, "#state");
for (i = 0; i < Object.keys(states).length; i++) {
let currentState = states[Object.keys(states)[i]];
// console.log(currentState)
data[currentState] = {};
await page.select("#state", Object.keys(states)[i]);
await new Promise((r) => setTimeout(() => r(), 300));
let districts = await countItem(page, "#district");
for (j = 0; j < Object.keys(districts).length; j++) {
let currentDistrict = districts[Object.keys(districts)[j]];
// console.log(currentDistrict)
data[currentState][currentDistrict] = {};
await page.select("#district", Object.keys(districts)[j]);
await new Promise((r) => setTimeout(() => r(), 300));
let vdcmun = await countItem(page, "#vdc_mun");
for (k = 0; k < Object.keys(vdcmun).length; k++) {
let currentVdcmun = vdcmun[Object.keys(vdcmun)[k]];
// console.log(currentVdcmun)
data[currentState][currentDistrict][currentVdcmun] = [];
await page.select("#vdc_mun", Object.keys(vdcmun)[k]);
await new Promise((r) => setTimeout(() => r(), 300));
let ward = await countItem(page, "#ward");
let currentWard = Object.values(ward);
console.log(currentWard);
data[currentState][currentDistrict][currentVdcmun] = currentWard;
}
}
}
// write to the file
const fs = require("fs");
const write = JSON.stringify(data);
fs.writeFileSync("all_nepal_location.json", write, "utf-8", (err) => {
if (err) {
console.log(err);
}
console.log("JSON written, File saved.");
});
})();