Skip to content

Instantly share code, notes, and snippets.

@m4p
Last active Aug 13, 2021
Embed
What would you like to do?
Corona Ampel Berlin Widget für Scriptable.app
// Corona Ampel Berlin Widget
//
// Copyright (C) 2020 by map <mail@map.wtf>
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
let widget = new ListWidget();
let padding = 22;
widget.setPadding(padding, padding, padding, padding);
widget.url = "https://www.berlin.de/corona/lagebericht/desktop/corona.html";
let apiResponse = await loadItems();
let header = widget.addText("🚦Covid-19 Berlin".toUpperCase());
header.font = Font.mediumSystemFont(10);
widget.addSpacer(16);
let vStack = widget.addStack();
vStack.layoutHorizontally();
addDataView(vStack, apiResponse.indicators.basic_reproduction_number);
vStack.addSpacer();
addDataView(vStack, apiResponse.indicators.incidence_new_infections);
widget.addSpacer();
let vStack2 = widget.addStack();
vStack2.layoutHorizontally();
addDataView(vStack2, apiResponse.indicators.icu_occupancy_rate);
vStack2.addSpacer();
addDataView(vStack2, apiResponse.vaccination.percentage_two_doses_display);
Script.setWidget(widget);
Script.complete();
widget.presentSmall();
function addDataView(widget, data) {
let viewStack = widget.addStack();
viewStack.layoutVertically();
let label = viewStack.addText(data.shortDescription);
label.font = Font.mediumSystemFont(12);
if (data.footnote != "") {
let footnote = viewStack.addText(data.footnote);
footnote.font = Font.mediumSystemFont(6);
}
let value = viewStack.addText(data.stringValue);
value.font = Font.mediumSystemFont(20);
value.textColor = colorForString(data.color);
}
async function loadItems() {
let url =
"https://raw.githubusercontent.com/knudmoeller/berlin_corona_cases/master/data/target/berlin_corona_traffic_light.latest.json";
let req = new Request(url);
let json = await req.loadJSON();
json.indicators.basic_reproduction_number.shortDescription = "R-Wert";
json.indicators.incidence_new_infections.shortDescription = "Inzidenz";
json.indicators.icu_occupancy_rate.shortDescription = "ITS";
json.indicators.basic_reproduction_number.footnote = "(4 Tage)";
json.indicators.incidence_new_infections.footnote = "(7 Tage)";
json.indicators.icu_occupancy_rate.footnote = "(in %)";
json.indicators.basic_reproduction_number.stringValue = json.indicators.basic_reproduction_number.value.toString();
json.indicators.incidence_new_infections.stringValue = json.indicators.incidence_new_infections.value.toString();
json.indicators.icu_occupancy_rate.stringValue =
json.indicators.icu_occupancy_rate.value.toString();
json.vaccination["percentage_two_doses_display"] = {}
json.vaccination.percentage_two_doses_display.shortDescription = "Geimpft";
json.vaccination.percentage_two_doses_display.footnote = "(in %)";
json.vaccination.percentage_two_doses_display.stringValue = json.vaccination.percentage_two_doses.toString();
json.vaccination.percentage_two_doses_display.color = "blue";
return json;
}
function colorForString(colorString) {
if (colorString == "red") {
return Color.red();
}
if (colorString == "yellow") {
return Color.yellow();
}
if (colorString == "blue") {
return Color.blue();
}
return Color.green();
}
@Macbalko

This comment has been minimized.

Copy link

@Macbalko Macbalko commented Nov 6, 2020

Wo bekomme ich Daten für mein Landkreis her?

@m4p

This comment has been minimized.

Copy link
Owner Author

@m4p m4p commented Nov 6, 2020

Ich denke da müsstest du dir deinen eigenen Scraper schreiben. Die Daten selbst gibt es beim RKI (siehe zum Beispiel hier) und beim DIVI, für die Intensivbettenbelegung.

@Macbalko

This comment has been minimized.

Copy link

@Macbalko Macbalko commented Nov 20, 2020

Was ist ein Scraper und mit den Daten kann ich leider nix anfangen, sry

@plaetzchen

This comment has been minimized.

Copy link

@plaetzchen plaetzchen commented May 17, 2021

Ich war mal so frei und habe (völlig amateurhaft) die Impfungen dazu gepackt:

let widget = new ListWidget();
let padding = 22;
widget.setPadding(padding, padding, padding, padding);
widget.url = "https://www.berlin.de/corona/lagebericht/desktop/corona.html";

let apiResponse = await loadItems();

let header = widget.addText("🚦Covid-19 Berlin".toUpperCase());
header.font = Font.mediumSystemFont(10);

widget.addSpacer(16);

let vStack = widget.addStack();
vStack.layoutHorizontally();

addDataView(vStack, apiResponse.indicators.basic_reproduction_number);
vStack.addSpacer();
addDataView(vStack, apiResponse.indicators.incidence_new_infections);
widget.addSpacer();

let vStack2 = widget.addStack();
vStack2.layoutHorizontally();

addDataView(vStack2, apiResponse.indicators.icu_occupancy_rate);
vStack2.addSpacer();
addDataView(vStack2, apiResponse.vaccination.percentage_two_doses_display);

Script.setWidget(widget);
Script.complete();
widget.presentSmall();

function addDataView(widget, data) {
  let viewStack = widget.addStack();
  viewStack.layoutVertically();

  let label = viewStack.addText(data.shortDescription);
  label.font = Font.mediumSystemFont(12);

  if (data.footnote != "") {
    let footnote = viewStack.addText(data.footnote);
    footnote.font = Font.mediumSystemFont(6);
  }

  let value = viewStack.addText(data.stringValue);
  value.font = Font.mediumSystemFont(20);
  value.textColor = colorForString(data.color);
}

async function loadItems() {
  let url =
    "https://raw.githubusercontent.com/knudmoeller/berlin_corona_cases/master/data/target/berlin_corona_traffic_light.latest.json";
  let req = new Request(url);
  let json = await req.loadJSON();
  json.indicators.basic_reproduction_number.shortDescription = "R-Wert";
  json.indicators.incidence_new_infections.shortDescription = "Inzidenz";
  json.indicators.icu_occupancy_rate.shortDescription = "ITS";
  json.indicators.basic_reproduction_number.footnote = "(4 Tage)";
  json.indicators.incidence_new_infections.footnote = "(7 Tage)";
  json.indicators.icu_occupancy_rate.footnote = "%";
  json.indicators.basic_reproduction_number.stringValue = json.indicators.basic_reproduction_number.value.toString();
  json.indicators.incidence_new_infections.stringValue = json.indicators.incidence_new_infections.value.toString();
  json.indicators.icu_occupancy_rate.stringValue =
    json.indicators.icu_occupancy_rate.value.toString();

json.vaccination["percentage_two_doses_display"] = {}

json.vaccination.percentage_two_doses_display.shortDescription = "Geimpft";
json.vaccination.percentage_two_doses_display.footnote = "%";
json.vaccination.percentage_two_doses_display.stringValue = json.vaccination.percentage_two_doses.toString();
json.vaccination.percentage_two_doses_display.color = "blue";
  return json;
}

function colorForString(colorString) {
  if (colorString == "red") {
    return Color.red();
  }
  if (colorString == "yellow") {
    return Color.yellow();
  }
  if (colorString == "blue") {
    return Color.blue();
  }
  return Color.green();
}
@m4p

This comment has been minimized.

Copy link
Owner Author

@m4p m4p commented May 17, 2021

Danke! Hab ich gleich mal übernommen. ❤️

@plaetzchen

This comment has been minimized.

Copy link

@plaetzchen plaetzchen commented May 17, 2021

Gerade überlegt ob man vll. als Subtitle eher (in %) macht, dass könnte visuell etwas ausgeglichener sein

@m4p

This comment has been minimized.

Copy link
Owner Author

@m4p m4p commented May 17, 2021

Gerade überlegt ob man vll. als Subtitle eher (in %) macht, dass könnte visuell etwas ausgeglichener sein

Gute Idee.

@plaetzchen

This comment has been minimized.

Copy link

@plaetzchen plaetzchen commented May 17, 2021

Zeile 80 sollte wohl auch "(in %)" sein dann

@m4p

This comment has been minimized.

Copy link
Owner Author

@m4p m4p commented May 17, 2021

ups.

@SimonHoenscheid

This comment has been minimized.

Copy link

@SimonHoenscheid SimonHoenscheid commented Aug 13, 2021

Es sieht für mich so aus als wären die gelieferten Daten falsch. Das Widget zeigt bei mir aktuell eine Inzidenz von 44,1, die Webseite jedoch 50,3

@m4p

This comment has been minimized.

Copy link
Owner Author

@m4p m4p commented Aug 13, 2021

@SimonHoenscheid

This comment has been minimized.

Copy link

@SimonHoenscheid SimonHoenscheid commented Aug 13, 2021

Danke!

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