Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Last active March 17, 2020 15:56
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 davidsharp/afdb3eef7a32637b41d277f6c21c56ca to your computer and use it in GitHub Desktop.
Save davidsharp/afdb3eef7a32637b41d277f6c21c56ca to your computer and use it in GitHub Desktop.
[bitbar] plugin that grabs some localised COVID-19 stats from https://thevirustracker.com
#!/usr/bin/env /usr/local/bin/node
const bitbar = require('bitbar');
const http = require('https');
// change me for your region
const region = 'GB'
http.get(`https://thevirustracker.com/free-api?countryTotal=${region}`, (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.error(error.message);
// Consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
const now = new Date();
const min = now.getMinutes().toString().padStart(2,'0');
const hr = now.getHours().toString().padStart(2,'0');
const parsedData = JSON.parse(rawData);
const data = parsedData.countrydata[0];
bitbar([
{text: `🦠 ${data.total_cases}`},
bitbar.separator,
...(data.total_new_cases_today||data.total_new_deaths_today?[
{text: `New cases today: ${data.total_new_cases_today}`},
{text: `New deaths today: ${data.total_new_deaths_today}`}
]:[{text:'No new cases today'}]),
bitbar.separator,
{text: `Recovered: ${data.total_recovered}`},
{text: `Deaths: ${data.total_deaths}`},
{text: `Active cases: ${data.total_active_cases}`},
{text: `Serious cases: ${data.total_serious_cases}`},
bitbar.separator,
{text: `Stats for ${region}`},
{text: `Powered by thevirustracker.com`, href: data.info.source},
{text: `Retrieved at ${hr}:${min}`},
])
} catch (e) {
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment