import fs from "node:fs"; const base = "3961023"; const FILE_NAME = `output_${base}.json`; const sleep = (timeout: number) => new Promise((res, rej) => { setTimeout(() => { res("ok"); }, timeout); }); (async () => { const { default: Isikukood } = require("./isikukood.js"); const MAX = 10000; let valid = []; for (let i = 0; i <= MAX; i += 1) { const current = i.toString().padStart(4, "0"); const full = base + current; const isValid = new Isikukood(full).validate(); if (!isValid) { continue; } valid.push(full); } const existingList: [string, string][] = []; try { const existingData = fs.readFileSync(FILE_NAME); const existingDataMapped = JSON.parse(existingData.toString()) as [string, string][]; existingDataMapped.forEach((data) => existingList.push(data)) } catch {} const responses = existingList; const writeToFile = () => { fs.writeFileSync(FILE_NAME, JSON.stringify(responses, undefined, 2)); }; const sleepBetweenSuccesses = async () => { console.debug("Got success response, sleeping..."); await sleep(2_500); }; const sleepBetweenErrors = async () => { console.debug("Got ratelimited, sleeping..."); await sleep(7_500); }; for (let validEntry of valid) { if (existingList.find(([idCode]) => idCode === validEntry)) { console.debug("Skipping, already fetched data for idCode=" + validEntry); continue; } console.debug("Fetching data for idCode=" + validEntry); const response = await fetch("https://isikukood.ee/" + validEntry); const responseText = await response.text(); if (responseText.includes("No scanning allowed, try again later.")) { await sleepBetweenErrors(); } else { if (responseText.startsWith("N/A")) { console.debug("No person exists with idCode=" + validEntry); responses.push([validEntry, "INVALID"] as [string, string]); } else { responses.push([ validEntry, responseText.substring(0, responseText.indexOf("\n\n")) ] as [string, string]); } writeToFile(); await sleepBetweenSuccesses(); } } console.debug("Writing results to file"); })();