Last active
April 20, 2020 10:17
-
-
Save davidsharp/e3b3839b2b2d6fe2d7ab71155ed78133 to your computer and use it in GitHub Desktop.
bitbar plugin that grabs some localised COVID-19 stats from https://corona.lmao.ninja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env /usr/local/bin/node | |
const bitbar = require('bitbar'); | |
const http = require('https'); | |
// change me for your country | |
const region = 'UK' | |
http.get(`https://corona.lmao.ninja/v2/countries/${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 parsedData = JSON.parse(rawData); | |
const data = parsedData; | |
const now = new Date(data.updated); | |
const min = now.getMinutes().toString().padStart(2,'0'); | |
const hr = now.getHours().toString().padStart(2,'0'); | |
bitbar([ | |
{text: `🦠 ${data.cases}`}, | |
bitbar.separator, | |
...(data.todayCases||data.todayDeaths?[ | |
{text: `New cases today: ${data.todayCases}`}, | |
{text: `New deaths today: ${data.todayDeaths}`} | |
]:[{text:'No new cases reported today'}]), | |
bitbar.separator, | |
...(data.recovered?[{text: `Recovered: ${data.recovered}`}]:[]), | |
{text: `Deaths: ${data.deaths}`}, | |
{text: `Active cases: ${data.active}`}, | |
{text: `Serious cases: ${data.critical}`}, | |
{text: `Cases per million: ${data.casesPerOneMillion}`}, | |
{text: `Deaths per million: ${data.deathsPerOneMillion}`}, | |
bitbar.separator, | |
{text: `Stats for ${data.countryInfo.iso2}`}, | |
{text: `Powered by corona.lmao.ninja`, href: `https://corona.lmao.ninja/v2/countries/`}, | |
{text: `Data last updated 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
Forked from my previous COVID-19 BitBar plugin, as the site providing the data has since gone down