-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telekom Mobiles Datenvolumen
94 lines (78 loc) · 2.7 KB
/
Telekom Mobiles Datenvolumen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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)
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 data from iCloud, not Telekom:
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
}