Last active
October 23, 2020 17:16
-
-
Save AleksCee/ffd5d8b0d55de205aa08033e4ca9befa to your computer and use it in GitHub Desktop.
Scriptable iOS widget: Corona Ampel auf Basis der RKI-API
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
let region = 3 // Niedersachsen | |
let param = args.widgetParameter | |
if (param != null && param.length > 0) { | |
region = param | |
} | |
const colors = {35: '#008000', 50: '#CCCC00', 51: '#FF0000'} | |
const data = await fetchData() | |
const widget = new ListWidget() | |
await createWidget() | |
// used for debugging if script runs inside the app | |
if (!config.runsInWidget) { | |
await widget.presentLarge() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
// build the content of the widget | |
async function createWidget() { | |
widget.setPadding(10,10,10,10) | |
let text = widget.addText('Corona 7Tage/100T EW für '+data.bundesland+': ' + data.overall + ' Fälle, Stand: ' + data.lastupdate) | |
text.font = Font.regularSystemFont(14) | |
widget.addSpacer(5) | |
let maxLength = data.landkreise.reduce((s,c) => {return c.text.length> s ? c.text.length : s},0) | |
console.log(maxLength) | |
let row | |
data.landkreise.forEach((landkreis, index) => { | |
let spacer | |
if (index % 2 === 0){ | |
row = widget.addStack() | |
row.layoutHorizontally() | |
spacer = maxLength - landkreis.text.length + 2 | |
} else { | |
row.addSpacer(spacer) | |
} | |
let text = row.addText(landkreis.text) | |
text.font = Font.regularSystemFont(10) | |
text.textColor = new Color(landkreis.color) | |
}) | |
} | |
// fetches the amount of toilet paper packages | |
async function fetchData() { | |
const url = 'https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=BL_ID%20%3D%20%27'+region+'%27&outFields=GEN,BL,county,cases7_per_100k,last_update,cases7_bl_per_100k&returnGeometry=false&returnDistinctValues=true&outSR=&f=json' | |
const req = new Request(url) | |
const apiResult = await req.loadJSON() | |
const result = {} | |
result.lastupdate = apiResult.features.length >0 ? apiResult.features[0].attributes.last_update : 'n/a' | |
result.bundesland = apiResult.features.length >0 ? apiResult.features[0].attributes.BL : 'n/a' | |
result.overall = apiResult.features.length >0 ? apiResult.features[0].attributes['cases7_bl_per_100k'].toFixed(2) : 'n/a' | |
result.landkreise = apiResult.features.map(landkreis => { | |
return { | |
text: landkreis.attributes.GEN + ': ' + landkreis.attributes['cases7_per_100k'].toFixed(2) + ' Fälle', | |
color: colors[Object.keys(colors).reduce((s,c) => {return landkreis.attributes['cases7_per_100k'] < s ? s : c}, 0)] | |
} | |
}) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dieses Script kann mit Hilfe von Scriptable als Widget in iOS genutzt werden.
Die Default-Einstellung ist für Niedersachen, dieser kann aber die Widget-Konfig mit dem Übergabeparameter übersteuert werden.
Hierzu muss man sich auf der Seite: API-Beschreibung sein Bundesland auswählen und dann aus der Spalte „Bundesland ID“ die ID raussuchen und im Widget als Parameter hinterlegen. Für Niedersachsen ist das z.B. die 3.
Leider muss das größte Widget genutzt werden, weil die Ausgabe pro Stadt je nach Bundesland sehr viele werden können. Die Städte werden in den Ampelfarben angezeigt, also bist 35 grün, 35-50 gelb, ab 51 rot.
Die Installation erfolgt wie von Marco (der mich zu diesem Script inspiriert hat - danke dafür) zu dem Klopapier-Abfrage-Widget beschrieben.
Hier eine Beispielausgabe: