Last active
December 29, 2018 10:23
-
-
Save meyt/b78e1fc423ace772791184fb3857a977 to your computer and use it in GitHub Desktop.
Extract TLD list JSON from IANA database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Requirements: | |
* - Node v10 | |
* - punycode.js | |
*/ | |
const request = require('request'); | |
const punycode = require('punycode'); | |
const tldDbUrl = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt'; | |
// wrap a request in an promise | |
function downloadPage(url) { | |
return new Promise((resolve, reject) => { | |
request(url, (error, response, body) => { | |
if (error) reject(error); | |
if (response.statusCode != 200) { | |
reject('Invalid status code <' + response.statusCode + '>'); | |
} | |
resolve(body); | |
}); | |
}); | |
} | |
async function main() { | |
const content = await downloadPage(tldDbUrl); | |
const result = []; | |
const items = content.split('\n') | |
delete items[0]; | |
delete items[items.length - 1]; | |
items.forEach(item => { | |
if (item.startsWith('XN--')) { | |
result.push(punycode.decode(item.substr(4))) | |
} else { | |
result.push(item) | |
} | |
}) | |
console.table(JSON.stringify(result)) | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment