Skip to content

Instantly share code, notes, and snippets.

@thorstenleidl
Forked from marco79cgn/vaccination-stats.js
Last active June 17, 2022 05:28
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thorstenleidl/75124fcb8d16b0f8531d5e50874f7e43 to your computer and use it in GitHub Desktop.
Save thorstenleidl/75124fcb8d16b0f8531d5e50874f7e43 to your computer and use it in GitHub Desktop.
A Scriptable widget that shows the amount of people who have received the corona vaccination in Germany
const version = 'v1.5'
const cacheMinutes = 60 // 60 min
const today = new Date();
let result
let widget = new ListWidget()
widget.setPadding(10, 15, 10, 10)
await getNumbers()
await createWidget()
Script.setWidget(widget)
Script.complete()
if (config.runsInApp) {
widget.presentSmall()
}
async function createWidget() {
let param
let bundesland
if (config.runsInApp) {
param = 'BY';
}
else {
param = args.widgetParameter;
}
switch (param) {
case 'BW':
bundesland = 'de.bw';
break;
case 'BY':
bundesland = 'de.by';
break;
case 'BE':
bundesland = 'de.be';
break;
case 'BB':
bundesland = 'de.bb';
break;
case 'HB':
bundesland = 'de.hb';
break;
case 'HH':
bundesland = 'de.hh';
break;
case 'HE':
bundesland = 'de.he';
break;
case 'MV':
bundesland = 'de.mv';
break;
case 'NI':
bundesland = 'de.ni';
break;
case 'NRW':
bundesland = 'de.nw';
break;
case 'RP':
bundesland = 'de.rp';
break;
case 'SL':
bundesland = 'de.sl';
break;
case 'SN':
bundesland = 'de.sn';
break;
case 'ST':
bundesland = 'de.st';
break;
case 'SH':
bundesland = 'de.sh';
break;
case 'TH':
bundesland = 'de.th';
break;
default:
bundesland = 'default';
}
if (bundesland === 'default') {
throw new Error('Kein gültiges Bundesland.')
}
for (let i = 1; i < result.length; i++) {
if (result[i][0] == 'de') {
de_data = result[i]
}
if (result[i][0] == bundesland) {
state_data = result[i]
}
}
const header = result[0]
// console.log(header)
const id = header.indexOf('id')
const date = header.indexOf('date')
const vaccinations1 = header.indexOf('vaccinations1')
const vaccinations1_quota = header.indexOf('vaccinations1_quota')
const vaccinations2 = header.indexOf('vaccinations2')
const vaccinations2_quota = header.indexOf('vaccinations2_quota')
const vaccinations3 = header.indexOf('vaccinations3')
const vaccinations3_quota = header.indexOf('vaccinations3_quota')
const vaccinations4 = header.indexOf('vaccinations4')
const vaccinations4_quota = header.indexOf('vaccinations4_quota')
result_de = {
id: de_data[id],
date: de_data[date],
vaccinations1: Number(de_data[vaccinations1]),
vaccinations1_quota: Number(de_data[vaccinations1_quota]),
vaccinations2: Number(de_data[vaccinations2]),
vaccinations2_quota: Number(de_data[vaccinations2_quota]),
vaccinations3: Number(de_data[vaccinations3]),
vaccinations3_quota: Number(de_data[vaccinations3_quota]),
vaccinations4: Number(de_data[vaccinations4]),
vaccinations4_quota: Number(de_data[vaccinations4_quota])
};
console.log('DE date: ' + result_de.date)
result_state = {
id: state_data[id],
date: state_data[date],
vaccinations1: Number(state_data[vaccinations1]),
vaccinations1_quota: Number(state_data[vaccinations1_quota]),
vaccinations2: Number(state_data[vaccinations2]),
vaccinations2_quota: Number(state_data[vaccinations2_quota]),
vaccinations3: Number(state_data[vaccinations3]),
vaccinations3_quota: Number(state_data[vaccinations3_quota]),
vaccinations4: Number(state_data[vaccinations4]),
vaccinations4_quota: Number(state_data[vaccinations4_quota])
};
console.log('State date: ' + result_state.date)
const upperStack = widget.addStack()
upperStack.layoutHorizontally()
let staticText1 = upperStack.addText("Impfungen DE 💉");
staticText1.font = Font.boldSystemFont(12);
staticText1.textColor = new Color("#00a86b");
widget.addSpacer(5)
let amountText1 = widget.addText("① " + result_de.vaccinations1.toLocaleString() + " (" + result_de.vaccinations1_quota.toLocaleString() + "%)")
amountText1.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText2 = widget.addText("② " + result_de.vaccinations2.toLocaleString() + " (" + result_de.vaccinations2_quota.toLocaleString() + "%)")
amountText2.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText3 = widget.addText("③ " + result_de.vaccinations3.toLocaleString() + " (" + result_de.vaccinations3_quota.toLocaleString() + "%)")
amountText3.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText4 = widget.addText("④ " + result_de.vaccinations4.toLocaleString() + " (" + result_de.vaccinations4_quota.toLocaleString() + "%)")
amountText4.font = Font.boldSystemFont(9)
widget.addSpacer(5)
let staticText2 = widget.addText("Impfungen " + param + " 💉")
staticText2.font = Font.boldSystemFont(12);
staticText2.textColor = new Color("#00a86b");
widget.addSpacer(2)
let amountText5 = widget.addText("① " + result_state.vaccinations1.toLocaleString() + " (" + result_state.vaccinations1_quota.toLocaleString() + "%)")
amountText5.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText6 = widget.addText("② " + result_state.vaccinations2.toLocaleString() + " (" + result_state.vaccinations2_quota.toLocaleString() + "%)")
amountText6.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText7 = widget.addText("③ " + result_state.vaccinations3.toLocaleString() + " (" + result_state.vaccinations3_quota.toLocaleString() + "%)")
amountText7.font = Font.boldSystemFont(9)
widget.addSpacer(1)
let amountText8 = widget.addText("④ " + result_state.vaccinations4.toLocaleString() + " (" + result_state.vaccinations4_quota.toLocaleString() + "%)")
amountText8.font = Font.boldSystemFont(9)
widget.addSpacer(5)
const lastUpdateDate = new Date(result_de.date)
let day = lastUpdateDate.getDate()
let month = lastUpdateDate.getMonth() + 1
let year = lastUpdateDate.getFullYear()
if (day<10)
{
day = "0" + day
}
if (month<10)
{
month = "0" + month
}
let lastUpdatedText = widget.addText("Stand: " + day + "." + month + "." + year)
lastUpdatedText.textColor = Color.gray()
lastUpdatedText.font = Font.mediumSystemFont(8)
}
async function getNumbers() {
// Set up the file manager.
const files = FileManager.local()
// Set up cache
const cachePath = files.joinPath(files.cacheDirectory(), "api-cache-covid-vaccine-numbers-" + version) // ggfs. namen anpassen
const cacheExists = files.fileExists(cachePath)
const cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
// Get Data
try {
// If cache exists and it's been less than 60 minutes since last request, use cached data.
if (cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheMinutes * 60 * 1000)) {
console.log("Get from Cache")
result_string = files.readString(cachePath)
} else {
console.log("Get from API")
const req2 = new Request('https://cdn.fnki.de/corona-data/vaccinations.rki.tsv')
result_string = await req2.loadString()
console.log("Write Data to Cache")
try {
files.writeString(cachePath, result_string)
} catch (e) {
console.log("Creating Cache failed!")
console.log(e)
}
}
} catch (e) {
console.error(e)
if (cacheExists) {
console.log("Get from Cache")
result_string = files.readString(cachePath)
} else {
console.log("No fallback to cache possible. Due to missing cache.")
}
}
const columns = result_string.split(/\r?\n/)
result = columns.map(col => col.split('\t'))
}
//
// Please copy everything until the end
//
@lululasse
Copy link

Super! Vielen Dank! Funktioniert einwandfrei!

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