Last active
March 3, 2022 21:26
-
-
Save cfoellmann/2438954f82026da3a6adcb5dfcfd466f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const apiUrl = "https://www.ai-fitness.de/studios/castrop-rauxel" | |
let widget = await createWidget() | |
widget.backgroundColor = new Color("#777777") | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(items) { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let temp = fm.temporaryDirectory() | |
let temp_path = fm.joinPath(temp, "scriptable-temp-aifitness.txt") | |
let path = fm.joinPath(dir, "scriptable-aifitness.txt") | |
const list = new ListWidget() | |
list.addSpacer(16) | |
try { | |
let r = new Request(apiUrl) | |
// API only answers for mobile Safari | |
// r.headers = { | |
// "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1", | |
// "Accept": "*/*" | |
// } | |
let data, fresh = 0 | |
try { | |
// Fetch html from website | |
data = await r.load() | |
// Write html to file | |
fm.write(temp_path, data) | |
fresh = 1 | |
} catch (err) { | |
// Read data from iCloud file | |
data = JSON.parse(fm.readString(path), null) | |
if (!data || !data.usedPercentage) { | |
const errorList = new ListWidget() | |
errorList.addText("Please disable WiFi for initial execution.") | |
return errorList | |
} | |
} | |
const line1 = list.addText("AI-Fitness") | |
line1.font = Font.mediumSystemFont(12) | |
let free | |
free = data.match(/werden.">(.*?)<\/span>/g) | |
const line2 = list.addText(free) | |
line2.font = Font.boldSystemFont(36) | |
line2.textColor = Color.green() | |
if (data.usedPercentage >= 75) { | |
line2.textColor = Color.orange() | |
} else if (data.usedPercentage >= 90) { | |
line2.textColor = Color.red() | |
} | |
const line3 = list.addText(data.usedVolumeStr + " / " + data.initialVolumeStr) | |
line3.font = Font.mediumSystemFont(12) | |
list.addSpacer(16) | |
let line4, line5 | |
if (data.remainingTimeStr) { | |
line4 = list.addText("Remaining time:") | |
line4.font = Font.mediumSystemFont(12) | |
line5 = list.addText(data.remainingTimeStr) | |
line5.font = Font.mediumSystemFont(12) | |
} | |
// Gray out if local data instead of Telekom API data: | |
if (fresh == 0) { | |
line1.textColor = Color.darkGray() | |
line2.textColor = Color.darkGray() | |
line3.textColor = Color.darkGray() | |
if (data.remainingTimeStr) { | |
line4.textColor = Color.darkGray() | |
line5.textColor = Color.darkGray() | |
} | |
} | |
} catch(err) { | |
list.addText("Error requesting webpage from https://www.ai-fitness.de/studios/castrop-rauxel") | |
} | |
// Add time of last widget refresh: | |
list.addSpacer(4) | |
const now = new Date(); | |
const timeLabel = list.addDate(now) | |
timeLabel.font = Font.mediumSystemFont(10) | |
timeLabel.centerAlignText() | |
timeLabel.applyTimeStyle() | |
timeLabel.textColor = Color.darkGray() | |
return list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment