Skip to content

Instantly share code, notes, and snippets.

@kevinjalbert
Forked from planecore/Coronavirus.js
Last active August 17, 2021 15:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinjalbert/275590b53f8d6b06c5703fad549099f9 to your computer and use it in GitHub Desktop.
Save kevinjalbert/275590b53f8d6b06c5703fad549099f9 to your computer and use it in GitHub Desktop.
Coronavirus Scriptable Widget (For Canadian Provinces)
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: user-md;
// heavily inspired by https://gist.github.com/planecore/e7b4c1e5db2dd28b1a023860e831355e
const province = "ON" // change this to your province
const url = "https://api.covid19tracker.ca/summary/split"
const req = new Request(url)
const res = await req.loadJSON()
const provinceData = res.data.find(element => element.province === province)
const timestamp = new Date().toLocaleTimeString()
if (config.runsInWidget) {
// create and show widget
let widget = createWidget("COVID-19 Stats", `${provinceData.change_cases} Today`, `${provinceData.total_cases} Total`, timestamp, "#53d769")
Script.setWidget(widget)
Script.complete()
} else {
// make table
let table = new UITable()
// add header
let row = new UITableRow()
row.isHeader = true
row.addText(`COVID-19 Stats in ${province}`)
table.addRow(row)
// fill data
table.addRow(createRow("Cases", provinceData.total_cases))
table.addRow(createRow("Today", provinceData.change_cases))
table.addRow(createRow("Deaths", provinceData.total_fatalities))
table.addRow(createRow("Recovered", provinceData.total_recoveries))
table.addRow(createRow("Critical", provinceData.total_criticals))
table.addRow(createRow("Last Updated", timestamp))
if (config.runsWithSiri)
Speech.speak(`There are ${provinceData.total_cases} cases in ${province}, and ${provinceData.change_cases} cases as of ${timestamp} today.`)
// present table
table.present()
}
function createRow(title, number) {
let row = new UITableRow()
row.addText(title)
row.addText((number || "null").toString()).rightAligned()
return row
}
function createWidget(pretitle, title, subtitle, timestamp, color) {
let w = new ListWidget()
w.backgroundColor = new Color(color)
let preTxt = w.addText(pretitle)
preTxt.textColor = Color.white()
preTxt.textOpacity = 0.8
preTxt.font = Font.systemFont(16)
w.addSpacer(5)
let titleTxt = w.addText(title)
titleTxt.textColor = Color.white()
titleTxt.font = Font.systemFont(22)
w.addSpacer(5)
let subTxt = w.addText(subtitle)
subTxt.textColor = Color.white()
subTxt.textOpacity = 0.8
subTxt.font = Font.systemFont(18)
w.addSpacer(5)
let updatedAtText = w.addText(`Updated at: ${timestamp}`)
updatedAtText.textColor = Color.white()
updatedAtText.textOpacity = 0.6
updatedAtText.font = Font.systemFont(12)
return w
}
@Wdavery
Copy link

Wdavery commented Oct 14, 2020

Set out to do exactly this today, of course I finished before seeing your version...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment