Last active
February 18, 2024 11:14
-
-
Save dennismetz/611b1e8f64c2160c8f24253f792fa0b6 to your computer and use it in GitHub Desktop.
iOS widget powered by the Scriptable app that shows the current capacity of your XtraFIT gym
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script for scriptable to get the current capacity of XTRAFIT Gyms | |
* | |
* Version 1.0.0 | |
* | |
* Author: Dennis Metz | |
* Website: https://www.dmetz.de | |
*/ | |
let allGymsWithLocationIds = { | |
"Bonn-Zentrum": 155160, | |
"Essen-Vogelheim": 157483, | |
"Frankfurt-Nord": 160171, | |
"Hamburg-Wandsbek": 160173, | |
"Köln-Buchheim": 160174, | |
"Köln-Ehrenfeld": 160176, | |
"Köln-Marsdorf": 160177, | |
"Köln-Ossendorf": 160178, | |
"Köln-Poll": 160179, | |
"Krefeld-Cracau": 160180, | |
"Langenfeld-Berghausen": 160181, | |
"Mainz-Bretzenheim": 160183, | |
"Offenbach-Zentrum": 160184, | |
"Reutlingen-Mitte": 196775, | |
"Sindelfingen-Goldbach": 168112, | |
"Unna-Kamen Karree": 160185, | |
"Wiesbaden-Hauptbahnhof": 160186, | |
}; | |
// add gym id here | |
const gymId = 160186 | |
let param = args.widgetParameter | |
if (param != null && param.length > 0) | |
gymId = param | |
let gymDetails = await fetchDetails(gymId) | |
const widget = new ListWidget() | |
await createWidget() | |
if (!config.runsInWidget) | |
await widget.presentSmall() | |
Script.setWidget(widget) | |
Script.complete() | |
//Create the widget | |
async function createWidget() { | |
const headlineText = widget.addText("🏋🏼 XTRAFIT Auslastung") | |
headlineText.font = Font.mediumRoundedSystemFont(16) | |
widget.addSpacer() | |
const widgetStack = widget.addStack() | |
widgetStack.layoutVertically() | |
widgetStack.bottomAlignContent() | |
const capacityText = widgetStack.addText(gymDetails.currentGymCapacityInPercentage + "%") | |
capacityText.font = Font.mediumRoundedSystemFont(40) | |
if (gymDetails.currentGymCapacityInPercentage < 65) { | |
capacityText.textColor = new Color("#33cc33") | |
} else if (gymDetails.currentGymCapacityInPercentage < 85){ | |
capacityText.textColor = new Color("#ff9900") | |
} else { | |
capacityText.textColor = new Color("#ff3300") | |
} | |
widgetStack.addSpacer(5) | |
const gymNameText = widgetStack.addText(gymDetails.webName) | |
gymNameText.font = Font.regularSystemFont(12) | |
widgetStack.addSpacer(2) | |
const gymCapacityText = widgetStack.addText("(" + gymDetails.currentlyCheckedInCount + "/" + gymDetails.maximumAllowedCheckedIn + ")") | |
gymCapacityText.font = Font.regularSystemFont(10) | |
} | |
// fetch the current capacity of the XTRAFit gym | |
async function fetchDetails(id) { | |
const url = `https://api.myfitapp.de/metric/display/${id}`; | |
const request = new Request(url); | |
const response = await request.loadString(); | |
const regexPercentage = /data-display-percentage="(\d+)"/; | |
const percentageMatch = response.match(regexPercentage); | |
let percentage = 0; | |
if (percentageMatch) { | |
percentage = parseInt(percentageMatch[1], 10); | |
if (isNaN(percentage)) { | |
percentage = 0; | |
} | |
} | |
const regexCurrentAmount = /data-display-val="(\d+)"/; | |
const currentAmountMatch = response.match(regexCurrentAmount); | |
let currentlyCheckedInCount = 0; | |
if (currentAmountMatch) { | |
currentlyCheckedInCount = parseInt(currentAmountMatch[1], 10); | |
if (isNaN(currentlyCheckedInCount)) { | |
currentlyCheckedInCount = 0; | |
} | |
} | |
const regexMaxAmount = /data-max="(\d+)"/; | |
const maxAmountMatch = response.match(regexMaxAmount); | |
let maximumAllowedCheckedIn = 0; | |
if (maxAmountMatch) { | |
maximumAllowedCheckedIn = parseInt(maxAmountMatch[1], 10); | |
if (isNaN(maximumAllowedCheckedIn)) { | |
maximumAllowedCheckedIn = 0; | |
} | |
} | |
let webName = 'Studio nicht gefunden'; | |
for (let key in allGymsWithLocationIds) { | |
if(allGymsWithLocationIds[key] === id) { | |
webName = key; | |
break; | |
} | |
} | |
return { | |
webName: webName, | |
currentGymCapacityInPercentage: percentage, | |
currentlyCheckedInCount: currentlyCheckedInCount, | |
maximumAllowedCheckedIn: maximumAllowedCheckedIn | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork of https://gist.github.com/eopo/f0dc692f23e2373000df6134b7b4e4e0