-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
1092 lines (1074 loc) · 33.2 KB
/
main.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {app, BrowserWindow, ipcMain, dialog, shell, Menu} = require('electron');
const request = require('request');
const cheerio = require('cheerio');
const notifier = require('node-notifier');
/*Load Datastores*/
var Datastore = require('nedb')
, settings = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/settings.db'})
, logindb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/login.db'})
, attdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/att.db'})
, pdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/p.db'})
, ffdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/ff.db'})
, odb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/o.db'})
, padb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/pa.db'})
, mdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/m.db'})
, gdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/g.db'})
, sdb = new Datastore({ filename: app.getPath('appData')+'/wrapper/data/settings/s.db'});
settings.loadDatabase();
logindb.loadDatabase();
attdb.loadDatabase();
pdb.loadDatabase();
ffdb.loadDatabase();
odb.loadDatabase();
padb.loadDatabase();
mdb.loadDatabase();
gdb.loadDatabase();
sdb.loadDatabase();
var path = require('path');
//Variable to confirm the availability to seating plan
var planAvailable=0;
let loginScreen, mainScreen;
let image;
if (process.platform === 'darwin') {
image = path.join(__dirname, 'icons', 'mac', 'app.icns');
}
else{
image = path.join(__dirname, 'icons', 'win', 'app.ico');
}
//Creating the menu bar
var mainScreenMenu = [
{
label: 'Account',
submenu: [
{
label: 'Home',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'home');
}
},
{
label: 'My info',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'info');
}
},
{
label: 'Logout and remove data',
click: function(menuItem, BrowserWindow, event){
dialog.showMessageBox(
{
type: 'info',
buttons:['Yes', 'No'],
title: 'Logout and clear everything?',
detail: 'This will log you out of webkiosk and clear everything stored on this PC. Software may restart. You will still be able to log back in. Do you want to continue?',
}, function(response){
if(response === 0){
clear();
}
}
);
}
},
{
label: 'Close',
role: 'quit'
}
]
},
{
label: 'Fees',
submenu: [
{
label: 'Full History',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'full');
}
},
{
label: 'Online History',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'online');
}
}
]
},
{
label: 'Academic Info',
submenu: [
{
label: 'Attendance Summary',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'attSummary');
}
},
{
label: 'View Subjects/Faculties',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'faculty');
}
}
]
},
{
label: 'Exam Info',
submenu:[
{
label: 'Seating Plan',
click: function(menuItem, BrowserWindow, event){
//Check if seating plan is actually available or not
if (planAvailable === 0){
dialog.showMessageBox({
type: 'info',
buttons: ['Close'],
title: 'No data available!',
detail: 'No Datesheet available. Probably there is ample of time for exams.'
});
}
else{
//If available then proceed to render
mainScreen.webContents.send('windowShift', 'seat');
}
}
},
{
label: 'Marks',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'marks');
}
},
{
label: 'Grades',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'grade');
}
},
{
label: 'View CGPA/SGPA',
click: function(menuItem, BrowserWindow, event){
mainScreen.webContents.send('windowShift', 'cg');
}
}
]
},
{
label: 'Chrome mode',
submenu:[
{
label: 'Start',
click: function(menuItem, BrowserWindow, event){
mainScreen.loadURL('https://webkiosk.jiit.ac.in');
}
},
{
label: 'Stop',
click: function(menuItem, BrowserWindow, event){
mainScreen.loadFile('views/main.html');
}
}
]
}
];
/*Update checkup start*/
function checkUpdates(e){
//Check for the latest release with github release page
request('https://api.github.com/repos/ngudbhav/Webkiosk-Wrapper/releases/latest', {headers: {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 '}}, function(error, html, body){
if(!error){
var v = app.getVersion().replace(' ', '');
if(JSON.parse(body).tag_name){
var latestV = JSON.parse(body).tag_name.replace('v', '');
var changeLog = JSON.parse(body).body;
//If version mismatch
if(latestV!=v){
//Present the dialog
dialog.showMessageBox(
{
type: 'info',
buttons:['Open Browser to download link', 'Close'],
title: 'Update Available',
detail: changeLog,
}, function(response){
if(response === 0){
shell.openExternal('https://github.com/ngudbhav/Webkiosk-Wrapper/releases/latest');
}
}
);
//Present the notification
notifier.notify(
{
appName: "NGUdbhav.webkiosk",
title: 'Update Available',
message: 'A new version is available. Click to open browser and download.',
icon: path.join(__dirname, 'icons', 'win', 'app.ico'),
sound: true,
wait:true
});
notifier.on('click', function(notifierObject, options) {
shell.openExternal('https://github.com/ngudbhav/Webkiosk-Wrapper/releases/latest');
});
}
else{
//If clicked on check for updates
if(e === 'f'){
dialog.showMessageBox({
type: 'info',
buttons:['Close'],
title: 'No update available!',
detail: 'You already have the latest version installed.'
});
}
}
}
if(mainScreen){
mainScreen.webContents.send('updateCheckup', null);
}
else if(loginScreen){
loginScreen.webContents.send('updateCheckup', null);
}
}
else{
if(e === 'f'){
dialog.showMessageBox({
type: 'error',
buttons:['Close'],
title: 'Update check failed!',
detail: 'Failed to connect to the update server. Please check your internet connection'
});
}
if(mainScreen){
mainScreen.webContents.send('updateCheckup', null);
}
else if(loginScreen){
loginScreen.webContents.send('updateCheckup', null);
}
}
});
}
ipcMain.on('update', function(e, item){
checkUpdates('f');
});
/*Update checkup end*/
function createWindow(){
logindb.find({}, function(error, results){
if(error){
console.log(error);
//Clear the previous saved credentials and delete saved user data
logindb.remove({}, { multi: true });
createLoginScreen();
//clear db and load the login screen
}
else{
if(results.length){
results[0].saved = true;
//fallback = true;
//Proceed to login with the saved credentials
login(results[0]);
}
else{
//Clear the previous saved credentials and delete saved user data
logindb.remove({}, { multi: true });
createLoginScreen();
}
}
});
}
function createLoginScreen(){
//Create login screen
loginScreen = new BrowserWindow({width: 1000, height: 600, icon: image, webPreferences: {
nodeIntegration: true
}});
loginScreen.loadFile(path.join(__dirname, 'views', 'login.html'));
//loginScreen.openDevTools();
loginScreen.setMenu(null);
//No need of menu
loginScreen.removeMenu();
Menu.setApplicationMenu(Menu.buildFromTemplate([]));
loginScreen.on('closed', function(){
loginScreen = null;
});
}
function createMainScreen(){
//Create after login screen
mainScreen = new BrowserWindow({width: 1100, height: 600, icon:image, webPreferences: {
nodeIntegration: true
}});
mainScreen.loadFile(path.join(__dirname, 'views', 'main.html'));
var menuBuild = Menu.buildFromTemplate(mainScreenMenu);
Menu.setApplicationMenu(menuBuild);
mainScreen.on('closed', function(){
mainScreen = null;
});
mainScreen.webContents.on('did-finish-load', () => {
//Retrieve user data and send the name to the title bar
logindb.find({}, function(error, results){
if(error) throw error;
else{
settings.find({}, function(error, name){
if(error) throw error;
else{
mainScreen.webContents.send('name', {name:name[0].name});
}
});
if(results[0].password[0] == '#' || results[0].password[0] == '&'){
//Check for password reset. Very trivial Confirmation. Need confirmation
mainScreen.webContents.send('parentalLoginStatus', {status:1});
}
else{
mainScreen.webContents.send('parentalLoginStatus', {status:0});
}
//Call all the APIs to retrieve full webkiosk data of the user
!mainScreen || getAttendance();
!mainScreen || getInfo();
!mainScreen || getFullInfo();
!mainScreen || getOnlineInfo();
!mainScreen || getPA();
!mainScreen || getGrades();
!mainScreen || getMarks();
!mainScreen || getSubjects();
!mainScreen || checkUpdates();
!mainScreen || getPlan();
}
});
});
}
var headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
}
//This headers will be used to authenticate every request with webkiosk.
//After login, The cookie is inserted into the headers and every request is made with new headers.
var fallback= false;
var loginStatus = 0;
/*APIS for actions in the menu bar*/
function getAttendance(){
//Request for attendance
request({secureProtocol: 'TLSv1_method', strictSSL: false, url:"https://webkiosk.jiit.ac.in/StudentFiles/Academic/StudentAttendanceList.jsp", headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
attdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].attendance){
if(mainScreen){
mainScreen.webContents.send('attendanceSummary', results[0].attendance);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getAttendance();
//again try to retrieve the data
}
else{
//Scrapping the attendance from the webpage
var $ = cheerio.load(body);
var subjects = [];
var lect_and_tut = [];
var lect = [];
var tut = [];
var prac = [];
$('#table-1>tbody>tr').each(function(i, item){
subjects.push($(this).children('td').eq(1).html());
if($(this).children('td').eq(2).children('a')!=undefined){
lect_and_tut.push($(this).children('td').eq(2).children('a').html());
}
else{
lect_and_tut.push('NA');
}
if($(this).children('td').eq(3).children('a')!=undefined){
lect.push($(this).children('td').eq(3).children('a').children('font').html());
}
else{
lect.push('NA');
}
if($(this).children('td').eq(4).children('a')!=undefined){
tut.push($(this).children('td').eq(4).children('a').html());
}
else{
tut.push('NA');
}
if($(this).children('td').eq(5).children('a')!=undefined){
prac.push($(this).children('td').eq(5).children('a').html());
}
else{
prac.push('NA');
}
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('attendanceSummary', {subjects:subjects, lect_and_tut:lect_and_tut, lect:lect, tut:tut, prac:prac});
}
//Remove the previous attendance data and save the new attendance record
attdb.remove({}, {multi:true});
attdb.insert({attendance:{subjects:subjects, lect_and_tut:lect_and_tut, lect:lect, tut:tut, prac:prac, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
}
});
}
function getPlan(e){
//Request for Seating plan
request({ secureProtocol: 'TLSv1_method', strictSSL: false, url:'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudViewSeatPlan.jsp', headers: headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Seating plan should not be cached
console.log(error);
}
else{
if (body.includes('Session Timeout')) {
//If session times out
createWindow();
//Login again with the already saved credentials in background
getPlan();
//again try to retrieve the data
}
else {
//checking for availability
var $ = cheerio.load(body);
var checkIfAvailable = $("#DScode>option");
if(checkIfAvailable){
//if available then start scrapping
var op = checkIfAvailable.val();
request({ secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudViewSeatPlan.jsp?x=&DScode='+op, headers: headers }, function(error, httpResponse, body){
if(error) throw body;
else{
var $ = cheerio.load(body);
var subjects = [];
var dateTime = [];
var room = [];
var seat = [];
$("#table-1").children('tbody').children('tr').each(function(i, item){
subjects.push($(this).children('td').eq(1).html());
dateTime.push($(this).children('td').eq(2).children('font').html());
room.push($(this).children('td').eq(3).html());
seat.push($(this).children('td').eq(4).html());
});
if(subjects.length!==0){
planAvailable = 1;
if (mainScreen) {
mainScreen.webContents.send('seating', { subjects: subjects, dateTime: dateTime, room: room, seat: seat });
}
}
else{
planAvailable = 0;
}
}
});
}
}
}
});
}
function getInfo(){
//Request for Personal Information
request({secureProtocol: 'TLSv1_method', strictSSL: false, url:'https://webkiosk.jiit.ac.in/StudentFiles/PersonalFiles/StudPersonalInfo.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
pdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].info){
if(mainScreen){
mainScreen.webContents.send('info', results[0].info);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getInfo();
//again try to retrieve the data
}
else{
//Scrapping the Personal data from the webpage
var $ = cheerio.load(body);
var tr = [];
$("table[cellpadding='2']>tbody").children('tr').each(function(i, item){
if(i !==0 && i!==7 && i!==9 && i<12){
tr.push($(this).children('td').eq(1).html());
}
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('info', {data:tr});
}
//Remove the previous data and save the new record
pdb.remove({}, {multi:true});
pdb.insert({info:{data:tr, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
}
});
}
function getFullInfo(){
//Request for full fees paid information
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/FAS/StudRegFee.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
ffdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].fullInfo){
if(mainScreen){
mainScreen.webContents.send('fullInfo', results[0].fullInfo);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getFullInfo();
//again try to retrieve the data
}
else{
//Scrapping
var $ = cheerio.load(body);
var sem = [];
var feesAmount = [];
var paid = [];
var dues = [];
$("#table-1>tbody").children('tr').each(function(i, item){
if(i<8){
sem.push($(this).children('td').eq(0).html());
feesAmount.push($(this).children('td').eq(2).html());
paid.push($(this).children('td').eq(4).html());
dues.push($(this).children('td').eq(5).html());
}
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('fullInfo', {sem:sem, feesAmount:feesAmount, paid:paid, dues:dues});
}
//Remove the previous data and save the new record
ffdb.remove({}, {multi:true});
ffdb.insert({fullInfo:{sem:sem, feesAmount:feesAmount, paid:paid, dues:dues, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
}
});
}
function getOnlineInfo(){
//Request to get the online fees paid history
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/pgfiles/OnlinePaymentHistory.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
odb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].onlineInfo){
if(mainScreen){
mainScreen.webContents.send('onlineInfo', results[0].onlineInfo);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getOnlineInfo();
//again try to retrieve the data
}
else{
//Scrapping
var $ = cheerio.load(body);
var sem = [];
var feesAmount = [];
var paid = [];
var trxn = [];
var status = [];
$("table>tbody").children('tr').each(function(i, item){
if(i>=2){
sem.push($(this).children('td').eq(1).html());
feesAmount.push($(this).children('td').eq(3).html());
paid.push($(this).children('td').eq(4).html());
trxn.push($(this).children('td').eq(5).html());
status.push($(this).children('td').eq(7).html());
}
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('onlineInfo', {sem:sem, feesAmount:feesAmount, paid:paid, trxn:trxn, status:status});
}
//Remove the previous data and save the new record
odb.remove({}, {multi:true});
odb.insert({onlineInfo:{sem:sem, feesAmount:feesAmount, paid:paid, trxn:trxn, status:status, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
}
});
}
function getPA(){
//Request to get the CGPA and SGPA record of every semester
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudCGPAReport.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
padb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].pa){
if(mainScreen){
mainScreen.webContents.send('pa', results[0].pa);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getPA();
//again try to retrieve the data
}
else{
var $ = cheerio.load(body);
var sem = [];
var credit = [];
var sg = [];
var cg = [];
$("#table-1>tbody").children('tr').each(function(i, item){
sem.push($(this).children('td').eq(0).html());
credit.push($(this).children('td').eq(2).html());
sg.push($(this).children('td').eq(6).html());
cg.push($(this).children('td').eq(7).html());
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('pa', {sem:sem, credit:credit, sg:sg, cg:cg});
}
//Remove the previous data and save the new record
padb.remove({}, {multi:true});
padb.insert({pa:{sem:sem, credit:credit, sg:sg, cg:cg, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
}
});
}
function getMarks(e){
//Get the marks of the current semester. If unavailable, will render the record of the last semester
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudentEventMarksView.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
mdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].marks){
if(mainScreen){
mainScreen.webContents.send('marks', results[0].marks);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getMarks(e);
//again try to retrieve the data
}
else{
//Get the latest records available on webkiosk
var $ = cheerio.load(body);
let val = $("select[name='exam']").children('option').eq(1).attr('value');
if(e){
//If user called marks for particular semester
val= e;
}
else{
//If no action from user
let option = [];
$("select[name='exam']").children('option').each(function(i, item){
var t = $(this).html();
if(t[0] == '2'){
option.push(t);
}
});
if(mainScreen){
mainScreen.webContents.send('switch', {option:option, type:'marks'});
}
}
//Request for marks of particular semester in the 'val' variable.
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudentEventMarksView.jsp?x=&exam='+val, headers:headers}, function(error, httpResponse, body){
if(error) throw error;
else{
//Scrapping
$ = cheerio.load(body);
var thead = $("#table-1>thead").children('tr').html();
var tr = [];
$("#table-1>tbody").children('tr').each(function(i, item){
tr.push($(this).html());
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('marks', {thead:thead, tr:tr});
}
//Remove previous data and save the new data.
mdb.remove({}, {multi:true});
mdb.insert({marks:{thead:thead, tr:tr, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
});
}
}
});
}
function getGrades(e){
//Get the grades of the current semester. If unavailable, will render the record of the last semester
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudentEventGradesView.jsp', headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
gdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].grade){
if(mainScreen){
mainScreen.webContents.send('grade', results[0].grade);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getGrades(e);
//again try to retrieve the data
}
else{
//Get the latest records available on webkiosk
var $ = cheerio.load(body);
let val = $("select[name='exam']").children('option').eq(1).attr('value');
if(e){
//If user called grades for particular semester
val = e;
}
else{
//If no action from user
let option = [];
$("select[name='exam']").children('option').each(function(i, item){
var t = $(this).html();
if(t[0] == '2'){
option.push(t);
}
});
if(mainScreen){
mainScreen.webContents.send('switch', {option:option, type:'grades'});
}
}
//Request for grades of particular semester in the 'val' variable.
request({secureProtocol: 'TLSv1_method', strictSSL: false, url: 'https://webkiosk.jiit.ac.in/StudentFiles/Exam/StudentEventGradesView.jsp?x=&exam='+val, headers:headers}, function(error, httpResponse, body){
if(error) throw error;
else{
//Scrapping
$ = cheerio.load(body);
var course = [];
var grade = [];
$("#table-1>tbody").children('tr').each(function(i, item){
course.push($(this).children('td').eq(1).html());
grade.push($(this).children('td').eq(3).html());
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('grade', {course:course, grade:grade});
}
//Remove previous data and save the new data
gdb.remove({}, {multi:true});
gdb.insert({grade:{course:course, grade:grade, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
});
}
}
});
}
function getSubjects(e){
//Get the subjects of the current semester. If unavailable, will render the record of the last semester
request({secureProtocol: 'TLSv1_method', strictSSL: false, url:"https://webkiosk.jiit.ac.in/StudentFiles/Academic/StudSubjectFaculty.jsp", headers:headers}, function(error, httpResponse, body){
if(error){
//Network connection failure
//Load the previously saved data from the system
sdb.find({}, function(error, results){
if(error) throw error;
else{
if(results[0].faculty){
if(mainScreen){
mainScreen.webContents.send('faculty', results[0].faculty);
}
}
}
});
//ECONNREFUSED
//getaddrinfo
console.log(error);
}
else{
if(body.includes('Session Timeout')){
//If session times out
createWindow();
//Login again with the already saved credentials in background
getSubjects(e);
//again try to retrieve the data
}
else{
//Get the latest records available on webkiosk
var $ = cheerio.load(body);
let option = [];
$("select[name='exam']").children('option').each(function(i, item){
var t = $(this).html();
if(t[0] == '2'){
option.push(t);
}
});
if(e){
//If user called subjects for particular semester
option[option.length-1] = e;
}
else{
if(mainScreen){
mainScreen.webContents.send('switch', {option:option, type:'faculty'});
}
}
//Request for subjects of particular semester in the 'option'.
request({secureProtocol: 'TLSv1_method', strictSSL: false, url:"https://webkiosk.jiit.ac.in/StudentFiles/Academic/StudSubjectFaculty.jsp?x=&exam="+option[option.length-1], headers:headers}, function(error, httpResponse, body){
if(error) throw error;
else{
//Scrapping
$ = cheerio.load(body);
var subject = [];
var lecture = [];
var tutorial = [];
var practical = [];
$("table[align='middle']>tbody").children('tr').each(function(i, item){
if(i!=0){
subject.push($(this).children('td').eq(1).html());
lecture.push($(this).children('td').eq(2).html());
tutorial.push($(this).children('td').eq(3).html());
practical.push($(this).children('td').eq(4).html());
}
});
if(mainScreen){
//Send the results to the render
mainScreen.webContents.send('faculty', {subject:subject, lecture:lecture, tutorial:tutorial, practical:practical});
}
//Remove previous data and save the new data
sdb.remove({}, {multi:true});
sdb.insert({faculty:{subject:subject, lecture:lecture, tutorial:tutorial, practical:practical, date:new Date()}}, function(error, results){
if(error) throw error;
});
}
});
}
}
});
}
/*APIS end*/
function checkLoginStatus(item){
if(loginStatus === 1){
//If the credentails are correct, then save the login data into the webkiosk.
logindb.insert({enroll: item.enroll, dob:item.dob, password:item.password, college: item.college}, function(error, results){
if(error){
clear();
throw new Error('There seems to be a problem in reading your login data. Please login again.');
}
else{
if(!mainScreen){
createMainScreen();
}
if(loginScreen){
loginScreen.close();
}
//Login creds saved
//Load main screen
}
});
//Login success
}
}
function login(data){
//Get the cookie from the webkiosk and save the cookie to authenticate future requests.
request({secureProtocol: 'TLSv1_method', strictSSL: false, url:'https://webkiosk.jiit.ac.in', headers: headers}, function(error, response, body){
if(error){
//Network connection failure
//Virtual login with the previous data.
if(data.saved){
loginStatus = 1;
fallback = true;
checkLoginStatus(data);
}
else{
if(error.code === 'ENOTFOUND'){
dialog.showErrorBox('Failed to login', 'Webkiosk is inaccessible. Probably, you are not connected to internet. Failed to login.');
loginScreen.webContents.send('failure', '');
}
else{
dialog.showErrorBox('Failed to login', 'Webkiosk is Down! Can not login. Please try again later.');
loginScreen.webContents.send('failure', '');
}
}
}
else{
var cookie = response.headers['set-cookie'];
var $ = cheerio.load(body);
var captcha = $('font[face="casteller"]').html();
headers.Cookie = cookie;
//Cookies overwritten
//Submit the login form with the data from the user.
request.post({secureProtocol: 'TLSv1_method', strictSSL: false, url:'https://webkiosk.jiit.ac.in/CommonFiles/UseValid.jsp', form: {txtInst:"Institute", InstCode:data.college, txtuType:"Member Type", UserType101117:"S", txtCode:"Enrollment No", MemberCode:data.enroll, DOB:"DOB", DATE1:data.dob, txtPin:"Password/Pin", Password101117:data.password, BTNSubmit:"Submit", txtCode:"Enter Captcha ", txtcap:captcha}, headers: headers}, function(error,httpResponse,body){
if(error){
console.log(error);
}
else{
//Invalid password case here
if(body.includes('Invalid Password')){
loginStatus = httpResponse.rawHeaders[5].split('=')[1];
dialog.showErrorBox('Authentication Error', 'Webkiosk reports that these credentials are invalid. Please make sure not to try the 3rd time before making sure!');
loginScreen.webContents.send('failure', 'NA');
return;
}
if(httpResponse.rawHeaders[5].split('=')[1]){
loginStatus = httpResponse.rawHeaders[5].split('=')[1];
if(httpResponse.rawHeaders[5].split('=')[1].includes('Date')){
dialog.showErrorBox('Authentication Error', 'Webkiosk reports that these credentials are invalid. Please make sure not to try the 3rd time before making sure!');