-
-
Save darthsoup/fd0d4e5065c1c418a437d18c44b66d2a to your computer and use it in GitHub Desktop.
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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: brown; icon-glyph: magic; | |
const getUsageColor = (usage) => { | |
usage = parseInt(usage) | |
if (usage >= 90) { | |
return Color.red(); | |
} else if (usage >= 75) { | |
return Color.orange(); | |
} else { | |
return Color.green(); | |
} | |
} | |
let items = await fetch() | |
let widget = await createWidget(items) | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(items) { | |
data = items | |
const list = new ListWidget() | |
list.useDefaultPadding() | |
try { | |
// Logo | |
const imgRow = list.addStack() | |
imgRow.layoutHorizontally() | |
imgRow.addSpacer() | |
const logoImgObj = await getImage('logo') | |
const logo = imgRow.addImage(logoImgObj) | |
logo.imageSize = new Size(32, 32) | |
list.addSpacer() | |
// Usage Stack | |
const usageContent = list.addStack() | |
usageContent.layoutHorizontally() | |
usageContent.useDefaultPadding() | |
usageContent.bottomAlignContent() | |
const usageText = usageContent.addText(`${data.usedPercentage}%`) | |
usageText.font = Font.boldSystemFont(24) | |
usageText.textColor = getUsageColor(data.usedPercentage) | |
// volumeIndicator Stack | |
const volumeIndicator = list.addText(data.usedVolumeStr + " / " + data.initialVolumeStr) | |
volumeIndicator.font = Font.mediumSystemFont(12) | |
list.addSpacer(16) | |
let remainingTimeLabelText, remainingTimeValueText | |
if (data.remainingTimeStr) { | |
remainingTimeLabelText = list.addText('Remaining time'.toUpperCase()) | |
remainingTimeLabelText.font = Font.mediumSystemFont(13) | |
remainingTimeLabelText.minimumScaleFactor = 0.8 | |
remainingTimeLabelText.textColor = Color.lightGray() | |
remainingTimeValueText = list.addText(data.remainingTimeStr) | |
remainingTimeValueText.font = Font.lightSystemFont(11) | |
remainingTimeValueText.textColor = Color.lightGray() | |
} | |
} catch(err) { | |
list.addText(err) | |
} | |
// Add time of last widget refresh: | |
list.addSpacer(4) | |
const now = new Date(); | |
const timeLabel = list.addDate(now) | |
timeLabel.font = Font.mediumSystemFont(7) | |
timeLabel.rightAlignText() | |
timeLabel.applyTimeStyle() | |
timeLabel.textColor = Color.darkGray() | |
return list | |
} | |
async function fetch() { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, "scriptable-telekom.json") | |
let r = new Request("https://pass.telekom.de/api/service/generic/v1/status") | |
// 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" | |
} | |
let data, fresh = 0 | |
try { | |
// Fetch data from pass.telekom.de | |
data = await r.loadJSON() | |
// Write JSON to iCloud file | |
fm.writeString(path, JSON.stringify(data, null, 2)) | |
fresh = 1 | |
return data | |
} 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 | |
} | |
return data | |
} | |
} | |
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 'logo': | |
imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Telekom_Logo_2013.svg/2000px-Telekom_Logo_2013.svg.png" | |
break | |
default: | |
console.log(`Sorry, couldn't find ${image}.`); | |
} | |
let iconImage = await loadImage(imageUrl) | |
fm.writeImage(path, iconImage) | |
return iconImage | |
} | |
} | |
async function loadImage(imgUrl) { | |
const req = new Request(imgUrl) | |
return await req.loadImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IOS Widget für Telekom Datenvolumen
Alles wie beim original, nur bisschen anders gestyled