-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
77 lines (65 loc) · 2 KB
/
index.ts
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
import puppeteer from "puppeteer";
import dotenv from "dotenv";
import mongoose from "mongoose";
import Country from "./schemas/Country";
import State from "./schemas/State";
import LGA from "./schemas/LGA";
dotenv.config();
(async () => {
try {
const connect = await mongoose.connect(process.env.DB_URI as string);
console.log("connected", connect.connection.host);
} catch (err) {
console.log(
"Oops! Sorry, connection to the DB failed.",
(err as Error)?.message
);
}
})();
(async () => {
try {
// Retrieve country details
const country = await Country.findOne({ name: "Nigeria" });
if (!country) {
throw new Error("Country not found");
}
const countryId = country._id;
// Launch the browser and open a new blank page
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(
"https://en.wikipedia.org/wiki/Local_government_areas_of_Nigeria",
{
waitUntil: "networkidle2",
}
);
// Extract states and LGAs
const statesAndLGAs = await page.$$eval("h3", (states) =>
states.map((state) => {
const h3Text = state.textContent?.split("[")?.[0] || "";
const nextElement = state.nextElementSibling;
const secondNextElement = nextElement?.nextElementSibling;
const cities =
nextElement?.tagName === "LINK" &&
secondNextElement?.tagName === "DIV"
? secondNextElement.textContent?.split("\n").filter(Boolean)
: [];
return { state: h3Text, cities };
})
);
for (const data of statesAndLGAs) {
const state = await State.create({
name: data.state.split(" ")[0],
countryId: countryId,
});
if (data.cities) {
for (const city of data.cities) {
await LGA.create({ name: city, stateId: state._id });
}
}
}
await browser.close();
} catch (error) {
console.error("An error occurred:", error);
}
})();