Skip to content

Instantly share code, notes, and snippets.

@darthsoup
Forked from Sillium/telekom.js
Last active October 24, 2020 23:01
Show Gist options
  • Save darthsoup/fd0d4e5065c1c418a437d18c44b66d2a to your computer and use it in GitHub Desktop.
Save darthsoup/fd0d4e5065c1c418a437d18c44b66d2a to your computer and use it in GitHub Desktop.
// 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()
}
@darthsoup
Copy link
Author

IOS Widget für Telekom Datenvolumen

photo5235887828058091488

Alles wie beim original, nur bisschen anders gestyled

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