-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImpfQuoten.js
349 lines (320 loc) · 11.8 KB
/
ImpfQuoten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: syringe;
//
// Script für https://scriptable.app
// iOS Widget zur Anzeige der gegen COVID-19 geimpften Personenzahl.
// Die Quote der Geimpften mit Bezug auf die Bevölkerung von Deutschland oder eines gewählten Bundeslands.
// Zusätzlich werden die Zahlen mit einem Fortschrittsbalken visualisiert.
// Konfiguriert als Widget Medium. Schaltet automatisch auch in den DarkMode.
//
// Script by MacSchierer, 03.05.2022, v2.3
// Download der aktuellen Version hier: GitHub https://github.com/MacSchierer/ImpfQuote
// https://fckaf.de/Bn0
//
// Verwendet die bereitgestellte Robert Koch-Institut COVID-19 API - von Marlon Lückert
// https://api.corona-zahlen.org, https://github.com/marlon360/rki-covid-api
//
// Optionale Konfiguration
//
// Individuelle Farbstufen für die Impfquote:
// Rot = Step1st < 25, Grün = Step2nd > 85, Dazwischen Orange, Ziel > 85 = "Herdenimmunität"
const Step1st = 25
const Step2nd = 85
const StepFin = 100
debug = false
//
// Ab hier nichts ändern
//
let APIurl = "https://api.corona-zahlen.org/vaccinations"
let hasError = false
let ErrorTxt = ""
let RegionKey= "Deutschland"
const BarWidth = 15
const BarHeigth = 105
let param = args.widgetParameter // Abfrage des Parameters vom Widget
if (debug) {param = "de"}
if (param != null && param.length > 0) {
param = param.toUpperCase()
if (setRegionKey(param) != false) {
RegionKey = setRegionKey(param)
RegionKey = param
}
else {
hasError = true
ErrorTxt += "Bundesland konnte nicht zugeordnet werden.\r\rBitte überprüfe den Parameter im Widget."
}
}
try {
AllItems = await loadItems(APIurl)
} catch (e) {
hasError = true
ErrorTxt += "Das Widget konnte keine Daten abrufen."
}
try {
if (RegionKey != "DE") {
RegItems = AllItems.data.states[RegionKey]
RegionName = RegItems.name
} else {
RegItems = AllItems.data
RegionName = "Deutschland"
}
// Daten zuordnen
Vacc1st = RegItems.vaccinated
QuoteVacc1st = RegItems.quote*100
DeltaVacc1st = RegItems.delta
Vacc2nd = RegItems.secondVaccination.vaccinated
QuoteVacc2nd = RegItems.secondVaccination.quote*100
DeltaVacc2nd = RegItems.secondVaccination.delta
Booster1st = RegItems.boosterVaccination.vaccinated
QuoteBooster1st = RegItems.boosterVaccination.quote*100
DeltaBooster1st = RegItems.boosterVaccination.delta
Booster2nd = RegItems['2ndBoosterVaccination'].vaccinated
QuoteBooster2nd = RegItems['2ndBoosterVaccination'].quote*100
DeltaBooster2nd = RegItems['2ndBoosterVaccination'].delta
if(debug){
log("Region: " + RegionName)
log("Vacc1st: " + Vacc1st + ", " + QuoteVacc1st + ", " + DeltaVacc1st)
log("Vacc2nd: " + Vacc2nd + ", " + QuoteVacc2nd + ", " + DeltaVacc2nd)
log("Booster1st: " + Booster1st + ", " + QuoteBooster1st + ", " + DeltaBooster1st)
log("Booster2nd: " + Booster2nd + ", " + QuoteBooster2nd + ", " + DeltaBooster2nd)
log("Stand: " + AllItems.meta.lastCheckedForUpdate)
}
} catch (e) {
hasError = true
ErrorTxt += "Beim Verarbeiten der Daten ist ein Fehler aufgetreten.\r\r"
ErrorTxt += "API: " + AllItems.message
}
// Fraben definieren
WidgetBgColor = Color.dynamic(new Color("#fefefe"), new Color("#1e1e1e"))
ContentBGColor = Color.dynamic(new Color("#efefef"), new Color("#444444"))
MainTextColor = Color.dynamic(new Color("#000000"), new Color("#ffffff"))
SubTextColor = Color.dynamic(new Color("#666666"), new Color("#aaaaaa"))
TitelColor = MainTextColor
BarTextColor1 = Color.dynamic(new Color("#ffffff"), new Color("#000000"))
BarTextColor2 = Color.dynamic(new Color("#000000"), new Color("#ffffff"))
// Ausgabe aufgelaufene Fehler oder Widget
if (hasError == true) {
let widget = errorWidget(ErrorTxt)
Script.setWidget(widget)
widget.presentMedium()
Script.complete()
} else {
const widget = new ListWidget()
widget.backgroundColor = WidgetBgColor
widget.setPadding(15, 15, 15, 15)
const Title = widget.addStack()
let TitleText = Title.addText("COVID-19 Impfungen")
TitleText.textColor = TitelColor
TitleText.font = Font.boldSystemFont(12)
TitleText.minimumScaleFactor = 0.5
TitleText.lineLimit = 1
Title.addSpacer()
widget.borderWidth = 1
widget.borderColor = MainTextColor
let DateText = Title.addDate(new Date(AllItems.meta.lastCheckedForUpdate))
DateText.textColor = SubTextColor
DateText.applyDateStyle()
DateText.font = Font.boldSystemFont(8)
const SubTitle = widget.addStack()
let SubTitleText = SubTitle.addText(RegionName)
SubTitleText.font = Font.systemFont(10)
SubTitleText.textColor = SubTextColor
SubTitleText.lineLimit = 1
widget.addSpacer(2)
const Content = widget.addStack()
Content.setPadding(2,2,2,2)
Content.layoutHorizontally()
const Stack1 = Content.addStack()
Stack1.layoutVertically()
Stack1.backgroundColor = ContentBGColor
Stack1.cornerRadius = 4
Stack1.setPadding(4,4,4,4)
const Stack1Head = Stack1.addStack()
Stack1Head.addSpacer()
let Title1stText = Stack1Head.addText("Vollständig")
Title1stText.textColor = MainTextColor
Title1stText.font = Font.boldSystemFont(14)
Stack1Head.addSpacer()
Stack1.addSpacer(4)
const Stack1Vacc = Stack1.addStack()
Stack1Vacc.addSpacer()
let VaccFullText = Stack1Vacc.addText(Vacc2nd.toLocaleString('de-DE'))
VaccFullText.textColor = MainTextColor
VaccFullText.font = Font.boldSystemFont(14)
Stack1Vacc.addSpacer()
const Stack1Diff = Stack1.addStack()
Stack1Diff.addSpacer()
let DiffFullText = Stack1Diff.addText("+" + DeltaVacc2nd.toLocaleString('de-DE'))
DiffFullText.textColor = SubTextColor
DiffFullText.font = Font.systemFont(12)
Stack1Diff.addSpacer()
Stack1.addSpacer(4)
const Stack1Percent = Stack1.addStack()
Stack1Percent.layoutHorizontally()
Stack1Percent.centerAlignContent()
Stack1Percent.addSpacer()
let QuoteFullText = Stack1Percent.addText((QuoteVacc2nd.toLocaleString('de-DE')).replace('.', ','))
QuoteFullText.textColor = MainTextColor
QuoteFullText.font = Font.boldSystemFont(28)
QuoteFullEin = Stack1Percent.addText("%")
QuoteFullEin.textColor = SubTextColor
QuoteFullEin.font = Font.systemFont(14)
Stack1Percent.addSpacer()
Stack1.addSpacer()
Content.addSpacer()
const BarContent1 = Content.addStack()
BarContent1.layoutVertically()
const progressBar1st = BarContent1.addImage(creatProgress(QuoteVacc2nd, QuoteVacc1st))
progressBar1st.cornerRadius = 4
progressBar1st.imageSize = new Size(BarWidth, BarHeigth)
Content.addSpacer()
const BarContent2 = Content.addStack()
BarContent2.layoutVertically()
const progressBar2nd = BarContent2.addImage(creatProgress(QuoteBooster1st, QuoteBooster2nd))
progressBar2nd.cornerRadius = 4
progressBar2nd.imageSize = new Size(BarWidth, BarHeigth)
Content.addSpacer()
const Stack2 = Content.addStack()
Stack2.layoutVertically()
Stack2.backgroundColor = ContentBGColor
Stack2.cornerRadius = 4
Stack2.setPadding(4,4,4,4)
const Stack2Head = Stack2.addStack()
Stack2Head.addSpacer()
let Title2ndText = Stack2Head.addText("Auffrischung")
Title2ndText.textColor = MainTextColor
Title2ndText.font = Font.boldSystemFont(14)
Stack2Head.addSpacer()
Stack2.addSpacer(4)
const Stack2Vacc = Stack2.addStack()
Stack2Vacc.addSpacer()
let VaccBoosterText = Stack2Vacc.addText(Booster1st.toLocaleString('de-DE'))
VaccBoosterText.textColor = MainTextColor
VaccBoosterText.font = Font.boldSystemFont(14)
Stack2Vacc.addSpacer()
const Stack2Diff = Stack2.addStack()
Stack2Diff.addSpacer()
let DiffBoosterText = Stack2Diff.addText("+" + DeltaBooster1st.toLocaleString('de-DE'))
DiffBoosterText.textColor = SubTextColor
DiffBoosterText.font = Font.systemFont(12)
Stack2Diff.addSpacer()
Stack2.addSpacer(4)
const Stack2Percent = Stack2.addStack()
Stack2Percent.layoutHorizontally()
Stack2Percent.centerAlignContent()
Stack2Percent.addSpacer()
let QuoteBoosterText = Stack2Percent.addText((QuoteBooster1st.toLocaleString('de-DE')).replace('.', ','))
QuoteBoosterText.textColor = MainTextColor
QuoteBoosterText.font = Font.boldSystemFont(28)
QuoteBoosterEin = Stack2Percent.addText("%")
QuoteBoosterEin.textColor = SubTextColor
QuoteBoosterEin.font = Font.systemFont(14)
Stack2Percent.addSpacer()
Stack2.addSpacer()
// Ausgabe
if (!config.runsInWidget) {
await widget.presentMedium()
} else {
Script.setWidget(widget)
}
Script.complete()
}
//
// Error Widget
//
function errorWidget(reason){
let w = new ListWidget()
w.setPadding(5,5,5,5)
let myGradient = new LinearGradient()
w.backgroundColor = new Color("#933")
myGradient.colors = [new Color("#990000"), new Color("#ff0000")]
myGradient.locations = [0.0,1]
w.backgroundGradient = myGradient
let title = w.addText("Fehler")
title.centerAlignText()
title.textColor = Color.white()
title.font = Font.semiboldSystemFont(24)
title.minimumScaleFactor = 0.5
let reasonText = w.addText(reason)
reasonText.centerAlignText()
reasonText.textColor = Color.white()
reasonText.font = Font.semiboldSystemFont(12)
reasonText.minimumScaleFactor = 0.5
return w
}
//
// JSON holen
//
async function loadItems(APIurl) {
let req = new Request(APIurl)
let json = await req.loadJSON()
return json
}
function creatProgress(BarValue1, BarValue2) {
BarValue1 = Math.round(BarValue1)
BarValue2 = Math.round(BarValue2)
const context = new DrawContext()
context.size = new Size(BarWidth, BarHeigth)
context.opaque = false
context.respectScreenScale = true
// BG
const path = new Path()
path.addRoundedRect(new Rect(0, 0, BarWidth, BarHeigth),4,4)
context.addPath(path)
context.setFillColor(ContentBGColor)
context.fillPath()
// BarValue1
if (BarValue1 < Step1st) {BarColor1 = new Color("#bb1e10")}
if (BarValue2 < Step1st) {BarColor2 = new Color("#bb1e1075")}
if (BarValue1 >= Step1st && BarValue1 < Step2nd) {BarColor1 = new Color("#f7b500")}
else if (BarValue1 >= Step2nd) {BarColor1 = new Color("#00b347")}
if (BarValue2 >= Step1st && BarValue2 < Step2nd) {BarColor2 = new Color("#f7b50075")}
else if (BarValue2 >= Step2nd) {BarColor2 = new Color("#00b34775")}
// BarValue2
context.setFillColor(BarColor2)
const path2 = new Path()
const path2BarHeigth = (BarHeigth * (BarValue2 / StepFin) > BarHeigth) ? BarHeigth : BarHeigth * (BarValue2 / StepFin)
path2.addRoundedRect(new Rect(0, BarHeigth, BarWidth, -path2BarHeigth),2,2)
context.addPath(path2)
context.fillPath()
// BarValue1
context.setFillColor(BarColor1)
const path1 = new Path()
const path1BarHeigth = (BarHeigth * (BarValue1 / StepFin) > BarHeigth) ? BarHeigth : BarHeigth * (BarValue1 / StepFin)
path1.addRoundedRect(new Rect(0, BarHeigth, BarWidth, -path1BarHeigth),2,2)
context.addPath(path1)
context.fillPath()
context.setFont(Font.boldSystemFont(8))
context.setTextAlignedCenter()
if (BarValue1 < 90) {
context.setTextColor(SubTextColor)
context.drawTextInRect("%", new Rect(0, 3, BarWidth, BarHeigth))
} else {
context.setTextColor(BarTextColor1)
context.drawTextInRect("%", new Rect(0, BarHeigth-15, BarWidth, BarHeigth))
}
if (BarValue1 < 10) {
PosCorr = -10
context.setTextColor(BarTextColor2)
}
else {
PosCorr = 2
context.setTextColor(BarTextColor1)
}
context.drawTextInRect(BarValue1.toString(), new Rect(0, BarHeigth-path1BarHeigth+PosCorr, BarWidth, path1BarHeigth-PosCorr))
return context.getImage()
}
//
// Regionen und Widgetparameter
//
function setRegionKey(Region) {
let Regions = { BW: "Baden-Württemberg", BY: "Bayern", BE: "Berlin", BB: "Brandenburg", HB: "Bremen", HH: "Hamburg", HE: "Hessen", MV: "Mecklenburg-Vorpommern", NI: "Niedersachsen", NW: "Nordrhein-Westfalen", RP: "Rheinland-Pfalz", SL: "Saarland", SN: "Sachsen", ST: "Sachsen-Anhalt", SH: "Schleswig-Holstein", TH: "Thüringen", DE: "Deutschland" };
if (Regions.hasOwnProperty(Region)) {
result = Regions[Region]
} else {
result = false
}
return result
}
// End of Script