Skip to content

Instantly share code, notes, and snippets.

@brkydnc
Created October 23, 2022 09:08
Show Gist options
  • Save brkydnc/bad6b7c55acd4836e70299ded1c53ce4 to your computer and use it in GitHub Desktop.
Save brkydnc/bad6b7c55acd4836e70299ded1c53ce4 to your computer and use it in GitHub Desktop.
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const capitalize = str => str.charAt(0).toLocaleUpperCase("tr-TR") + str.slice(1).toLocaleLowerCase("tr-TR");
const parser = new DOMParser();
const fetchDocument = path => fetch("https://www.nufusune.com/" + path)
.then(response => response.text())
.then(text => parser.parseFromString(text, "text/html"));
const indexOfDistricts = await fetchDocument("ilceler");
const anchors = indexOfDistricts.querySelectorAll('td[data-label="İL ADI"] a');
const promises = Array.from(anchors).map(async anchor => {
const href = anchor.getAttribute("href");
const cityId = href.slice(0, -9);
const cityName = capitalize(anchor.textContent.slice(0, -9));
const districts = await fetchDocument(href).then(async document => {
const anchors = document.querySelectorAll('td[data-label="İLÇELERİ"] a');
const promises = Array.from(anchors).map(async anchor => {
const href = anchor.getAttribute("href");
const districtId = href.slice(0, -(21 + cityName.length));
const districtName = capitalize(anchor.textContent);
const neighborhoods = await fetchDocument(href).then(async document => {
const neighborhoods = Array.from(document.querySelectorAll("#ikiye1 li a"))
.map(anchor => capitalize(anchor.textContent.slice(0, -10)));
const villages = Array.from(document.querySelectorAll("#ikiye2 li a"))
.map(anchor => capitalize(anchor.textContent.slice(0, -5)));
return neighborhoods.concat(villages);
});
return { id: districtId, name: districtName, neighborhoods }
})
return await Promise.all(promises);
});
return { id: cityId, name: cityName, districts }
});
const data = await Promise.all(promises);
Deno.writeTextFile("./data.json", JSON.stringify(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment