Skip to content

Instantly share code, notes, and snippets.

@ffittschen
Created October 28, 2020 09:14
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 ffittschen/fa9d3ea50f8faf14b88d855162bce3c1 to your computer and use it in GitHub Desktop.
Save ffittschen/fa9d3ea50f8faf14b88d855162bce3c1 to your computer and use it in GitHub Desktop.
Kombination der Covid-19 Neuinfektionen und der Inzidenz-Zahl für die aktuelle Location. Inspiriert von https://gist.github.com/kevinkub/46caebfebc7e26be63403a7f0587f664
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
// Define URLs based on the corona.rki.de webpage
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,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";
// 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);
label = list.addText("+" + newCasesData.value);
label.font = Font.mediumSystemFont(24);
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 >= 50) {
label.textColor = Color.red();
} else if (incidenceData.value >= 25) {
label.textColor = Color.orange();
}
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");
}
return list;
}
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;
}
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment