Skip to content

Instantly share code, notes, and snippets.

@dennismetz
Last active February 18, 2024 11:14
Show Gist options
  • Save dennismetz/611b1e8f64c2160c8f24253f792fa0b6 to your computer and use it in GitHub Desktop.
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
/**
* 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
};
}
@dennismetz
Copy link
Author

dennismetz commented Mar 4, 2023

Fork of https://gist.github.com/eopo/f0dc692f23e2373000df6134b7b4e4e0

Fork of https://gist.github.com/masselmello/6d4f4c533b98b2550ee23a7a5e6c6cff

IMG_0698

How to use:

1. You need to have Scriptable on your iOS 14 device and open it

2. Tap on the plus in the upper right corner

3. Tap on "Untitled Script" and give the script a name, for example "Gym Capacity"

4. Copy the source code from above and paste it in the blank field

5. Save it.

6. Go to your home screen and enter wiggle mode. Then tap on the plus in the upper left corner.

7. Select Scriptable from the promoted widgets, the app list or search for scriptable.

8. Select your widget size.

9. Tap on "Add widget"

10. Tap on the newly added widget.

11. Select the XTRAFIT script. On "When Interacting", you can select "Run Script", but it'll work just fine with "Open App"

12. Paste your studio ID as parameter. You can see it in the table below.

13. Finished! 😍

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