Skip to content

Instantly share code, notes, and snippets.

@jona7o
Last active April 18, 2021 04:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jona7o/2908bfe5ccb4ef2fd0f4f382e1e50eaa to your computer and use it in GitHub Desktop.
Save jona7o/2908bfe5ccb4ef2fd0f4f382e1e50eaa to your computer and use it in GitHub Desktop.
covid19-widget
// 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";
const imgUrl = await new Request("https://innfactory.de/wp-content/uploads/2018/03/innFactory_web_2x.png");
const img = await imgUrl.loadImage();
// Initialize Widget
let widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
// Build Widget
async function createWidget(items) {
const widgetList = new ListWidget();
let header, label;
// fetch new cases
const newCasesData = await getNewCasesData();
header = widgetList.addText("📈 Neuinfektionen".toUpperCase());
header.font = Font.mediumSystemFont(10);
label = widgetList.addText("+" + newCasesData.value);
label.font = Font.mediumSystemFont(24);
const country = widgetList.addText(newCasesData.areaName);
country.font = Font.mediumSystemFont(12);
country.textColor = Color.gray();
widgetList.addSpacer();
// fetch new incidents
const incidenceData = await getIncidenceData();
header = widgetList.addText("🦠 Inzidenz".toUpperCase());
header.font = Font.mediumSystemFont(10);
if (incidenceData) {
label = widgetList.addText(incidenceData.value + "");
label.font = Font.mediumSystemFont(24);
if (incidenceData.value >= 100) {
label.textColor = new Color('#8b0000');
} else if (incidenceData.value >= 50) {
label.textColor = Color.red();
} else if (incidenceData.value >= 25) {
label.textColor = Color.orange();
} else {
label.textColor = Color.green();
}
const city = widgetList.addText(incidenceData.areaName);
city.font = Font.mediumSystemFont(12);
city.textColor = Color.gray();
if (incidenceData.shouldCache) {
widgetList.refreshAfterDate = new Date(Date.now() + 60 * 60 * 1000);
}
} else {
widgetList.addText("Daten nicht verfügbar");
}
widgetList.addSpacer();
let image = widgetList.addImage(img);
image.centerAlignImage();
image.imageSize = new Size(50, 16)
return widgetList;
}
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;
}
}
@jona7o
Copy link
Author

jona7o commented Oct 24, 2020

7926352A-824A-41FE-B19C-4754D9E0B18A

Geforked und für gut befunden.

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