| 1 | const fs = require('fs'); |
| 2 | |
| 3 | async function run() { |
| 4 | const res = await fetch('https://restcountries.com/v3.1/all'); |
| 5 | const data = await res.json(); |
| 6 | |
| 7 | const COUNTRY_DATA = {}; |
| 8 | for (const country of data) { |
| 9 | if (!country.idd || !country.idd.root) continue; |
| 10 | const root = country.idd.root.replace('+', ''); |
| 11 | const suffixes = country.idd.suffixes || ['']; |
| 12 | |
| 13 | for (const suffix of suffixes) { |
| 14 | const code = root + suffix; |
| 15 | if (!COUNTRY_DATA[code] && country.latlng && country.latlng.length === 2) { |
| 16 | COUNTRY_DATA[code] = { |
| 17 | name: country.name.common, |
| 18 | lat: country.latlng[0], |
| 19 | lng: country.latlng[1] |
| 20 | }; |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | COUNTRY_DATA['1'] = { name: 'United States / Canada', lat: 39.8283, lng: -98.5795 }; |
| 26 | |
| 27 | const out = `const COUNTRY_DATA: Record<string, { name: string, lat: number, lng: number }> = ${JSON.stringify(COUNTRY_DATA, null, 2)};`; |
| 28 | fs.writeFileSync('country_data.ts', out); |
| 29 | console.log("Generated " + Object.keys(COUNTRY_DATA).length + " countries."); |
| 30 | } |
| 31 | run(); |