-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloodDonationBarometerWidget.js
187 lines (153 loc) · 6.17 KB
/
bloodDonationBarometerWidget.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit:
// icon-color: deep-gray; icon-glyph: magic;
// Licence: GPL-3.0 License https://github.com/bjoerrrn/BloodDonationBarometer/blob/main/LICENSE
// Source: https://github.com/bjoerrrn/BloodDonationBarometer
// Version: 1.0.0
// Blood donation data sources categorized by organizations
const rk = {
// Deutsches Rotes Kreuz (German Red Cross)
bwh: { url: "https://www.blutspende.de/startseite", lbl: "DRK 🇩🇪 Ba-Wü, Hessen" },
nstob: { url: "https://www.blutspende-leben.de/blut-spenden", lbl: "DRK 🇩🇪 NSTOB" },
no: { url: "https://www.blutspende-nordost.de", lbl: "DRK 🇩🇪 Nord-Ost" },
// Schweizerisches Rotes Kreuz (Swiss Red Cross)
irb: { api: "interregional", lbl: "SRK 🇨🇭 Interregional" }, // no data available
aargau_solothurn: { api: "aargau_solothurn", lbl: "SRK 🇨🇭 Aargau - Solothurn" },
basel: { api: "basel", lbl: "SRK 🇨🇭 Basel" },
fribourg: { api: "fribourg", lbl: "SRK 🇨🇭 Freiburg" },
geneve: { api: "geneve", lbl: "SRK 🇨🇭 Genf" },
gesamt: { api: "schweiz_gesamt", lbl: "SRK 🇨🇭 Schweiz Gesamt" }, // no data available
graubuenden: { api: "graubuenden", lbl: "SRK 🇨🇭 Graubünden" },
neuchatel_jura: { api: "neuchatel_jura", lbl: "SRK 🇨🇭 Neuchâtel-Jura" },
nordostschweiz: { api: "nordostschweiz", lbl: "SRK 🇨🇭 Nordostschweiz" },
svizzera_italiana: { api: "svizzera_italiana", lbl: "SRK 🇨🇭 Svizzera italiana" },
zentralschweiz: { api: "zentralschweiz", lbl: "SRK 🇨🇭 Zentralschweiz" },
zuerich: { api: "zuerich", lbl: "SRK 🇨🇭 Zürich" }
};
// Widget Configuration
const backgroundColor = new Color("#000", 1);
const widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);
widget.backgroundColor = backgroundColor;
// Drawing Configuration
const contextSize = 282;
const drawContext = new DrawContext();
drawContext.size = new Size(contextSize, contextSize);
drawContext.opaque = false;
drawContext.setTextAlignedCenter();
// Layout Configuration
const vertPadding = 30;
const vertLabel = 110;
const hortImage1 = 78;
const hortImage2 = 193;
const symbolSize = 11;
// Define drawText()
function drawText(text, fontSize, x, y, color = Color.white(), bold = false) {
drawContext.setFont(bold ? Font.boldSystemFont(fontSize) : Font.systemFont(fontSize));
drawContext.setTextColor(color);
drawContext.drawText(text.toString(), new Point(x, y));
}
// `nstob` Labels & Extraction
async function extractBloodData(payload) {
const scriptMatch = payload.match(/<script[^>]+data-drupal-selector="drupal-settings-json"[^>]*>(.*?)<\/script>/s);
if (!scriptMatch) throw new Error("Could not find JSON data.");
const rawJsonString = scriptMatch[1].replace(/"/g, '"').replace(/&/g, '&');
const jsonData = JSON.parse(rawJsonString);
if (!jsonData.blutgruppen) throw new Error("No blood data found.");
const bloodData = jsonData.blutgruppen.default || jsonData.blutgruppen;
console.log("✅ Extracted Blood Data:", bloodData);
return bloodData;
}
// Swiss Red Cross API extraction
async function fetchSwissBloodData(institute) {
const url = `https://www.blutspende.ch/api/blood_supplies/${institute}?locale=de`;
const request = new Request(url);
try {
const jsonData = await request.loadJSON();
console.log(`✅ Full API Response for ${institute}:`, jsonData);
if (!jsonData || !jsonData.blood_supplies) {
console.log(`⚠️ No data available for ${institute}`);
return null;
}
return jsonData.blood_supplies;
} catch (error) {
console.log(`❌ API Error for ${institute}:`, error);
return null;
}
}
// Symbol mapping (Swiss API & Numeric)
function getSymbol(loc, i) {
if (i === undefined || i === null) {
console.log(`⚠️ Missing Data for ${loc}`);
return "?";
}
console.log(`🔍 Processing ${loc}: ${i}`);
if (loc === "nstob") {
return ["🔥", "🩸", "🩸🩸", "🩸🩸🩸", "🩸🩸🩸🩸"][i] || "?";
}
const symbols = {
"red": "🔥",
"yellow": "🩸",
"green": "🩸🩸",
"blue": "🩸🩸🩸",
"grey": "🩸🩸🩸🩸"
};
if (typeof i === "string" && symbols[i]) return symbols[i];
// Numeric Mapping for `no`, `bwh`
const value = Number(i);
if (!isNaN(value)) {
if (value <= 15) return "🔥";
if (value <= 25) return "🩸";
if (value <= 50) return "🩸🩸";
if (value <= 75) return "🩸🩸🩸";
return "🩸🩸🩸🩸";
}
return "?";
}
// buildWidget
async function buildWidget() {
drawText("🩸 Barometer", 25, 35, 25, Color.red(), true);
let loc;
try { loc = readParams(); } catch (err) {
console.log("❌ Error reading location:", err);
drawText("Bitte Standort angeben.", 20, 35, vertLabel, Color.yellow());
return;
}
try {
drawText(rk[loc].lbl, 15, 35, 75, Color.white(), true);
widget.url = rk[loc].url;
let obj;
if (rk[loc].api) {
obj = await fetchSwissBloodData(rk[loc].api);
} else {
const request = new Request(rk[loc].url);
const payload = await request.loadString();
obj = await extractBloodData(payload);
}
if (!obj) {
console.log(`⚠️ No valid data for ${loc}`);
drawText("Keine Daten verfügbar.", 15, 35, vertLabel, Color.yellow());
return;
}
console.log(`✅ Data Loaded for ${loc}`, obj);
const labels = ["A+", "B+", "AB+", "0+", "A-", "B-", "AB-", "0-"];
const keys = Object.keys(obj);
labels.forEach((label, index) => {
const x = index < 4 ? 35 : 150;
const y = vertLabel + (index % 4) * vertPadding;
const value = obj[keys[index]] || "?";
drawText(label, 20, x, y);
drawText(getSymbol(loc, value), symbolSize, index < 4 ? hortImage1 : hortImage2, y + vertPadding - 26);
});
drawText(`Stand: ${new Date().toLocaleDateString("de-DE")}`, 15, 35, vertLabel + 4 * vertPadding + 5);
} catch (err) {
console.log(`❌ Error loading data for ${loc}:`, err);
drawText("Remote Daten nicht lesbar.", 20, 35, vertLabel, Color.yellow());
}
}
// Run the script
await buildWidget();
widget.backgroundImage = drawContext.getImage();
Script.setWidget(widget);
Script.complete();
widget.presentSmall();