-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (164 loc) · 5.21 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
var request = require('request'),
cheerio = require('cheerio'),
config = require('./config.json'),
log4js = require('log4js'),
jar = request.jar();
var userid = 31120110480,
term = 1130,
loginURL = 'https://erp.bits-pilani.ac.in:4431/psp/hcsprod/?cmd=login&languageCd=ENG',
gradeBookURL = 'https://erp.bits-pilani.ac.in:4431/psc/hcsprod/EMPLOYEE/HRMS/c/SA_LEARNER_SERVICES.SSR_SSENRL_GRADE.GBL?ACAD_CAREER=0001&EMPLID='+ userid + '&INSTITUTION=BITS&STRM=' + term,
facebookURL = 'https://graph.facebook.com/',
postedToGroup = false,
// Edit the message as required.
messageString = 'ARC has uploaded the grades of Semester 1, 2014-15 onto \
the ERP server. Click below to check your grades.\n\n\
Sent via ERP Result Notifier',
mailSent = false,
// User Agent string.
ua = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
ssl = 'SSLv3_method',
$ = '',
// Interval handle for the login function until it is successful.
// Possible in the event of a network/server failure.
loginInterval = '',
handle = '',
email = require('emailjs'),
mailServer = email.server.connect({
user: config.gmail_username,
password: config.gmail_password,
host: config.smtpServer,
ssl: true
}),
mailMessage = {
from: "ERP Result Notifier<" + config.gmail_username + ">",
subject: "Semester 1, 2014-15 Grades",
to: config.destination_email
};
// Configure logger
log4js.configure({
appenders: [
{
type: 'file',
filename: "./log",
category: 'info',
maxLogSize: 2048000,
backups: 10
}
]
});
var logger = log4js.getLogger('info');
// Send a pre-defined email to destination_email when the results are out.
var sendMail = function() {
// Prevent duplicate feedback.
if(mailSent){
return;
}
// Edit the email text as required.
mailMessage.text = "Grades Updated.";
mailServer.send(mailMessage, function(err, message) {
if (err) {
logger.error(err);
logger.error("Error sending mail");
return;
}
mailSent = true;
logger.info("Mail sent successfully");
});
};
var postToFacebook = function() {
// Prevent duplicate feedback.
if(postedToGroup){
return;
}
facebookURL += config.group_id + '/feed?access_token=' + config.access_token
+ '&message=' + messageString + '&link=' + loginURL;
request.post({
url: facebookURL,
}, function(e, r, data) {
if (e) {
logger.error("Request Failed", e);
return;
}
data = JSON.parse(data);
if (data.hasOwnProperty('id')) {
logger.info("Successfully posted to facebook group");
postedToGroup = true;
} else {
logger.error("Error posting to group");
}
});
};
var login = function() {
logger.info("Logging in...");
request.post({
url: loginURL,
jar: jar,
form: {
userid: config.username,
pwd: config.password
},
headers: {
'User-Agent': ua
},
secureProtocol: ssl
}, function(e, r, body) {
if (e) {
logger.error("Login Request Failed", e);
// Retry after every 5 seconds until login succeeds.
if(!loginInterval){
logger.info("Setting login interval");
loginInterval = setInterval(login, 5000);
}
return;
}
// Clear login interval.
clearInterval(loginInterval);
if(body.indexOf('errorCode') >= 0){
logger.info("Invalid User ID/Password combination");
return;
}
logger.info("Login Successful");
handle = setInterval(checkGrade, config.interval);
});
};
var checkGrade = function() {
logger.info("Checking for grades...");
request.post({
url: gradeBookURL,
jar: jar,
headers: {
'User-Agent': ua
},
secureProtocol: ssl
}, function(e, r, body) {
if (e) {
logger.error("gradeCheck Request Failed", e);
return;
}
if(body.indexOf('Sign-in') >= 0){
// Possible in the event of a network failure.
logger.error("Session Cookies have expired, logging in...");
// Clear checkGrade interval
logger.info("Clearing existing checkGrade interval");
clearInterval(handle);
// Session cookies have expired, re-login.
login();
return;
}
$ = cheerio.load(body);
// GPA Element
var gpa = $("#STATS_ENRL\\$12").text().trim();
if (gpa.length) {
if (parseInt(gpa) > 0) {
postToFacebook();
sendMail();
logger.info(gpa);
logger.info("Result Declared");
clearInterval(handle);
}
} else {
logger.info("Not available yet");
}
});
};
login();