-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
476 lines (451 loc) · 12.2 KB
/
index.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const puppeteer = require('puppeteer');
const getTime = require('date-fns/getTime');
const fs = require('fs');
require('dotenv').config();
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
let routine = JSON.parse(fs.readFileSync('./schedule.json'));
let i;
(async function () {
while (true) {
let month = new Date().getMonth();
let date = new Date().getDate();
let year = new Date().getFullYear();
let result, delay, end, close;
let day = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
][new Date().getDay()];
if (new Date().getDay() === 0) {
console.log('Holiday!');
return 10;
}
let hour = new Date().getHours();
switch (hour) {
case 9:
if (new Date().getMinutes() > 45) {
i = routine[day][0];
end = getTime(new Date(year, month, date, 10, 45, 0, 000));
close = end - Date.now();
} else {
console.log(
'You are early Today. But no worries I will take care of it'
);
console.log('Waiting for the scheduled time ....');
result = getTime(
new Date(year, month, date, 9, 46, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][0];
end = getTime(new Date(year, month, date, 10, 45, 0, 000));
close = end - Date.now();
}
break;
case 10:
if (new Date().getMinutes() < 45) {
i = routine[day][0];
end = getTime(new Date(year, month, date, 10, 45, 0, 000));
close = end - Date.now();
} else {
if (routine[day][1] == -1) {
console.log('Break is going on!! I am going to sleep');
result = getTime(
new Date(year, month, date, 12, 35, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][3];
end = getTime(
new Date(year, month, date, 13, 30, 0, 000)
);
close = end - Date.now();
} else {
i = routine[day][1];
end = getTime(
new Date(year, month, date, 11, 45, 0, 000)
);
close = end - Date.now();
}
}
break;
case 11:
if (new Date().getMinutes() < 45) {
i = routine[day][1];
end = getTime(new Date(year, month, date, 11, 45, 0, 000));
close = end - Date.now();
} else {
if (routine[day][2] == -1) {
console.log('Break is going on!! I am going to sleep');
result = getTime(
new Date(year, month, date, 12, 35, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][3];
end = getTime(
new Date(year, month, date, 13, 30, 0, 000)
);
close = end - Date.now();
} else {
i = routine[day][2];
end = getTime(
new Date(year, month, date, 12, 45, 0, 000)
);
close = end - Date.now();
}
}
break;
case 12:
if (new Date().getMinutes() < 45) {
if (routine[day][2] == -1) {
console.log('Break is going on!! I am going to sleep');
result = getTime(
new Date(year, month, date, 12, 35, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][3];
end = getTime(
new Date(year, month, date, 13, 30, 0, 000)
);
close = end - Date.now();
} else {
i = routine[day][2];
end = getTime(
new Date(year, month, date, 12, 45, 0, 000)
);
close = end - Date.now();
}
} else {
if (routine[day][3] == -1) {
console.log('Break is going on!! I am going to sleep');
result = getTime(
new Date(year, month, date, 13, 35, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][4];
end = getTime(
new Date(year, month, date, 14, 30, 0, 000)
);
close = end - Date.now();
} else {
i = routine[day][3];
end = getTime(
new Date(year, month, date, 13, 30, 0, 000)
);
close = end - Date.now();
}
}
break;
case 13:
if (new Date().getMinutes() < 30) {
if (routine[day][3] == -1) {
console.log('Break is going on!! I am going to sleep');
result = getTime(
new Date(year, month, date, 13, 35, 0, 000)
);
delay = result - Date.now();
sleep(delay);
i = routine[day][4];
end = getTime(
new Date(year, month, date, 14, 30, 0, 000)
);
close = end - Date.now();
} else {
i = routine[day][3];
end = getTime(
new Date(year, month, date, 13, 30, 0, 000)
);
close = end - Date.now();
}
} else {
i = routine[day][4];
end = getTime(new Date(year, month, date, 14, 30, 0, 000));
close = end - Date.now();
}
break;
case 14:
if (new Date().getMinutes() < 30) {
i = routine[day][4];
end = getTime(new Date(year, month, date, 14, 30, 0, 000));
close = end - Date.now();
} else {
if (routine[day][5] == -1) {
console.log('Class is over!!');
return 2;
}
i = routine[day][5];
end = getTime(new Date(year, month, date, 15, 30, 0, 000));
close = end - Date.now();
}
break;
case 15:
if (new Date().getMinutes() < 30) {
i = routine[day][5];
end = getTime(new Date(year, month, date, 15, 30, 0, 000));
close = end - Date.now();
} else {
console.log('Class is over!!');
return 2;
}
break;
default:
if (hour < 9 && hour > 7) {
console.log(
'You are too early today. Please come back after 9 am'
);
} else {
console.log('No schedule!');
}
return 3;
}
const browser = await puppeteer
.launch({
defaultViewport: null,
headless: false,
args: [
'--use-fake-ui-for-media-stream',
'--use-file-for-fake-video-capture',
],
})
.catch((e) => 'yo!!');
const page = await browser.newPage().catch((e) => 'What!!');
await page.setDefaultNavigationTimeout(0);
await page
.goto('https://cuchd.blackboard.com/ultra/', {
waitUntil: 'networkidle0',
})
.catch((e) => 'What!!');
await page
.waitForSelector('.button-1', { timeout: 40000 })
.catch(async (e) => {
while (true) {
await sleep(3000);
await page.reload();
await page
.waitForSelector('.button-1')
.catch((e) => console.log('Error!!'));
console.log(3);
let b = await page.$('.button-1');
if (b) break;
}
});
await page.click('.button-1');
await page.type('#user_id', process.env.UID);
await page.type('#password', process.env.PASSWORD);
await page.click('#entry-login');
await page.waitForNavigation({ timeout: 500000 });
await page
.waitForSelector('.course-id', { timeout: 60000 })
.catch(async (e) => {
let i = 0;
while (true) {
i++;
console.log('BB not working!!');
await sleep(2000);
await page.reload();
await sleep(3000);
await page
.waitForSelector('.course-id')
.catch((e) => console.log('Nope!!'));
let b = await page.$('.course-id');
if (b) break;
}
close -= i * 5000;
});
let demo = await page.$$('h4');
await demo[i].click().catch(async (e) => {
await page.evaluate(
'window.scrollTo(0, document.body.scrollHeight)'
);
demo = await page.$$('h4');
console.log('scroll');
await sleep(10000);
close = close - 10000;
await demo[i].click().catch((e) => console.log('Not Loaded!'));
});
await page.waitFor(10000);
close = close - 10000;
await page.waitForSelector('span.blue-link');
await page.waitFor(10000);
close = close - 10000;
let join = await page
.$$('button#sessions-list-dropdown')
.catch((e) => console.log('Not started!'));
await join[0].click().catch((e) => console.log('Can\'t join'));
let final = await page
.$x(
'/html/body/div[1]/div[2]/bb-base-layout/div/main/div[3]/div/div[3]/div/div/div/div[2]/div/div[2]/div[3]/div/div[2]/div[3]/aside/div[6]/div[2]/div[2]/div/div/ul/li[2]/a'
)
.catch((e) => console.log('MC'));
let pageTarget = await page.target();
if (final[0]) {
await final[0].click().catch((e) => {
console.log('BB ERROR!!');
});
} else {
(async function () {
console.log(
'Session has not started yet!! \nTeacher is lazy!!!'
);
let i = 0,
j = 0;
let start = Date.now();
while (true) {
if (Date.now() - start >= close) {
close = 0;
return;
}
await sleep(5000);
await page
.reload({ waitUntil: 'networkidle0' })
.catch((e) => true);
await page
.waitForSelector('button#sessions-list-dropdown', {
timeout: 40000,
})
.catch((e) => {
j++;
console.log('Not available');
});
join = await page
.$$('button#sessions-list-dropdown')
.catch((e) => console.log('BB error!!'));
console.log(join.length);
if (join[0]) {
await
(10000);
i++;
await join[0].click().catch((e) => console.log('BC'));
final = await page.$x(
'/html/body/div[1]/div[2]/bb-base-layout/div/main/div[3]/div/div[3]/div/div/div/div[2]/div/div[2]/div[3]/div/div[2]/div[3]/aside/div[6]/div[2]/div[2]/div/div/ul/li[2]/a'
);
if (final[0]) {
close = close - (i * 12000 + j * 40000);
break;
}
}
}
await final[0].click();
})();
}
const newTarget = await browser
.waitForTarget((target) => target.opener() === pageTarget, {
timeout: 900000,
})
.catch((e) => {
close = 0;
console.log('Something went seriously wrong!');
return;
});
await page.setDefaultNavigationTimeout(0);
const newPage = await newTarget.page({ timeout: 900000 });
let appUrl = newPage._target._targetInfo.url;
await newPage
.goto(appUrl, { waitUntil: 'networkidle0' })
.catch((e) => console.log('error'));
await sleep(30000);
let seconds = 0;
async function second() {
seconds++;
await sleep(30000);
console.log('second!!');
close = close - seconds * 30000;
let t = 0;
await newPage
.waitForSelector('#techcheck-modal', { timeout: 50000 })
.catch(async (e) => {
t++;
close = close - t * 30000;
await newPage.reload().catch((e) => console.log('reload!!'));
second();
});
}
second().catch(async (e) => {
await sleep(30000);
close = close - 30000;
await newPage.reload();
second();
});
async function third() {
var end1 = await newPage.$(
'.reveal-overlay > #techcheck-modal > button.close'
);
if (end1) {
await end1.click().catch(async (e) => {
await sleep(30000);
await newPage.reload();
console.log('third0!!');
second();
third();
});
} else {
await newPage.reload();
await sleep(30000);
close = close - 30000;
console.log('third!!');
second();
third();
}
}
third().catch(async () => {
await newPage.reload();
second();
third();
});
await sleep(30000);
close = close - 30000;
await newPage
.waitForSelector(`div.announcement-content`, { timeout: 900000 })
.catch((e) => {
close = close - 900000;
console.log('Not Available');
});
const finish = await newPage
.$('#announcement-modal-page-wrap > button.close')
.catch((e) => {
console.log('Not Available');
});
if (finish) {
await finish.click().catch((e) => {
console.log('Not Available');
});
} else {
console.log('Last!!');
}
/* Uncomment this block only if chat is enabled */
// await newPage.waitForSelector('div.panel-open-control', { timeout: 60000 }).catch(e =>{
// console.log('Not Available');
// });
// const chat = await newPage.$('div.panel-open-control > button.side-panel-toggle').catch(e =>{
// console.log('Not Available');
// });
// await chat.click().catch(e =>{
// console.log('Not Available');
// });;
// await sleep(1000);
// await newPage.click('button.channel-item').catch(e => 'chat not available');
// let message = '';
// if (new Date().getHours() >= 12) {
// message = 'G ood Afternoon'
// } else {
// message = 'G ood Morning'
// }
// await newPage.type('textarea#message-input', message, {delay: 60}).catch('No chat today!');
// await newPage.keyboard.press('Enter').catch('No chat today!');
await sleep(close);
await browser.close();
}
})();