Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Last active Apr 1, 2020
Embed
What would you like to do?
Simple script to get latest COVID-19 update in Timor-Leste
// Simple script to get latest COVID-19 update in Timor-Leste.
// Use it with:
// node ./covid-19.tl.js
const { request } = require('https');
const { promisify } = require('util');
const requestAsync = promisify((options, callback) => {
const req = request(options, res => {
if (!res.statusCode) {
callback(null, null);
return;
}
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () =>
callback(null, {
statusCode: res.statusCode,
header: res.headers,
data: data.trim(),
}),
);
});
req.on('error', err => callback(err, null));
if (options.body) {
req.write(options.body);
}
req.end();
});
(async () => {
try {
const url =
'https://covid19.gov.tl/sites/default/files/covid19/timoleste_municipalities_map1.csv';
const { data } = await requestAsync(url);
const stack = data.split('\r\n');
const head = stack
.shift()
.split(',')
.reverse();
const result = stack
.map(s => s.split(',').reverse())
.map(v =>
head.reduce(
(a, c, i) =>
Object.assign(
{ [c]: isNaN(Number(v[i])) ? v[i] : Number(v[i]) },
a,
),
{},
),
);
console.table(result);
} catch (error) {
console.error(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment