-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vodafone Mobiles Datenvolumen
234 lines (199 loc) · 6.03 KB
/
Vodafone 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
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: teal; icon-glyph: magic;
// Credits:
// - Sillium@GitHub (https://gist.github.com/Sillium/f904fb89444bc8dde12cfc07b8fa8728)
// - Chaeimg@Github (https://github.com/chaeimg/battCircle)
// How many minutes should the cache be valid
let cacheMinutes = 60;
let backColor; //Widget background color
let textColor; //Widget text color
let fillColor;
let strokeColor;
let widgetInputRAW = args.widgetParameter;
let widgetInput = null;
if (widgetInputRAW !== null) {
widgetInput = widgetInputRAW.toString().split("|");
}
// BackgroundColor|TextColor|CircleFillColor|CircleStrokeColor
if (widgetInput !== null && widgetInput.length == 4) {
backColor = widgetInput[0];
textColor = widgetInput[1];
fillColor = widgetInput[2];
strokeColor = widgetInput[3];
} else if (Device.isUsingDarkAppearance()) {
backColor = '222222';
textColor = 'EDEDED';
fillColor = 'EDEDED';
strokeColor = '121212';
} else {
backColor = 'D32D1F';
textColor = 'EDEDED';
fillColor = 'EDEDED';
strokeColor = 'B0B0B0';
}
const canvas = new DrawContext();
const canvSize = 200;
const canvTextSize = 36;
const canvWidth = 22;
const canvRadius = 80;
canvas.size = new Size(canvSize, canvSize);
canvas.respectScreenScale = true;
function sinDeg(deg) {
return Math.sin((deg * Math.PI) / 180);
}
function cosDeg(deg) {
return Math.cos((deg * Math.PI) / 180);
}
function drawArc(ctr, rad, w, deg) {
bgx = ctr.x - rad;
bgy = ctr.y - rad;
bgd = 2 * rad;
bgr = new Rect(bgx, bgy, bgd, bgd);
bgc = new Rect(0, 0, canvSize, canvSize);
canvas.setFillColor(new Color(backColor));
canvas.fill(bgc);
canvas.setFillColor(new Color(fillColor));
canvas.setStrokeColor(new Color(strokeColor));
canvas.setLineWidth(w);
canvas.strokeEllipse(bgr);
for (t = 0; t < deg; t++) {
rect_x = ctr.x + rad * sinDeg(t) - w / 2;
rect_y = ctr.y - rad * cosDeg(t) - w / 2;
rect_r = new Rect(rect_x, rect_y, w, w);
canvas.fillEllipse(rect_r);
}
}
async function getSessionCookies() {
let req;
req = new Request("https://www.vodafone.de/mint/rest/session/start")
req.method = "POST";
req.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
req.body = JSON.stringify({
"authMethod": "AAA",
"byPIN": false,
"additionalParameters": {
"deviceType": "Smartphone"
}
})
try {
let res = await req.loadJSON()
return { cookies: req.response.cookies, msisdn: res.msisdn}
} catch (e) {
console.log(e)
throw e
}
};
async function getUsage() {
let {cookies, msisdn} = await getSessionCookies();
let CookieValues = cookies.map(function(v){
return v.name + "=" + v.value
})
let req;
req = new Request(`https://www.vodafone.de/api/enterprise-resources/core/bss/sub-nil/mobile/payment/service-usages/subscriptions/${msisdn}/unbilled-usage`)
req.headers = {
'x-vf-api': '1499082775305',
'Referer': 'https://www.vodafone.de/meinvodafone/services/',
'Accept': 'application/json',
'Cookies': CookieValues.join(';')
}
try {
let res = await req.loadJSON()
let datenContainer = res['serviceUsageVBO']['usageAccounts'][0]['usageGroup'].find(function(v){
return v.container == "Daten"
})
let datenvolumen = datenContainer.usage.find(function(v){
return v.code == "-1"
})
console.log(datenvolumen)
return {
total: datenvolumen.total,
used: datenvolumen.used,
remaining: datenvolumen.remaining
}
} catch (e) {
console.log(e)
throw e
}
};
var today = new Date();
// Set up the file manager.
const files = FileManager.local()
// Set up cache .
const cachePath = files.joinPath(files.documentsDirectory(), "widget-vodafone")
const cacheExists = files.fileExists(cachePath)
const cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
// Get Data
let data;
let lastUpdate
try {
// If cache exists and it's been less than 30 minutes since last request, use cached data.
if (cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheMinutes * 60 * 1000)) {
console.log("Get from Cache")
data = JSON.parse(files.readString(cachePath))
lastUpdate = cacheDate
} else {
console.log("Get from API")
data = await getUsage()
files.writeString(cachePath, JSON.stringify(data))
lastUpdate = today
}
} catch (e) {
console.log(e)
console.log("Get from Cache")
data = JSON.parse(files.readString(cachePath))
lastUpdate = cacheDate
}
// Create Widget
let widget = new ListWidget();
widget.setPadding(10, 10, 10, 10)
widget.backgroundColor = new Color(backColor)
let provider = widget.addText("Vodafone")
provider.font = Font.mediumSystemFont(12)
provider.color = new Color(textColor)
widget.addSpacer()
let remainingPercentage = (100 / data.total * data.remaining).toFixed(0);
drawArc(
new Point(canvSize / 2, canvSize / 2),
canvRadius,
canvWidth,
Math.floor(remainingPercentage * 3.6)
);
const canvTextRect = new Rect(
0,
100 - canvTextSize / 2,
canvSize,
canvTextSize
);
canvas.setTextAlignedCenter();
canvas.setTextColor(new Color(textColor));
canvas.setFont(Font.boldSystemFont(canvTextSize));
canvas.drawTextInRect(`${remainingPercentage}%`, canvTextRect);
const canvImage = canvas.getImage();
let image = widget.addImage(canvImage);
image.centerAlignImage()
widget.addSpacer()
// Total Values
let remainingGB = (data.remaining / 1024).toFixed(2)
let totalGB = (data.total / 1024).toFixed(0)
let totalValuesText = widget.addText(`${remainingGB} GB von ${totalGB} GB`)
totalValuesText.font = Font.mediumSystemFont(12)
totalValuesText.centerAlignText()
totalValuesText.color = new Color(textColor)
// Last Update
widget.addSpacer(5)
let lastUpdateText = widget.addDate(lastUpdate)
lastUpdateText.font = Font.mediumSystemFont(10)
lastUpdateText.centerAlignText()
lastUpdateText.applyTimeStyle()
lastUpdateText.textColor = Color.lightGray()
if(!config.runsInWidget) {
await widget.presentSmall()
} else {
// Tell the system to show the widget.
Script.setWidget(widget)
Script.complete()
}