Skip to content

Instantly share code, notes, and snippets.

@MisterMike
Last active October 23, 2020 09:36
Show Gist options
  • Save MisterMike/8b5d72aae1688891eaacce05d9266c6e to your computer and use it in GitHub Desktop.
Save MisterMike/8b5d72aae1688891eaacce05d9266c6e to your computer and use it in GitHub Desktop.
const canton = 'BS'
const apiUrl = (canton) => `https://data.bs.ch/api/records/1.0/search/?dataset=100077&q=&facet=date&facet=name&facet=abbreviation_canton_and_fl&refine.abbreviation_canton_and_fl=${canton}`
const apiUrlCH = `https://data.bs.ch/api/records/1.0/search/?dataset=100077&q=&facet=date&facet=name&facet=abbreviation_canton_and_fl`
const widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
const data = await getData()
const dataCH = await getDataCH()
const list = new ListWidget()
const header = list.addText("🦠 Laborfälle".toUpperCase())
header.font = Font.mediumSystemFont(13)
if(data && dataCH) {
list.addSpacer()
const label = list.addText(data.incidence+" ("+data.cantonName+")")
label.font = Font.boldSystemFont(24)
label.textColor = data.incidence >= 50 ? Color.red() : data.incidence >= 35 ? Color.orange() : Color.green()
list.addSpacer()
const labelCH = list.addText(dataCH.incidence+" ("+dataCH.cantonName+")")
labelCH.font = Font.boldSystemFont(24)
labelCH.textColor = dataCH.incidence >= 50 ? Color.red() : dataCH.incidence >= 35 ? Color.orange() : Color.green()
if(data.shouldCache) {
list.refreshAfterDate = new Date(Date.now() + 60*60*1000)
}
} else {
list.addSpacer()
list.addText("API Daten Fehler")
}
return list
}
async function getDataCH() {
try {
let data = await new Request(apiUrlCH).loadJSON()
return { incidence: data.nhits.toFixed(0), cantonName: "CH", shouldCache: true };
} catch(e) {
return null
}
}
async function getData() {
try {
let data = await new Request(apiUrl(canton)).loadJSON()
return { incidence: data.nhits.toFixed(0), cantonName: data.records[0].fields.abbreviation_canton_and_fl, shouldCache: true };
} catch(e) {
return null
}
}
async function getLocation() {
try {
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
return { latitude: fixedCoordinates[0], longitude: fixedCoordinates[1] }
} else {
Location.setAccuracyToThreeKilometers()
return await Location.current()
}
} catch(e) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment