Skip to content

Instantly share code, notes, and snippets.

@benzinkanister79
Forked from malakka/incidence.js
Created October 27, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benzinkanister79/bbfb52a20f4d330b47c21aa32967921f to your computer and use it in GitHub Desktop.
Save benzinkanister79/bbfb52a20f4d330b47c21aa32967921f to your computer and use it in GitHub Desktop.
COVID-19 Inzidenz-Widget für iOS innerhalb Deutschlands 🇩🇪
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
const newCasesApiUrl = `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?f=json&where=NeuerFall%20IN(1%2C%20-1)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22AnzahlFall%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true`;
const incidenceUrl = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,last_update,cases,cases7_per_100k&geometry=${location.longitude.toFixed(3)}%2C${location.latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`
const saveIncidenceLatLon = (location) => {
let fm = FileManager.iCloud()
let path = fm.joinPath(fm.documentsDirectory(), "covid19latlon.json")
fm.writeString(path, JSON.stringify(location))
}
const getsavedIncidenceLatLon = () => {
let fm = FileManager.iCloud()
let path = fm.joinPath(fm.documentsDirectory(), "covid19latlon.json")
let data = fm.readString(path)
return JSON.parse(data)
}
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let data, attr, header, label
const list = new ListWidget()
// fetch new cases
data = await new Request(newCasesApiUrl).loadJSON()
if(!data || !data.features || !data.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für die Anfrage nach den Neuinfektionen.")
return errorList
}
header = list.addText("🦠 Neuinfektionen ".toUpperCase())
header.centerAlignText()
header.font = Font.mediumSystemFont(10)
label = list.addText("+"+data.features[0].attributes.value)
label.font = Font.mediumSystemFont(20)
label.centerAlignText()
const country = list.addText("Deutschland")
country.centerAlignText()
country.font = Font.mediumSystemFont(12)
country.textColor = Color.gray()
list.addSpacer()
// fetch new incidents
let location
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
location = {
latitude: fixedCoordinates[0],
longitude: fixedCoordinates[1]
}
} else {
Location.setAccuracyToThreeKilometers()
try {
location = await Location.current()
console.log('get current lat/lon')
saveIncidenceLatLon(location)
} catch(e) {
console.log('using saved lat/lon')
location = getsavedIncidenceLatLon()
}
}
data = await new Request(incidenceUrl(location)).loadJSON()
if(!data || !data.features || !data.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
return errorList
}
attr = data.features[0].attributes
const incidence = attr.cases7_per_100k.toFixed(1)
const cityName = attr.GEN
const cases =attr.cases
const lastUpdate = attr.last_update
header = list.addText("🦠 Inzidenz".toUpperCase())
header.centerAlignText()
header.font = Font.mediumSystemFont(10)
label = list.addText(incidence)
label.centerAlignText()
label.font = Font.mediumSystemFont(24)
label2 = list.addText("("+cases+")")
label2.centerAlignText()
label2.font = Font.mediumSystemFont(12)
if(incidence >= 50) {
label.textColor = Color.red()
} else if(incidence >= 25) {
label.textColor = Color.orange()
}
const city = list.addText(cityName)
city.centerAlignText()
city.font = Font.mediumSystemFont(12)
city.textColor = Color.gray()
list.addSpacer()
label3 = list.addText ("letztes Update: "+lastUpdate.substr(0,10))
label3.centerAlignText()
label3.font = Font.mediumSystemFont(6)
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment