-
-
Save Baumchen/6d91df0a4c76c45b15576db0632e4329 to your computer and use it in GitHub Desktop.
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0 | |
// Multiple values: Use with `48.260,11.439,DAH;48.809,11.897,KEH;49.122,12.549,LA` as widget parameter e.g. | |
// Current location can be used with: `current,NAME;48.809,11.897,KEH;49.122,12.549,LA` as widget parameter e.g. | |
const apiUrl = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,last_update,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` | |
function parseLocation(location) { | |
const fixedCoordinates = location.split(",") | |
if (fixedCoordinates[0] === "current") { | |
return { | |
current_location: true, | |
name: fixedCoordinates.length >= 2 ? fixedCoordinates[1] : null, | |
} | |
} | |
return { | |
latitude: parseFloat(fixedCoordinates[0]), | |
longitude: parseFloat(fixedCoordinates[1]), | |
name: fixedCoordinates.length >= 3 ? fixedCoordinates[2] : null, | |
} | |
} | |
async function dataForLocation(location) { | |
let locationData | |
if (location.current_location) { | |
Location.setAccuracyToThreeKilometers() | |
locationData = await Location.current() | |
} else { | |
locationData = location | |
} | |
const data = await new Request(apiUrl(locationData)).loadJSON() | |
if (!data || !data.features || !data.features.length) { | |
return { | |
error: "Keine Ergebnisse für den aktuellen Ort gefunden.", | |
} | |
} | |
const attr = data.features[0].attributes | |
const incidence = attr.cases7_per_100k.toFixed(1) | |
const cityName = location["name"] ? location["name"] : attr.GEN | |
const lastUpdate = parseInt(attr.last_update.split(".")[0]) | |
return { | |
incidence: incidence, | |
name: cityName, | |
updateDay: lastUpdate, | |
} | |
} | |
let widget = await createWidget() | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(items) { | |
let parameters | |
if (args.widgetParameter) { | |
parameters = args.widgetParameter | |
} else { | |
parameters = "current" | |
} | |
const locations = parameters.split(";").map(parseLocation) | |
const list = new ListWidget() | |
const header = list.addStack() | |
header.layoutHorizontally() | |
header.centerAlignContent() | |
const headerTitle = header.addText("🦠 Inzidenz".toUpperCase()) | |
headerTitle.font = Font.mediumSystemFont(13) | |
headerTitle.leftAlignText() | |
header.addSpacer() | |
const currentDate = new Date().getDate() | |
const dateLabel = header.addText(currentDate + ".") | |
dateLabel.font = Font.mediumSystemFont(10) | |
dateLabel.rightAlignText() | |
list.addSpacer() | |
for (location of locations) { | |
const data = await dataForLocation(location) | |
if (data["error"]) { | |
list.addText(data["error"]) | |
continue | |
} | |
const incidence = data["incidence"] | |
const cityName = data["name"] | |
const line = list.addStack() | |
line.layoutHorizontally() | |
line.centerAlignContent() | |
line.useDefaultPadding() | |
const label = line.addText(incidence + "") | |
label.font = Font.boldSystemFont(24) | |
label.leftAlignText() | |
if (incidence >= 50) { | |
label.textColor = Color.red() | |
} else if (incidence >= 35) { | |
label.textColor = Color.orange() | |
} else { | |
label.textColor = Color.green() | |
} | |
line.addSpacer() | |
if (currentDate !== data.updateDay) { | |
const oldData = line.addText("⌛") | |
oldData.font = Font.systemFont(10) | |
} | |
const name = line.addText(cityName) | |
name.rightAlignText() | |
if (currentDate !== data.updateDay) { | |
name.textColor = Color.orange() | |
} | |
} | |
return list | |
} |
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
// Multiple values: Use with 48.260,11.439,DAH;48.809,11.897,KEH;49.122,12.549,LA
as widget parameter e.g.
// Current location can be used with: current,NAME;48.809,11.897,KEH;49.122,12.549,LA
as widget parameter e.g.
const apiUrl = (location) => https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,cases7_per_100k,last_update&geometry=${location.longitude.toFixed(3)}%2C${location.latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json
function parseLocation (location) {
const fixedCoordinates = location.split(",")
if (fixedCoordinates[0] === "current") {
return {
current_location: true,
name: fixedCoordinates.length >= 2 ? fixedCoordinates[1] : null,
}
}
return {
latitude: parseFloat(fixedCoordinates[0]),
longitude: parseFloat(fixedCoordinates[1]),
name: fixedCoordinates.length >= 3 ? fixedCoordinates[2] : null,
}
}
async function dataForLocation(location) {
let locationData
if (location.current_location) {
Location.setAccuracyToThreeKilometers()
locationData = await Location.current()
} else {
locationData = location
}
const data = await new Request(apiUrl(locationData)).loadJSON()
if (!data || !data.features || !data.features.length) {
return {
error: "Keine Ergebnisse für den aktuellen Ort gefunden.",
}
}
const attr = data.features[0].attributes
const incidence = attr.cases7_per_100k.toFixed(1)
const lastRefresh = attr.last_update
const cityName = location["name"] ? location["name"] : attr.GEN
return {
incidence: incidence,
name: cityName,
date: lastRefresh
}
}
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let parameters
if (args.widgetParameter) {
parameters = args.widgetParameter
} else {
parameters = "current"
}
const locations = parameters.split(";").map(parseLocation)
const list = new ListWidget()
const header = list.addText("🦠 Inzidenz".toUpperCase())
header.font = Font.mediumSystemFont(13)
list.addSpacer()
for (location of locations) {
const data = await dataForLocation(location)
if (data["error"]) {
list.addText(data["error"])
continue
}
const incidence = data["incidence"]
const cityName = data["name"]
const lastRefresh = data["date"]
const line = list.addStack()
line.layoutHorizontally()
line.centerAlignContent()
line.useDefaultPadding()
const label = line.addText(incidence+"")
label.font = Font.boldSystemFont(24)
label.leftAlignText()
if(incidence >= 50) {
label.textColor = Color.red()
} else if(incidence >= 35) {
label.textColor = Color.orange()
} else {
label.textColor = Color.green()
}
line.addSpacer()
const name = line.addText(cityName)
name.rightAlignText()
line.addSpacer()
const label1 = line.addText(lastRefresh.substr(0,10))
label1.font = Font.mediumSystemFont(6)
}
return list
}
Hoffe jetzt passt der Code. Das einfügen am iPhone klappt bei mir nicht so richtig. Einfach alles aus meinem vorherigen Post in ein Script einfügen.
Hoffe jetzt passt der Code. Das einfügen am iPhone klappt bei mir nicht so richtig. Einfach alles aus meinem vorherigen Post in ein Script einfügen.
Danke.
Vielen Dank für das tolle Widget
Auf meinem Blog gibt es einen kleinen Beitrag dazu
Stelle mich irgendwie dumm an. Ich bekomme es nicht zum laufen. Kann man den richtigen Code vielleicht nochmal richtig einstellen.
Danke