-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.ts
31 lines (28 loc) · 966 Bytes
/
load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// @file load.ts
// @author David Hammond
// @date 13 Nov 2020
//
import axios from 'axios';
import { promises as fs } from 'fs';
import { JSDOM } from 'jsdom';
// Scrape country population data from https://www.worldometers.info/
const loadPopulationData = async (): Promise<{[key: string]: number}> => {
const html = (await axios.get('https://www.worldometers.info/world-population/population-by-country/'
, { timeout: 2000})).data;
const { window } = new JSDOM(html);
const $ = require('jquery')(window) as JQueryStatic;
const data: {[key: string]: number} = {};
$('tr').each(function() {
const td = $(this).find('td');
if ($(td[0]).html()) {
data[$(td[1]).text()] = Number($(td[2]).text().replace(/,/g,''));
}
});
return data;
};
// Load and save data
loadPopulationData()
.then(data => fs.writeFile('src/data/population.json', JSON.stringify(data)))
.then(() => console.log('done!'))
.catch(err => console.error(err));