Skip to content

Instantly share code, notes, and snippets.

@Sillium
Last active April 9, 2024 06:04
Show Gist options
  • Save Sillium/f904fb89444bc8dde12cfc07b8fa8728 to your computer and use it in GitHub Desktop.
Save Sillium/f904fb89444bc8dde12cfc07b8fa8728 to your computer and use it in GitHub Desktop.
const apiUrl = "https://pass.telekom.de/api/service/generic/v1/status"
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 path = fm.joinPath(dir, "scriptable-telekom.json")
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"
}
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
} 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("Telekom")
line1.font = Font.mediumSystemFont(12)
const line2 = list.addText(data.usedPercentage + "%")
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 fetching JSON from https://pass.telekom.de/api/service/generic/v1/status")
}
// 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
}
Copy link

ghost commented Nov 9, 2020

so habe ich es jetzt geändert:

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: magic;
// creator: https://github.com/Sillium | functions added https://github.com/LupusArgentum | Color and Layout edit by https://github.com/olikdesign

const apiUrl = "https://pass.telekom.de/api/service/generic/v1/status"

let widget = await createWidget()
widget.backgroundColor = new Color("#E20074")
if (!config.runsInWidget) await widget.presentSmall()
Script.setWidget(widget)
Script.complete()

async function createWidget(items) {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let jsonLocalPath = fm.joinPath(dir, "scriptable-telekom.json")
let lastFetchDateLocalPath = fm.joinPath(dir, "lastUpdate.txt")

const list = new ListWidget()
list.addSpacer()

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"
}
let data = false, api_online = false, lastFetchDate = false
try {
// Fetch data from pass.telekom.de
data = await r.loadJSON()
// Write JSON to local file
fm.writeString(jsonLocalPath, JSON.stringify(data, null, 2))
api_online = true
lastFetchDate = new Date()
fm.writeString(lastFetchDateLocalPath, lastFetchDate.toString())
} catch (err) {
console.log(err)
// Read data from local file
if (fm.fileExists(jsonLocalPath) && fm.fileExists(lastFetchDateLocalPath)) {
data = JSON.parse(fm.readString(jsonLocalPath), null)
lastFetchDate = new Date(fm.readString(lastFetchDateLocalPath, null))
} else {
const errorList = new ListWidget()
errorList.addText("Please disable WiFi for initial execution.")
return errorList
}
}

let stack = list.addStack()
let dataIcon = SFSymbol.named('antenna.radiowaves.left.and.right');
let dataIconElement = stack.addImage(dataIcon.image)
dataIconElement.imageSize = new Size(15, 15)
dataIconElement.tintColor = Color.white()
stack.addSpacer(4)
let titlename = stack.addText("Datenvolumen")
titlename.font = Font.mediumSystemFont(14)
titlename.textColor = Color.white()

const availabledata = data.initialVolumeStr - data.usedVolumeStr; const fontName = "Futura-Medium" const line2 = list.addText(availabledata) line2.font = new Font(fontName, 36) line2.textColor = new Color("#101e43") if (data.usedPercentage >= 75) { line2.textColor = Color.orange() } else if (data.usedPercentage >= 90) { line2.textColor = Color.red()
}
let row = list.addStack()
function addUsedData() {
let stack = row.addStack()
stack.layoutHorizontally()
let line3 = stack.addText(data.usedVolumeStr)
line3.font = Font.boldSystemFont(14)
line3.textColor = Color.white()
stack.addSpacer(10)
const lineSpacer = stack.addText("●")
lineSpacer.font = Font.heavySystemFont(14)
lineSpacer.rightAlignText()
lineSpacer.textColor = Color.green()
if (data.usedPercentage >= 75) {
lineSpacer.textColor = Color.orange()
} else if (data.usedPercentage >= 90) {
lineS.textColor = Color.red()
}
}
addUsedData();
const line4 = list.addText("von " + data.initialVolumeStr + " verbraucht")
line4.font = Font.mediumSystemFont(10)
line4.textColor = Color.white()
list.addSpacer(6)

list.addSpacer()
let line5

// alt text on line5 if local data instead of Telekom API data:
if (api_online) {
let plan = (data.remainingSeconds ? "prepaid" : data.remainingTimeStr ? "postpaid" : "")
switch (plan) {
case "prepaid":
let days = Math.floor(data.remainingSeconds / 86400)
let hours = Math.floor((data.remainingSeconds % 86400) / 3600)
line5 = list.addText("noch " + days + " Tage " + hours + " Std.")
line5.font = Font.mediumSystemFont(10)
line5.textColor = new Color("#101e43")
break;

case "postpaid":
  line5 = list.addText("gültig bis:\n" + data.remainingTimeStr)
  line5.font = Font.mediumSystemFont(12)
  break;

}

} else {
line5 = list.addText("API Offline")
line5.font = Font.boldSystemFont(12)
line5.textColor = new Color("#101e43")
}

// Add time (and date) of last data fetch
const df = new DateFormatter()
const wasFetchedToday = (lastFetchDate.getDate() == new Date().getDate())
df.dateFormat = (wasFetchedToday ? "HH:mm" : "dd.MM. HH:mm")

let timeLabel = list.addText("aktualisiert " + df.string(lastFetchDate))
timeLabel.font = Font.mediumSystemFont(9)
timeLabel.textColor = new Color("#101e43", 0.5)
list.addSpacer()
} catch (err) {
console.log(err)
list.addText("Error fetching JSON from https://pass.telekom.de/api/service/generic/v1/status")
}

return list
}

Aber es erscheint folgender Fehler:

2020-11-09 14:08:05: Error on line 121: SyntaxError: Unexpected keyword 'const'. Expected ';' after variable declaration.

@olikdesign
Copy link

olikdesign commented Nov 9, 2020

@HelmutTo

hier ist der funktionierende Code:

function formatBytes(a,b=2){if(0===a)return"0 Bytes";const c=0>b?0:b,d=Math.floor(Math.log(a)/Math.log(1024));return parseFloat((a/Math.pow(1024,d)).toFixed(c))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]} const availabledata = data.initialVolume - data.usedVolume; const fontName = "Futura-Medium" const line2 = list.addText(formatBytes(availabledata, 2)) line2.font = new Font(fontName, 26) line2.textColor = new Color("#101e43") if (data.usedPercentage >= 75) { line2.textColor = Color.orange() } else if (data.usedPercentage >= 90) { line2.textColor = Color.red() }

Copy link

ghost commented Nov 9, 2020 via email

@olikdesign
Copy link

@HelmutTo

Mail ist raus.

Copy link

ghost commented Nov 9, 2020

Super und Danke - so sollte es sein!

@Goldsucher
Copy link

Goldsucher commented Nov 17, 2020

Danke für die Info - hab fie lokale Ablage übersehen...

Ist „remainingSeconds“ denn nicht die Remaining Time?

damit kriegst du die verbleibende Zeit für Congstar:

let line4, line5
if (data.remainingSeconds) {
  line4 = list.addText("verbleibende Zeit:")
  line4.font = Font.mediumSystemFont(12)
  seconds = data.remainingSeconds
  var d = Math.floor(seconds / (3600*24))
  var h = Math.floor(seconds % (3600*24) / 3600)
  //line5 = list.addText(data.remainingTimeStr)
  line5 = list.addText(d + " Tage " + h + " Stunden")
  line5.font = Font.mediumSystemFont(12)
}

@Sillium
Copy link
Author

Sillium commented Nov 19, 2020

Für alle, die es interessiert, habe ich hier ein Update des Widgets gemacht: https://gist.github.com/Sillium/313164aec3d835c076ebfcd330f1be14

Neu:

  • Anzeige kann englisch oder deutsch konfiguriert werden
  • man kann wählen, ob verbrauchte oder verbleibende Daten angezeigt werden
  • es kann konfiguriert werden, wie das Widget aussehen soll
  • die verbleibende Zeit wird aus "remainingSeconds" genommen und nicht aus "remainingTimeStr", das es wohl bei Congstar usw. nicht gibt

Copy link

ghost commented Nov 20, 2020

Hi,
leider weiss ich nicht wo ich im Script auf deutsch einstellen kann, wo ich im Script das Fraenk Logo integrieren kann...
Kann mir mal jemand farblich die Passagen markieren? - Bitte?

@NoTrace-Kai
Copy link

Das mit dem fraenk Logo würde mich auch reizen ;)

@chimcen
Copy link

chimcen commented Nov 20, 2020

Das passiert alles per Widget-Parameter - Steht auch so dort...

Z.B. Fraenk mit Anzeige der verbrauchten Daten: de;used;fraenk

@Sillium
Copy link
Author

Sillium commented Nov 20, 2020

Das mit dem fraenk Logo würde mich auch reizen ;)

Bitte mal dem obigen Link (https://gist.github.com/Sillium/313164aec3d835c076ebfcd330f1be14) folgen und die paar Sätze Anleitung lesen.

@Sillium
Copy link
Author

Sillium commented Nov 20, 2020

Hi,
leider weiss ich nicht wo ich im Script auf deutsch einstellen kann, wo ich im Script das Fraenk Logo integrieren kann...
Kann mir mal jemand farblich die Passagen markieren? - Bitte?

Bitte mal dem obigen Link (https://gist.github.com/Sillium/313164aec3d835c076ebfcd330f1be14) folgen und die paar Sätze Anleitung lesen.

@chimcen
Copy link

chimcen commented Nov 20, 2020

Das mit dem fraenk Logo würde mich auch reizen ;)

Bitte mal dem obigen Link (https://gist.github.com/Sillium/313164aec3d835c076ebfcd330f1be14) folgen und die paar Sätze Anleitung lesen.

YMMD

@Sillium
Copy link
Author

Sillium commented Nov 20, 2020

Hi,
leider weiss ich nicht wo ich im Script auf deutsch einstellen kann, wo ich im Script das Fraenk Logo integrieren kann...
Kann mir mal jemand farblich die Passagen markieren? - Bitte?

Was vielleicht nicht ganz klar wurde: Ihr müsst das neue Script verwenden.

@Sillium
Copy link
Author

Sillium commented Nov 20, 2020

Das mit dem fraenk Logo würde mich auch reizen ;)

Was vielleicht nicht ganz klar wurde: Ihr müsst das neue Script verwenden.

@chimcen
Copy link

chimcen commented Nov 20, 2020

Wenn man liest war es eigentlich schon klar ;)

@NoTrace-Kai
Copy link

Ups ... jetzt habe ich es auch verstanden und es hat sogar beim ersten Versuch geklappt ;)
Danke für die Hilfe ... und Sorry für die Mühen ;)

Copy link

ghost commented Dec 23, 2020

@Sillium

Erstmal vielen Dank für die tolle Arbeit. Leider aktualisiert sich das Widget nicht selbstständig im Mobilfunknetz. Hab für 15 min WLAN abgeschaltet aber das Widget bleibt ausgegraut. Hast du eine Idee voran es liegen könnte?

@User15060164
Copy link

Bei mir wird die falsche verbleibende GB Anzahl angezeigt genauso wie die Prozent Zahl (was kann ich tun)

@Romy-006
Copy link

Wo kann ich einen JavaScript Code bekommen für ein Widget, um die mobilen Daten am Handy ein-und auszuschalten?

@marc1234S
Copy link

Super und Danke - so sollte es sein!

Könntest du mir den Code auch zuschicken

@Vortilion
Copy link

Zeigt mir seit heute plötzlich nur noch „undefined“ an. ;(

@Sillium
Copy link
Author

Sillium commented Jul 23, 2021

Zeigt mir seit heute plötzlich nur noch „undefined“ an. ;(

Das ist, weil die Telekom für diesen Monat für alle/viele Kunden unbegrenztes Datenvolumen freigeschaltet hat. Siehe https://pass.telekom.de

@sfksuperman
Copy link

I'm getting Please disable WiFi for initial execution error even when my WiFi is disabled. Please share the working script. Thanks.

@Funterrain
Copy link

Since yesterday I‘m getting
2022-06-23 09:05:39: Error on line 16:27: No file to import at Telekom/TelekomDataUsageLibrary_1.0.3.js.

@Sillium
Copy link
Author

Sillium commented Jul 5, 2022

I don't have time to fiddle around with this, but here's a version that should fix a few of the bugs: https://gist.github.com/Sillium/235dc9fd873b25e321b3299da64c9c38

Please delete the folder iCloud Drive --> Scriptable --> <script name> depending on how you named the script in your Scriptable app.

@wolflu05
Copy link

I also wrote my own version if somebody is interested. https://gist.github.com/wolflu05/05ee63670e095fbe21420645f4b31d5a However, there is a little thing which im currently trying out. I plan to have an indicator of how much data I can consume this day, so I have the same data also for the next days.

@Quickred
Copy link

Funktioniert auch mit CONGSTAR, da ja im gleichen D-Netz

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