-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
281 lines (257 loc) · 9.07 KB
/
script.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
const axios = require('axios').default;
const fs = require('fs')
require('dotenv').config()
const ChartJsImage = require('chartjs-to-image');
const moment = require('moment');
const landlinenumber = process.env.LANDLINE_NUMBER; // 02xxxxxxxx
const password = process.env.HASHED_PASSWORD;
async function getAnnonymousToken() {
const url = `https://api-my.te.eg/api/user/generatetoken?channelId=WEB_APP`;
let config = {
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en,ar;q=0.9,en-US;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Origin': 'https://my.te.eg',
'Pragma': 'no-cache',
'Referer': 'https://my.te.eg/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
}
let tokenResult = await axios.get(url, config);
console.log(tokenResult.data);
return tokenResult.data.body.jwt;
}
async function getWEToken(jwt) {
const url = `https://api-my.te.eg/api/user/login?channelId=WEB_APP`;
let data = JSON.stringify({
"header": {
"msisdn": landlinenumber,
"numberServiceType": "FBB",
"timestamp": new Date().getTime(),
"locale": "en"
},
"body": {
"password": password
}
});
let config = {
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en,ar;q=0.9,en-US;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Jwt': jwt,
'Origin': 'https://my.te.eg',
'Pragma': 'no-cache',
'Referer': 'https://my.te.eg/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
}
let tokenResult = await axios.post(url, data, config);
console.log(tokenResult);
if (!tokenResult.data.body) {
throw new Error("login failed, incorrect landline number or password");
}
// console.log(tokenResult);
return { jwt: tokenResult.data.body.jwt, customerId: tokenResult.data.header.customerId };
}
async function getCurrentUsage(jwt, customerId) {
const url = `https://api-my.te.eg/api/line/freeunitusage`;
let data = JSON.stringify({
"header": {
"customerId": customerId,
"msisdn": landlinenumber,
"numberServiceType": "FBB",
"locale": "en"
}
});
let config = {
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en,ar;q=0.9,en-US;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Jwt': jwt,
'Origin': 'https://my.te.eg',
'Pragma': 'no-cache',
'Referer': 'https://my.te.eg/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
};
let usageResult = await axios.post(url, data, config);
return {
freeUsage: usageResult.data.body.summarizedLineUsageList.find(u => u.summaryGroupName == "ADSL_USAGE_PREPAID").freeAmount,
initialTotalAmount: usageResult.data.body.summarizedLineUsageList.find(u => u.summaryGroupName == "ADSL_USAGE_PREPAID").initialTotalAmount
}
}
function getDateWithOffset(offset) {
const currentMoment = moment().utcOffset(offset);
console.log("current moment", currentMoment);
return currentMoment;
}
async function main() {
if (!landlinenumber) {
throw new Error("landline not provided");
}
if (!password) {
throw new Error("password not provided");
}
let annoynmousToken = await getAnnonymousToken();
let { jwt, customerId } = await getWEToken(annoynmousToken);
let { freeUsage, initialTotalAmount } = await getCurrentUsage(jwt, customerId);
let now = getDateWithOffset(2);
let day = now.date(), month = now.month(), year = now.year(), hour = now.hour();
let text = `${now.toString()}|${freeUsage}/${initialTotalAmount}\n`;
let filename = `quota-logs/${day}-${month}-${year}.txt`
fs.appendFileSync(filename, text, { flag: 'a+' });
if (hour == 0 || hour == 1) { // new day: append to summary file daily
fs.appendFileSync("quota-logs/README.md", "## " + text, { flag: 'a+' });
generateChart("quota-logs/README.md");
}
}
function generateChart(dataFile) {
let file = fs.readFileSync(dataFile);
let fileContent = file.toString().split("\n");
let dates = [];
let dataset = [];
let delta = [];
// get current quota for every day
for (const line of fileContent) {
if (!line) continue;
let splt = line.split("|");
let date = new Date(splt[0].split("## "[1]));
let usage = splt[1].split("/")[0];
dates.push(date);
dataset.push(usage);
}
// get delta usage for every day
for (let i = 0; i < dataset.length; i++) {
const usage = dataset[i];
if(!dataset[i+1])
delta.push(0);
else
delta.push(dataset[i + 1] - usage );
}
const data = {
labels: dates,
datasets: [{
label: 'Usage',
backgroundColor: "#228DAC",
borderColor: "#228DAC",
fill: false,
data: dataset,
}]
};
const deltaData = {
labels: dates,
datasets: [{
label: 'Delta',
backgroundColor: "#AC8D22",
borderColor: "#AC8D22",
fill: false,
data: delta,
}]
};
let chartConfig = {
type: 'line',
data: data,
options: {
responsive: true,
title: {
display: true,
text: 'Personal Landline Consumption'
},
scales: {
xAxes: [{
type: 'time',
scaleLabel: {
display: true,
labelString: 'Date'
},
ticks: {
// show ticks every four days :)
callback: function (val, index) {
return index % 4 === 0 ? val : '';
},
}
}],
yAxes: [{
type: 'linear',
scaleLabel: {
display: true,
labelString: 'value'
}
}]
}
},
};
let deltaChartConfig = {
type: 'line',
data: deltaData,
options: {
responsive: true,
title: {
display: true,
text: 'Personal Landline Consumption'
},
scales: {
xAxes: [{
type: 'time',
scaleLabel: {
display: true,
labelString: 'Date'
},
ticks: {
// show ticks every four days :)
// callback: function (val, index) {
// return index % 4 === 0 ? val : '';
// },
}
}],
yAxes: [{
type: 'linear',
scaleLabel: {
display: true,
labelString: 'value'
},
ticks: {
max: 50
}
}]
}
},
};
const myChart = new ChartJsImage();
myChart.setConfig(chartConfig);
myChart.setWidth(1400);
myChart.setWidth(800);
myChart.toFile('./chart.png');
const deltaChart = new ChartJsImage();
deltaChart.setConfig(deltaChartConfig);
deltaChart.setWidth(1400);
deltaChart.setWidth(800);
deltaChart.toFile('./delta-chart.png');
}
main()