Skip to content

Instantly share code, notes, and snippets.

@stephenmathieson
Created March 9, 2022 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenmathieson/96bee738447d0688d2b99ed53e5c4663 to your computer and use it in GitHub Desktop.
Save stephenmathieson/96bee738447d0688d2b99ed53e5c4663 to your computer and use it in GitHub Desktop.
Is a well-known person alive?
const { JSDOM } = require('jsdom');
const got = require('got').default;
const WIKIPEDIA_URL = 'https://en.wikipedia.org/wiki';
/**
* Check if the person with `firstName` and `lastName` is currently alive.
*
* @param {string} firstName
* @param {string} lastName
*/
const isPersonAlive = async (firstName, lastName) => {
const res = await got(`${WIKIPEDIA_URL}/${firstName}_${lastName}`);
const dom = new JSDOM(res.body);
const { document } = dom.window;
const ths = document.querySelectorAll('table.vcard th[scope=row]');
for (const th of ths) {
const text = th.innerHTML.trim();
if (text === 'Died') {
return false;
}
}
return true;
};
(async () => {
const [hawking, putin] = await Promise.all([
isPersonAlive('Stephen', 'Hawking'),
isPersonAlive('Vladimir', 'Putin'),
]);
console.log('Stephen Hawking is', hawking ? 'alive' : 'dead');
console.log('Vladimir Putin is', putin ? 'alive' : 'dead');
})();
{
"name": "is-person-alive",
"version": "1.0.0",
"main": "is-person-alive.js",
"license": "MIT",
"dependencies": {
"got": "11",
"jsdom": "19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment