Last active
May 24, 2021 15:25
-
-
Save davidsharp/e0bfa84a657ef113b3d59f24767b8314 to your computer and use it in GitHub Desktop.
bitbar plugin that grabs some localised COVID-19 stats from https://disease.sh
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'); | |
const fetch = require('node-fetch') | |
var emojiFlags = require('emoji-flags'); | |
// change me for your country | |
const region = 'UK' | |
http.get(`https://disease.sh/v3/covid-19/countries/${region}`, async (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; | |
} | |
let yesterday = async () => { | |
try{ | |
const rawData = await fetch(`https://disease.sh/v3/covid-19/countries/${region}?yesterday=true`) | |
const data = await rawData.json() | |
return [ | |
{text: `Yesterday's cases: ${data.todayCases.toLocaleString()}`}, | |
{text: `Yesterday's deaths: ${data.todayDeaths.toLocaleString()}`} | |
] | |
} | |
catch(e){return null} | |
} | |
let vaccinations = async () => { | |
try{ | |
const rawData = await fetch(`https://disease.sh/v3/covid-19/vaccine/coverage/countries/${region}?lastdays=1`) | |
const {timeline} = await rawData.json() | |
const vaccinations = Object.values(timeline)[0].toLocaleString() | |
// this is 1st + 2nd in the UK | |
return ({text: `Vaccinations 💉 : ${vaccinations}`}) | |
} | |
catch(e){ | |
return {text:e.toString()} | |
} | |
} | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', (chunk) => { rawData += chunk; }); | |
res.on('end', async () => { | |
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.toLocaleString()}`}, | |
bitbar.separator, | |
...(data.todayCases||data.todayDeaths?[ | |
{text: `New cases today: ${data.todayCases.toLocaleString()}`}, | |
{text: `New deaths today: ${data.todayDeaths.toLocaleString()}`} | |
]:( | |
await yesterday() || | |
[{text:'No new cases reported today'}] | |
)), | |
bitbar.separator, | |
...(data.recovered?[{text: `Recovered: ${data.recovered.toLocaleString()}`}]:[]), | |
{text: `Deaths: ${data.deaths.toLocaleString()}`}, | |
{text: `Active cases: ${data.active.toLocaleString()}`}, | |
{text: `Serious cases: ${data.critical.toLocaleString()}`}, | |
{text: `Cases per million: ${data.casesPerOneMillion.toLocaleString()}`}, | |
{text: `Deaths per million: ${data.deathsPerOneMillion.toLocaleString()}`}, | |
bitbar.separator, | |
await vaccinations(), | |
bitbar.separator, | |
{text: `Stats for ${data.countryInfo.iso2} ${emojiFlags.countryCode(data.countryInfo.iso2).emoji}`}, | |
{text: `Powered by disease.sh`, href: `https://disease.sh`}, | |
{text: `Data last updated at ${hr}:${min}`} | |
]) | |
} catch (e) { | |
console.error(e.message); | |
} | |
}); | |
}).on('error', (e) => { | |
console.error(`Got error: ${e.message}`); | |
}); |
Newest update is more hopeful, added vaccinations total as a stat (UK data seems to be 1st dose vaccinations specifically)
This is an updated version of https://gist.github.com/davidsharp/e3b3839b2b2d6fe2d7ab71155ed78133 (note for posterity, removing long link from description)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New version requires node-fetch too, but fetches yesterday's stats if today's haven't been added yet