Last active
January 4, 2021 22:05
-
-
Save abbe79/e0e43670a8bc7beb17eec26a9be30291 to your computer and use it in GitHub Desktop.
corona incidence for Scriptable on iOS
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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: deep-green; icon-glyph: traffic-light; | |
// share-sheet-inputs: file-url, url, plain-text; | |
async function getLocation(){ | |
let param = args.widgetParameter | |
if (param != null && param.length > 0) { | |
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat) | |
return { latitude: fixedCoordinates[0], longitude: fixedCoordinates[1], source: "" } | |
} else { | |
Location.setAccuracyToKilometer() | |
const myLocation = await Location.current() | |
return { latitude: myLocation.latitude, longitude: myLocation.longitude, source: "⌖ " } | |
} | |
} | |
async function getData(location){ | |
const url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?outFields=county,last_update,cases7_per_100k&geometry=" + location.longitude + "%2C" + location.latitude + "&geometryType=esriGeometryPoint&inSR=4326&f=json&returnGeometry=false" | |
const json = await new Request(url).loadJSON() | |
const data = json.features[0].attributes | |
const county = location.source + data.county.replace('LK ','') | |
const lastUpdate = data.last_update.substring(0, 10) | |
const incidenceNumber = data.cases7_per_100k | |
const incidence = incidenceNumber.toLocaleString(undefined, {maximumFractionDigits:1}) | |
let bgColor = Color.green() | |
let textColor = Color.black() | |
if(incidenceNumber >= 100){ | |
bgColor = new Color("440000") | |
textColor = Color.white() | |
} else if(incidenceNumber >= 50){ | |
bgColor = Color.red() | |
textColor = Color.white() | |
} else if(incidenceNumber >= 35){ | |
bgColor = Color.orange() | |
} | |
return {county: county, lastUpdate: lastUpdate, incidence: incidence, bgColor: bgColor, textColor: textColor} | |
} | |
async function createWidget(data){ | |
const widget = new ListWidget() | |
widget.backgroundColor = data.bgColor | |
widget.useDefaultPadding() | |
widget.url = "https://www.lgl.bayern.de/gesundheit/infektionsschutz/infektionskrankheiten_a_z/coronavirus/karte_coronavirus/" | |
const offset = 1000 * 60 * 60 | |
const date = new Date(Date.now() + offset) | |
widget.refreshAfterDate = date | |
const countyText = widget.addText(data.county) | |
countyText.centerAlignText() | |
countyText.font = Font.regularSystemFont(20) | |
countyText.textColor = data.textColor | |
widget.addSpacer() | |
const incidenceText = widget.addText(data.incidence) | |
incidenceText.centerAlignText() | |
incidenceText.font = Font.boldSystemFont(35) | |
incidenceText.textColor = data.textColor | |
if (!config.runsInWidget) { | |
widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
} | |
const location = await getLocation() | |
const data = await getData(location) | |
createWidget(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fetches corona incidence for your current location from https://services7.arcgis.com.
For use in Scriptable (https://apps.apple.com/de/app/scriptable/id1405459188)
You may paste some geo coordinates as widget parameter to set a fixed location.