Skip to content

Instantly share code, notes, and snippets.

@rsalzer
Forked from planecore/Coronavirus.js
Last active May 25, 2021 16:12
Show Gist options
  • Save rsalzer/957a66abd4ccba10d4a833431214b6be to your computer and use it in GitHub Desktop.
Save rsalzer/957a66abd4ccba10d4a833431214b6be to your computer and use it in GitHub Desktop.
Coronavirus Scriptable Widget
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
const canton = "ZH"
const url = `https://covid19-rest.herokuapp.com/api/openzh/v1/country/CH/area/${canton}`
const req = new Request(url)
const res = await req.loadJSON()
const today = res.records[res.records.length-1]
const yesterday = res.records[res.records.length-2]
const twodaysago = res.records[res.records.length-3]
const diffToday = today.ncumul_conf - yesterday.ncumul_conf
const diffYday = yesterday.ncumul_conf - twodaysago.ncumul_conf
if (config.runsInWidget) {
// create and show widget
let widget = createWidget(`Coronavirus in ${canton}`, `${today.date}: +${diffToday}`, `${yesterday.date}: +${diffYday}`, "#0066CC")
Script.setWidget(widget)
Script.complete()
} else {
// make table
let table = new UITable()
// add header
let row = new UITableRow()
row.isHeader = true
row.addText(`Coronavirus Stats in ${canton}`)
table.addRow(row)
// fill data
table.addRow(createRow(`New ${today.date}`, diffToday))
table.addRow(createRow(`New ${yesterday.date}`, diffYday))
table.addRow(createRow("Total", today.ncumul_conf))
table.addRow(createRow("Deaths", today.ncumul_deceased_fwd))
if (config.runsWithSiri)
Speech.speak(`There are ${res.cases} cases in ${country}, and ${res.todayCases} cases today.`)
// present table
table.present()
}
function createRow(title, number) {
let row = new UITableRow()
row.addText(title)
row.addText(number.toString()).rightAligned()
return row
}
function createWidget(pretitle, title, subtitle, color) {
let w = new ListWidget()
w.backgroundColor = new Color(color)
let preTxt = w.addText(pretitle)
preTxt.textColor = Color.white()
preTxt.font = Font.systemFont(12)
w.addSpacer(5)
let titleTxt = w.addText(title)
titleTxt.textColor = Color.white()
titleTxt.font = Font.systemFont(12)
w.addSpacer(5)
let subTxt = w.addText(subtitle)
subTxt.textColor = Color.white()
subTxt.font = Font.systemFont(12)
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment