Skip to content

Instantly share code, notes, and snippets.

@matmuenzel
Created October 22, 2020 06:53
Show Gist options
  • Select an option

  • Save matmuenzel/65a0c72d42d22ca2c60d22693d85d496 to your computer and use it in GitHub Desktop.

Select an option

Save matmuenzel/65a0c72d42d22ca2c60d22693d85d496 to your computer and use it in GitHub Desktop.
Corona-Indicies-Scriptable
// Forked from Keven Kub.
// See https://gist.github.com/kevinkub/46caebfebc7e26be63403a7f0587f664
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
const apiUrl = ({ longitude, latitude }) =>
`https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,cases7_per_100k&geometry=${longitude.toFixed(3)}%2C${latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`;
const widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function createWidget() {
let location;
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
location = {
latitude: fixedCoordinates[0],
longitude: fixedCoordinates[1]
}
} else {
Location.setAccuracyToThreeKilometers()
location = await Location.current()
}
const data = await new Request(apiUrl(location)).loadJSON();
if (!data || !data.features || !data.features.length) {
const errorList = new ListWidget();
errorList.addText('Keine Ergebnisse für den Ort verfügbar 🥺');
return errorList;
}
const attr = data.features[0].attributes;
const incidence = attr.cases7_per_100k.toFixed(1);
const cityName = attr.GEN;
const list = new ListWidget();
const bgGradient = new LinearGradient();
bgGradient.locations = [0, 1];
bgGradient.colors = Device.isUsingDarkAppearance()
? [new Color('111'), new Color('222')]
: [new Color('fff'), new Color('fafafa')];
list.backgroundGradient = bgGradient;
const header = list.addText('🦠 7-Tage-Inzidenz'.toUpperCase());
header.font = Font.mediumSystemFont(13);
header.textColor = Device.isUsingDarkAppearance() ? Color.white() : Color.black();
list.addSpacer();
const label = list.addText(incidence + '');
label.font = Font.boldSystemFont(24);
label.textColor = Color.green();
//change text color depending on which warning level is active
if (incidence >= 50) {
label.textColor = Color.red()
} else if(incidence >= 35) {
label.textColor = Color.orange()
}
let text = list.addText(cityName);
text.textOpacity = 0.5;
text.textColor = Device.isUsingDarkAppearance() ? Color.white() : Color.black();
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment