Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Last active April 1, 2020 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisanalfa/5a4eee6720e71985068bcaef46696aba to your computer and use it in GitHub Desktop.
Save krisanalfa/5a4eee6720e71985068bcaef46696aba to your computer and use it in GitHub Desktop.
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