Skip to content

Instantly share code, notes, and snippets.

@schaechinger
Forked from Sillium/telekom.js
Last active October 21, 2020 21:00
Show Gist options
  • Save schaechinger/45f7ee303d449e58e440503e411751a5 to your computer and use it in GitHub Desktop.
Save schaechinger/45f7ee303d449e58e440503e411751a5 to your computer and use it in GitHub Desktop.
const apiUrl = "https://pass.telekom.de/api/service/generic/v1/status";
let widget = await createWidget();
const gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [new Color("#d10063"), new Color("#e20074")];
widget.backgroundGradient = gradient;
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function createWidget(items) {
const fm = FileManager.local();
const dir = fm.documentsDirectory();
const path = fm.joinPath(dir, "scriptable-telekom.json");
const list = new ListWidget();
list.addSpacer(14);
const labels = [];
// Telekom - header
const headerLabel = list.addText("Telekom");
labels.push(headerLabel);
headerLabel.font = Font.boldSystemFont(14);
let data,
isLiveData = false;
try {
const req = new Request(apiUrl);
req.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",
};
try {
// Fetch data from pass.telekom.de
data = await req.loadJSON();
data.lastUpdateTime = new Date();
// Write JSON to iCloud file
fm.writeString(path, JSON.stringify(data, null, 2));
isLiveData = true;
} catch (err) {
// Read data from iCloud file
data = JSON.parse(fm.readString(path), null);
}
// % - available percentage
const availableLabel = list.addText(100 - data.usedPercentage + "%");
availableLabel.font = Font.boldSystemFont(36);
availableLabel.textColor = Color.green();
if (10 >= data.usedPercentage) {
availableLabel.textColor = Color.red();
} else if (25 <= data.usedPercentage) {
availableLabel.textColor = Color.orange();
}
if (!isLiveData) {
labels.push(availableLabel);
}
// gb - volume
const volumeLabel = list.addText(
`${data.usedVolumeStr} / ${data.initialVolumeStr}`
);
labels.push(volumeLabel);
volumeLabel.font = Font.mediumSystemFont(12);
list.addSpacer(12);
// days - remaining days
if (data.remainingTimeStr) {
const remainingHeaderLabel = list.addText("Verbleibende Zeit:");
labels.push(remainingHeaderLabel);
remainingHeaderLabel.font = Font.mediumSystemFont(12);
const remainingLabel = list.addText(data.remainingTimeStr);
labels.push(remainingLabel);
remainingLabel.font = Font.mediumSystemFont(12);
}
} catch (err) {
list.addText("Fehler beim Datenabruf");
}
// Add time of last successful widget update
list.addSpacer(4);
const date = new Date(data.lastUpdateTime);
const timeLabel = list.addText(date.toTimeString().substr(0, 5));
labels.push(timeLabel);
timeLabel.font = Font.mediumSystemFont(10);
timeLabel.centerAlignText();
if (!isLiveData) {
timeLabel.textColor = Color.darkGray();
}
labels.forEach((label) => {
label.textColor = Color.white();
});
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment