Skip to content

Instantly share code, notes, and snippets.

@hervehobbes
Forked from marco79cgn/vaccination-stats.js
Created December 31, 2020 07:22
Show Gist options
  • Save hervehobbes/fde2e30fac670d156861ea11a04f5d37 to your computer and use it in GitHub Desktop.
Save hervehobbes/fde2e30fac670d156861ea11a04f5d37 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 cacheMinutes = 60 // 60 min
const today = new Date();
let result
let widget = new ListWidget()
widget.setPadding(8, 8, 8, 8)
widget.url = "https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Daten/Impfquoten-Tab.html"
await getNumbers()
await createWidget()
Script.setWidget(widget)
Script.complete()
if (config.runsInApp) {
widget.presentSmall()
}
async function createWidget() {
let day = today.getDate().toString()
const upperStack = widget.addStack()
upperStack.layoutHorizontally()
let image = await getImage('vaccination-logo.png')
let logoImage = upperStack.addImage(image)
logoImage.imageSize = new Size(60, 60)
upperStack.addSpacer(12)
let calendarStack = upperStack.addStack()
calendarStack.layoutVertically()
calendarStack.addSpacer(4)
let dayNameText = calendarStack.addText(getWeekday(today).toUpperCase())
dayNameText.textColor = Color.red()
if (getWeekday(today) === "Donnerstag") {
dayNameText.font = Font.boldSystemFont(8)
} else {
dayNameText.font = Font.boldSystemFont(10)
}
let spacer = " "
if (day < 10) {
spacer = " "
}
let dayText = calendarStack.addText(spacer + day)
dayText.font = Font.semiboldSystemFont(26)
let dateText = calendarStack.addText(getMonthName(today))
dateText.font = Font.boldSystemFont(11)
widget.addSpacer(8)
result.vaccinated
let staticText = widget.addText("Anzahl Geimpfter:")
staticText.font = Font.boldSystemFont(11)
let amountText = widget.addText(result.vaccinated.toLocaleString())
amountText.font = Font.boldSystemFont(16)
widget.addSpacer(3)
const lastUpdateDate = new Date(result.lastUpdate)
let lastUpdatedText = widget.addText("Stand: " + lastUpdateDate.toLocaleDateString())
lastUpdatedText.textColor = Color.gray()
lastUpdatedText.font = Font.mediumSystemFont(10)
}
function getWeekday(date) {
var weekday = new Array(7);
weekday[0] = "Sonntag";
weekday[1] = "Montag";
weekday[2] = "Dienstag";
weekday[3] = "Mittwoch";
weekday[4] = "Donnerstag";
weekday[5] = "Freitag";
weekday[6] = "Samstag";
return weekday[date.getDay()];
}
function getMonthName(date) {
var monthName = new Array(12);
monthName[0] = " Januar";
monthName[1] = " Februar";
monthName[2] = " März";
monthName[3] = " April";
monthName[4] = " Mai";
monthName[5] = " Juni";
monthName[6] = " Juli";
monthName[7] = " August";
monthName[8] = "September";
monthName[9] = " Oktober";
monthName[10] = "November";
monthName[11] = "Dezember";
return monthName[date.getMonth()];
}
// get images from iCloud or download them once
async function getImage(image) {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, image)
if (fm.fileExists(path)) {
return fm.readImage(path)
} else {
// download once
let imageUrl
switch (image) {
case 'vaccination-logo.png':
imageUrl = "https://cdn2.iconfinder.com/data/icons/corona-virus-covid-19-14/512/9_Flu_protection_vaccine_virus-512.png"
break
default:
console.log(`Sorry, couldn't find ${image}.`);
}
let req = new Request(imageUrl)
let loadedImage = await req.loadImage()
fm.writeImage(path, loadedImage)
return loadedImage
}
}
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") // 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 = JSON.parse(files.readString(cachePath))
} else {
console.log("Get from API")
const req2 = new Request('https://rki-vaccination-data.vercel.app/api')
result = await req2.loadJSON()
console.log("Write Data to Cache")
try {
files.writeString(cachePath, JSON.stringify(result))
} catch (e) {
console.log("Creating Cache failed!")
console.log(e)
}
}
} catch (e) {
console.error(e)
if (cacheExists) {
console.log("Get from Cache")
result = JSON.parse(files.readString(cachePath))
} else {
console.log("No fallback to cache possible. Due to missing cache.")
}
}
console.log(result.vaccinated) // Gesamtzahl an Impfungen
}
//
// Pleas copy everything until the end
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment