Skip to content

Instantly share code, notes, and snippets.

@Aim23
Forked from marco79cgn/vaccination-stats.js
Created December 30, 2020 08:55
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 Aim23/7964ad22601505f70ef1e67edf1ffc00 to your computer and use it in GitHub Desktop.
Save Aim23/7964ad22601505f70ef1e67edf1ffc00 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
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 createWidget()
Script.setWidget(widget)
Script.complete()
if(config.runsInApp) {
widget.presentSmall()
}
async function createWidget() {
var today = new Date();
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(18)
let calendarStack = upperStack.addStack()
calendarStack.layoutVertically()
calendarStack.addSpacer(4)
let dayNameText = calendarStack.addText(getWeekday(today).toUpperCase())
dayNameText.textColor = Color.red()
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(10)
widget.addSpacer(8)
const number = await getLatestNumber()
let staticText = widget.addText("Anzahl Geimpfter:")
staticText.font = Font.boldSystemFont(11)
let amountText = widget.addText(number.split(" (")[0])
amountText.font = Font.boldSystemFont(16)
widget.addSpacer(3)
let lastUpdatedText = widget.addText(number.split(" (")[1].replace(")",""))
lastUpdatedText.textColor = Color.gray()
lastUpdatedText.font = Font.mediumSystemFont(10)
}
// fetches the latest numbers
async function getLatestNumber() {
let wbv = new WebView()
await wbv.loadURL("https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Daten/Impfquoten-Tab.html")
// javasript to grab data from the website
let jscript = `
result = '';
const metas = document.getElementsByTagName('meta');
for (let i = 0; i < metas.length; i++)
{
if (metas[i].getAttribute('name') === 'description')
{
result = metas[i].getAttribute('content');
}
}
JSON.stringify(result)
`
// Run the javascript
let result = await wbv.evaluateJavaScript(jscript)
let substring = result.split("Gesamtzahl der Impfungen bis einschl. ")[1].split(": ")[1]
result = substring.replace("\"", "")
return result
}
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.iCloud()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, image)
if (fm.fileExists(path)) {
await fm.downloadFileFromiCloud(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
}
}
//
// please copy until the end
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment