Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PixelPartner
Forked from marco79cgn/vaccination-stats.js
Last active December 30, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PixelPartner/2cda3d1523312b814bb1c90185105380 to your computer and use it in GitHub Desktop.
Save PixelPartner/2cda3d1523312b814bb1c90185105380 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) {
switch (date.getDay()) {
case 0: return "Sonntag";
case 1: return "Montag";
case 2: return "Dienstag";
case 3: return "Mittwoch";
case 4: return "Donnerstag";
case 5: return "Freitag";
default: return "Samstag";
}
}
function getMonthName(date) {
switch (date.getMonth()) {
case 0: return " Januar";
case 1: return " Februar";
case 2: return " März";
case 3: return " April";
case 4: return " Mai";
case 5: return " Juni";
case 6: return " Juli";
case 7: return " August";
case 8: return "September";
case 9: return " Oktober";
case 10: return "November";
default: return "Dezember";
}
}
// get images from local 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
}
//
// Please copy everything until the end
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment