-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
telekom-data-usage.js
405 lines (342 loc) · 10.9 KB
/
telekom-data-usage.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: purple; icon-glyph: magic;
//########## SETUP ###########
const BACKGROUND_COLOR = Color.dynamic(
new Color("#ffffff"),
new Color("#161618")
);
const TELEKOM_COLOR = new Color("#ea0a8e");
const TEXT_COLOR = Color.dynamic(new Color("000000"), new Color("#ffffff"));
//########## END OF SETUP ##########
const apiURL = "https://pass.telekom.de/api/service/generic/v1/status";
const imgURL =
"https://raw.githubusercontent.com/marcjulianschwarz/telekom-data-usage-widget/main/symbols/";
let parameters = await args.widgetParameter;
if (parameters != null) {
parameters = parameters.toLowerCase();
} else {
parameters = "icloud";
}
let widgetSize = config.widgetFamily;
let fm = FileManager.iCloud();
if (parameters.includes("local")) {
fm = FileManager.local();
} else if (parameters.includes("icloud")) {
fm = FileManager.iCloud();
}
let dir = fm.joinPath(fm.documentsDirectory(), "telekom-widget");
let path = fm.joinPath(dir, "telekom-data.json");
if (!fm.fileExists(dir)) {
fm.createDirectory(dir);
}
let wifi = false;
let df = new DateFormatter();
df.useShortDateStyle();
// FONTS USED BY THE WIDGET
let thin_font = Font.regularRoundedSystemFont(13);
let small_font = Font.regularRoundedSystemFont(11);
let bold_font = Font.heavyRoundedSystemFont(13);
let title_font = Font.heavyRoundedSystemFont(11);
if (widgetSize == "medium") {
thin_font = Font.regularRoundedSystemFont(15);
small_font = Font.regularRoundedSystemFont(13);
bold_font = Font.heavyRoundedSystemFont(15);
title_font = Font.heavyRoundedSystemFont(17);
}
// HELPER CLASS
class Telekom {
constructor(
name,
usedVolume,
initialVolume,
remainingVolume,
usedPercentage,
validUntil,
usedAt
) {
this.name = name;
this.usedVolume = usedVolume;
this.initialVolume = initialVolume;
this.remainingVolume = remainingVolume;
this.usedPercentage = usedPercentage;
this.validUntil = validUntil;
this.usedAt = usedAt;
}
}
// HELPER FUNCTION TO CALCULATE DAYS AND HOURS FOR VALIDUNTIL
function getDaysHours(data) {
now = new Date(Date.now());
hours_total = (data.validUntil - now) / 1000 / 60 / 60;
hours = hours_total % 24;
days = (hours_total - hours) / 24;
return days + "d " + Math.round(hours) + "h";
}
// Get all data and process
async function getFromApi() {
let request = new Request(apiURL);
request.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",
};
data = await request.loadJSON();
return data;
}
async function saveImages() {
console.log("loading and saving images");
var imgs = ["full", "half", "low", "empty", "almost"];
for (img of imgs) {
let img_path = fm.joinPath(dir, img + ".png");
if (!fm.fileExists(img_path)) {
console.log("Loading image: " + img + ".png");
let request = new Request(imgURL + img + ".png");
console.log(request);
image = await request.loadImage();
fm.writeImage(img_path, image);
}
}
}
async function getImageFor(name) {
let img_path = fm.joinPath(dir, name + ".png");
await fm.downloadFileFromiCloud(img_path);
img = await fm.readImage(img_path);
return img;
}
async function saveData(data) {
data.savedDate = Date.now();
fm.writeString(path, JSON.stringify(data));
console.log("Saved new data to file");
}
async function getFromFile() {
await fm.downloadFileFromiCloud(path);
data = await JSON.parse(fm.readString(path));
console.log("Fetching data from file was successful");
return data;
}
async function processData(data) {
var name = data.passName;
var usedVolumeNum = data.usedVolume / 1024 / 1024 / 1024;
var initialVolumeNum = data.initialVolume / 1024 / 1024 / 1024;
var usedVolume =
String(Math.round((usedVolumeNum + Number.EPSILON) * 100) / 100).replace(
".",
","
) + " GB";
var initialVolume =
String(Math.round((initialVolumeNum + Number.EPSILON) * 100) / 100).replace(
".",
","
) + " GB";
var remainingVolumeNum =
(data.initialVolume - data.usedVolume) / 1024 / 1024 / 1024;
var remainingVolume =
String(Math.round((remainingVolumeNum + Number.EPSILON) * 100) / 100) +
" GB";
remainingVolume = remainingVolume.replace(".", ",");
var usedPercentage = data.usedPercentage;
var date = data.savedDate + data.remainingSeconds * 1000 - 60 * 1000;
var validUntil = new Date(date);
var telekom = new Telekom(
name,
usedVolume,
initialVolume,
remainingVolume,
usedPercentage,
validUntil
);
return telekom;
}
// Create all widgets
function createFirstWidget() {
first = new ListWidget();
first.addText(
"Wlan für die erste Einrichtung ausschalten und Widget erneut erstellen."
);
return first;
}
async function createErrorWidget(err) {
err_widget = new ListWidget();
err_widget.addText(err);
return err_widget;
}
async function createSmallVisualWidget(data) {
var small_visual_widget = new ListWidget();
var img = await getImageFor("empty");
if (data.usedPercentage >= 75 && data.usedPercentage <= 95) {
img = await getImageFor("low");
} else if (data.usedPercentage >= 50 && data.usedPercentage < 75) {
img = await getImageFor("half");
} else if (data.usedPercentage >= 25 && data.usedPercentage < 50) {
img = await getImageFor("almost");
} else if (data.usedPercentage < 25) {
img = await getImageFor("full");
}
var widget_image = small_visual_widget.addImage(img);
widget_image.centerAlignImage();
small_visual_widget.backgroundColor = BACKGROUND_COLOR;
return small_visual_widget;
}
async function createSmallWidget(data) {
var widget = new ListWidget();
var header_stack = widget.addStack();
var title = header_stack.addText(data.name);
// WIFI Symbol
if (wifi) {
header_stack.addSpacer();
let symbol_image = SFSymbol.named("wifi.exclamationmark").image;
var symbol = header_stack.addImage(symbol_image);
symbol.imageSize = new Size(17, 17);
symbol.tintColor = TEXT_COLOR;
}
widget.addSpacer();
var used_txt = widget.addText(
"Benutzt: \n" + data.usedVolume + " (" + data.usedPercentage + "%)"
);
widget.addSpacer();
var available_txt = widget.addText(
data.remainingVolume + " von " + data.initialVolume
);
widget.addSpacer();
var footer = widget.addText(
"Bis: " +
df.string(data.validUntil).toLocaleString() +
" (" +
getDaysHours(data) +
")"
);
// ASSIGNING FONTS and COLORS
title.font = title_font;
available_txt.font = bold_font;
used_txt.font = thin_font;
footer.font = small_font;
title.textColor = TELEKOM_COLOR;
used_txt.textColor = TEXT_COLOR;
footer.textColor = TEXT_COLOR;
// COLORING BASED ON DATA PERCENTAGE
if (data.usedPercentage >= 75) {
available_txt.textColor = Color.red();
} else if (data.usedPercentage >= 50 && data.usedPercentage < 75) {
available_txt.textColor = Color.orange();
} else if (data.usedPercentage >= 25 && data.usedPercentage < 50) {
available_txt.textColor = Color.yellow();
} else {
available_txt.textColor = Color.green();
}
widget.backgroundColor = BACKGROUND_COLOR;
return widget;
}
async function createMediumWidget(data) {
medium_widget = new ListWidget();
var body_stack = medium_widget.addStack();
var info_stack = body_stack.addStack();
body_stack.addSpacer();
var visual_stack = body_stack.addStack();
// Stack Layout
body_stack.layoutHorizontally();
info_stack.layoutVertically();
visual_stack.layoutVertically();
var title = info_stack.addText(data.name);
info_stack.addSpacer();
var used_txt = info_stack.addText(
"Benutzt: " + data.usedVolume + " (" + data.usedPercentage + "%)."
);
info_stack.addSpacer();
var available_txt = info_stack.addText(
data.remainingVolume + " von " + data.initialVolume
);
info_stack.addSpacer();
var footer = info_stack.addText(
"Bis: " +
df.string(data.validUntil).toLocaleString() +
" (" +
getDaysHours(data) +
")"
);
img = await getImageFor("empty");
// COLORING BASED ON DATA PERCENTAGE
if (data.usedPercentage >= 75 && data.usedPercentage <= 95) {
available_txt.textColor = Color.red();
img = await getImageFor("low");
} else if (data.usedPercentage >= 50 && data.usedPercentage < 75) {
available_txt.textColor = Color.orange();
img = await getImageFor("half");
} else if (data.usedPercentage >= 25 && data.usedPercentage < 50) {
available_txt.textColor = Color.yellow();
img = await getImageFor("almost");
} else if (data.usedPercentage < 25) {
available_txt.textColor = Color.green();
img = await getImageFor("full");
} else if (data.usedPercentage > 95) {
available_txt.textColor = Color.red();
}
visual_stack.addSpacer();
visual_stack.addImage(img);
// ASSIGNING FONTS and COLORS
title.font = title_font;
available_txt.font = bold_font;
used_txt.font = thin_font;
footer.font = small_font;
title.textColor = TELEKOM_COLOR;
used_txt.textColor = TEXT_COLOR;
footer.textColor = TEXT_COLOR;
medium_widget.backgroundColor = BACKGROUND_COLOR;
return medium_widget;
}
async function createLargeWidget(data) {
var large_widget = new ListWidget();
var img = await getImageFor("empty");
if (data.usedPercentage >= 75 && data.usedPercentage <= 95) {
img = await getImageFor("low");
} else if (data.usedPercentage >= 50 && data.usedPercentage < 75) {
img = await getImageFor("half");
} else if (data.usedPercentage >= 25 && data.usedPercentage < 50) {
img = await getImageFor("almost");
} else if (data.usedPercentage < 25) {
img = await getImageFor("full");
}
var widget_image = large_widget.addImage(img);
widget_image.centerAlignImage();
large_widget.backgroundColor = BACKGROUND_COLOR;
return large_widget;
}
// Runtime:
await saveImages();
try {
data = await getFromApi();
saveData(data);
} catch {
wifi = true;
console.log(
"Couldnt fetch data from API. Wifi still on? Trying to read from file."
);
}
if (!fm.fileExists(path)) {
console.log("File doesnt exist. Looks like your first init.");
first = await createFirstWidget();
Script.setWidget(first);
} else {
data = await getFromFile();
processedData = await processData(data);
widget = createErrorWidget("Widget couldnt be created");
switch (widgetSize) {
case "small":
if (parameters.includes("visual")) {
widget = await createSmallVisualWidget(processedData);
} else {
widget = await createSmallWidget(processedData);
}
break;
case "medium":
widget = await createMediumWidget(processedData);
break;
case "large":
widget = await createLargeWidget(processedData);
break;
default:
widget = await createSmallWidget(processedData);
}
widget.presentSmall();
widget.url = "https://pass.telekom.de";
Script.setWidget(widget);
}
Script.complete();