Skip to content

Instantly share code, notes, and snippets.

@dwd0tcom
Last active June 27, 2023 12:53
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save dwd0tcom/00a7aa6d41f6f945d16171249a50a29d to your computer and use it in GitHub Desktop.
Save dwd0tcom/00a7aa6d41f6f945d16171249a50a29d to your computer and use it in GitHub Desktop.
A scriptable widget to display and show the current incidence and vaccine status.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: syringe;
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
// Vaccine API by @_ThisIsBenny_
// Version 1.3
// For updates check:
// https://gist.github.com/dwd0tcom/00a7aa6d41f6f945d16171249a50a29d/
// Define URLs based on the corona.rki.de webpage
const newCasesApiUrl = `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/ArcGIS/rest/services/Covid19_hubv/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,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 incidenceUrlStates =
"https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/Coronaf%E4lle_in_den_Bundesl%E4ndern/FeatureServer/0/query?where=1%3D1&outFields=cases7_bl_per_100k&returnGeometry=false&outSR=4326&f=json";
const vaccineStatus = "https://rki-vaccination-data.vercel.app/api";
// Initialize Widget
let widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
// Build Widget
async function createWidget(items) {
const list = new ListWidget();
let header, label;
// fetch new cases
const newCasesData = await getNewCasesData();
header = list.addText("🚨 Neuinfektionen".toUpperCase());
header.font = Font.mediumSystemFont(10);
header.minimumScaleFactor = 0.9;
label = list.addText("+" + newCasesData.value.toLocaleString());
label.font = Font.mediumSystemFont(24);
label.minimumScaleFactor = 0.9;
const country = list.addText(newCasesData.areaName);
country.font = Font.mediumSystemFont(12);
country.textColor = Color.gray();
list.addSpacer();
// fetch new incidents
const incidenceData = await getIncidenceData();
header = list.addText("🦠 Inzidenz".toUpperCase());
header.font = Font.mediumSystemFont(10);
if (incidenceData) {
label = list.addText(incidenceData.value + "");
label.font = Font.mediumSystemFont(24);
if (incidenceData.value >= 1000) {
label.textColor = new Color('#6E1472');
} else if (incidenceData.value >= 500) {
label.textColor = new Color('#C82B81');
} else if (incidenceData.value >= 250) {
label.textColor = new Color('#8c0801');
} else if (incidenceData.value >= 100) {
label.textColor = new Color('#cc0a00');
} else if (incidenceData.value >= 50) {
label.textColor = new Color('#ff3d33');
} else if (incidenceData.value >= 25) {
label.textColor = new Color('#f9af4d');
} else if (incidenceData.value >= 5) {
label.textColor = new Color('#fcf39f')
} else if (incidenceData.value >= 0) {
label.textColor = new Color('#000000');
}
const city = list.addText(incidenceData.areaName);
city.font = Font.mediumSystemFont(12);
city.textColor = Color.gray();
if (incidenceData.shouldCache) {
list.refreshAfterDate = new Date(Date.now() + 60 * 60 * 1000);
}
} else {
list.addText("Daten nicht verfügbar");
}
list.addSpacer();
// fetch new vaccines
const number = await getVaccineData();
let amount = number.value.toLocaleString();
header = list.addText("💉 " + amount + " geimpfte");
header.font = Font.mediumSystemFont(8);
header.textColor = Color.gray()
return list;
}
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;
}
}
// Get vaccine Status
async function getVaccineData() {
let data = await new Request(vaccineStatus).loadJSON();
const attr = data.vaccinated;
return {
value: attr,
}
}
async function getNewCasesData() {
let data = await new Request(newCasesApiUrl).loadJSON();
const attr = data.features[0].attributes;
return {
value: attr.value,
areaName: "Deutschland",
shouldCache: false,
};
}
async function getIncidenceData() {
try {
const location = await getLocation();
if (location) {
let data = await new Request(incidenceUrl(location)).loadJSON();
const attr = data.features[0].attributes;
return {
value: attr.cases7_per_100k.toFixed(1),
areaName: attr.GEN,
shouldCache: true,
};
} else {
let data = await new Request(incidenceUrlStates).loadJSON();
const incidencePerState = data.features.map(
(f) => f.attributes.cases7_bl_per_100k
);
const averageIncidence =
incidencePerState.reduce((a, b) => a + b) / incidencePerState.length;
return {
value: averageIncidence.toFixed(1),
areaName: "Deutschland",
shouldCache: false,
};
}
} catch (e) {
return null;
}
}
@ungarbaron
Copy link

👍🏻 Astrein
Hast textColot gepimpt? Oder ist mir das vorher nicht aufgefallen?

@dwd0tcom
Copy link
Author

Ja, hab die Farben an die vom RKI angepasst aber davor schon 😃

@slovenija2505
Copy link

Hallo zusammen,
hat vielleicht noch jemand diesen Fehler?
Was muss den geändert werden, damit es wieder geht?
C3B1B746-BE8A-4CC0-A4F7-3229667DC79F

@dwd0tcom
Copy link
Author

dwd0tcom commented May 8, 2022

Hallo, das Problem ist, dass das RKI den kompletten Datensatz geändert hat und die API von @ThisIsBenny eingestellt wurde. Ein Quickfix wäre, die Zeile 97 - 103 auszukommentieren. Ich weiß nicht wie sinnvoll es ist, die Impfdaten noch reinzuladen, die stehen seit Monaten fast gleich. Was echt traurig ist 🥲

@slovenija2505
Copy link

Super, vielen Dank für den Tip.
Jetzt habe ich wenigstens die Inzidenz wieder!
👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment