-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
372 lines (299 loc) · 11 KB
/
app.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
// Include dependencies:
var express = require('express');
var fs = require('fs');
var https = require('https');
var querystring = require('querystring');
// Jquery stuff
var cheerio = require('cheerio');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
// Create directories:
if(!fs.existsSync('static')) {
fs.mkdirSync('static');
}
if(!fs.existsSync('static/timetable')) {
fs.mkdirSync('static/timetable');
}
function GetTimetable(code, yr, callback, errorCallback) {
// Remove white spaces from code, make upper case:
code = code.trim().toUpperCase();
// We should probably check if said timetable exists first!
if(fs.existsSync('static/timetable/'+yr+'/'+code+'.json')) {
// Read the file:
fs.readFile('static/timetable/'+yr+'/'+code+'.json', function (err, data) {
if (err) throw err;
// Call the callback:
callback(data);
});
return true;
}
var html_data = '';
var req;
if(yr < 2015) {
var post_data = querystring.stringify({
scodes: code,
year: parseInt(yr),
sortby: 'act'
});
var options = {
hostname: 'sis.unimelb.edu.au',
path: '/cgi-bin/subjects.pl',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
req = https.request(options, function(res) {
res.on('data', function(d) {
html_data+=d;
});
res.on('end', function() {
// for error callback:
var foundData = false;
// Process the data:
var count = html_data.split('<h2>Year:').length - 1;
// Did we fail:
if(count <= 0) {
// No results found:
if(errorCallback) {
errorCallback('Failed to find subject!');
return false;
}
}
var result = new Array();
// Process results:
var pos = 0;
for(var i=0;i<count;i++) {
pos = html_data.indexOf('<h2>Year:', pos);
var end = html_data.indexOf('</table>', pos);
var d = html_data.substr(pos, end-pos);
// Grab the subject name:
var a = d.indexOf(code)+code.length+1;
var name = d.substr(a, d.indexOf('</b>')-a);
a = name.indexOf(')');
// Remove bracket:
if(a != -1) {
name = name.substr(a+2);
}
// Workout which year the data is from:
var a = d.indexOf(' ')+8;
var year = d.substr(10, d.indexOf(' ')-10);
var semester = d.substr(a, d.indexOf('</h2>')-a);
// Chop down the data:
d = d.substr(d.indexOf('<tr>'), d.length);
d = d.split('</tr>');
var data = new Array();
// Process data:
for(var j=0;j<d.length-1;j++) {
// Temp data array:
var dta = new Array()
// Split our string:
var dd = d[j].split('</td>');
// Process our string:
for(var k=2;k<dd.length-1;k++) {
dta.push(dd[k].substr(4));
}
// Fix class length:
dta[4] = parseFloat(dta[4]);
// Store dta:
data.push(dta);
}
// Push the data on:
result.push({
sem:semester,
data:data
});
// Move pos forward:
pos += 1;
}
// Ensure directories exist:
if(!fs.existsSync('static/timetable/'+year)) {
fs.mkdirSync('static/timetable/'+year);
}
var fin = JSON.stringify({name:name,code:code,year:year,data:result});
// Store the results:
fs.writeFile('static/timetable/'+year+'/'+code+'.json', fin, function(err) {
if(err) {
console.log(err);
}
});
callback(fin);
});
});
req.write(post_data);
} else {
// WHY YOU CHANGE FORMAT FOR 2015?!?
// Grab the year
var theYear = 2017;
try {
theYear = parseInt(yr);
} catch(e) {
// Do nothing
}
var options = {
hostname: 'sws.unimelb.edu.au',
path: '/' + theYear + '/Reports/List.aspx?objects=' + code + '&weeks=1-52&days=1-7&periods=1-56&template=module_by_group_list',
method: 'GET'
};
req = https.request(options, function(res) {
res.on('data', function(d) {
html_data+=d;
});
res.on('end', function() {
console.log('End');
// Prepare cheerio
var $ = cheerio.load(html_data);
// Pull subject name
var subjectTitleData = $('div[data-role="collapsible"] h3').html();
if(subjectTitleData != null) {
// Grab the subject title
var subjectTitle = subjectTitleData.split(' - ')[1].split('\n')[0];
var semesterData = [];
$('table[class="cyon_table"]').each(function(tableIndex, table) {
console.log('yes');
$('tr', table).each(function(trIndex, tr) {
var classInfo = $('td:nth-child(1)', tr).text().split('/');
if(classInfo.length != 6) return;
//var description = $('td:nth-child(2)', tr).text();
var startInfo = $('td:nth-child(4)', tr).text().split(':');
var start = '';
if(parseInt(startInfo[0]) >= 12) {
if(startInfo[0] == '12') {
start = startInfo[0] + ':' + startInfo[1] + 'pm';
} else {
start = (parseInt(startInfo[0])-12) + ':' + startInfo[1] + 'pm';
}
} else {
start = startInfo[0] + ':' + startInfo[1] + 'am';
}
var finishInfo = $('td:nth-child(5)', tr).text().split(':');
var finish = '';
if(parseInt(finishInfo[0]) >= 12) {
if(finishInfo[0] == '12') {
finish = finishInfo[0] + ':' + finishInfo[1] + 'pm';
} else {
finish = (parseInt(finishInfo[0])-12) + ':' + finishInfo[1] + 'pm';
}
} else {
finish = finishInfo[0] + ':' + finishInfo[1] + 'am';
}
var durationInfo = $('td:nth-child(6)', tr).text();
//var code = classInfo[0];
var semester = classInfo[3];
// Things we store
var classType = classInfo[4] + '/' + classInfo[5];
var day = $('td:nth-child(3)', tr).text();
var runTimes = start + ' - ' + finish;
var location = $('td:nth-child(8)', tr).text();
var duration = parseInt(durationInfo[0]);
var ourData = [classType, day, runTimes, location, duration];
// Ensure a store for our semester exists
if(!semesterData[semester]) semesterData[semester] = [];
if (!(classInfo[4].indexOf('Breakout') >= 0)) {
// Don't include breakout rooms on timetable
// Store this class
semesterData[semester].push(ourData);
}
});
});
// Build result array
var result = [];
for(var semesterID in semesterData) {
result.push({
sem:semesterID,
data:semesterData[semesterID]
});
}
// Ensure directories exist:
if(!fs.existsSync('static/timetable/'+yr)) {
fs.mkdirSync('static/timetable/'+yr);
}
var fin = JSON.stringify({name:subjectTitle,code:code,year:yr,data:result});
// Store the results:
fs.writeFile('static/timetable/'+yr+'/'+code+'.json', fin, function(err) {
if(err) {
console.log(err);
}
});
callback(fin);
} else {
// No results found:
if(errorCallback) {
errorCallback('Failed to find subject!');
return false;
}
}
});
});
}
req.on('error', function(e) {
console.log(e);
if(errorCallback) {
errorCallback('request error');
return false;
}
});
req.end();
}
// Create the web server:
var app = express();
// Setup static dir
app.use(express.static(__dirname + '/static'));
// Index page:
app.get('/', function (req, res) {
res.sendfile('static/index.htm');
});
// Grabs a timetable for all the subjects in recordings.htm
function QueryRecordings() {
var z = String(fs.readFileSync('recordings.htm'));
z = z.split('\n');
for(var i=0;i<z.length;i++) {
var a = z[i].indexOf('">')+2;
var code = z[i].substr(a, z[i].indexOf(' - ')-a);
GetTimetable(code, 2013, function(){});
}
}
// Builds a json list of subjects for auto complete:
function BuildSubjectList() {
var lst = new Array();
var taken = {};
var z = String(fs.readFileSync('recordings.htm'));
z = z.split('\n');
for(var i=0;i<z.length;i++) {
var a = z[i].indexOf('">')+2;
var dash = z[i].indexOf(' - ')
var code = z[i].substr(a, dash-a);
var name = z[i].substr(dash+3, z[i].indexOf('</a>')-dash-3);
// Check if we already have this subject:
if(!taken[code]) {
lst.push(code+' '+name);
}
// Stop same subject appearing twice:
taken[code] = true;
}
var fin = JSON.stringify(lst);
// Store the results:
fs.writeFile('static/subjects.json', fin, function(err) {
if(err) {
console.log(err);
}
});
}
// Queries:
app.get('/timetable/:year/:code.json', function (req, res) {
// Send the timetable:
GetTimetable(req.params.code, req.params.year, function(data) {
res.writeHead(200, {"Content-Type": "application/json"});
res.write(data);
res.end();
}, function(e){
var err = new Array('error', e);
res.writeHead(200, {"Content-Type": "application/json"});
res.write(JSON.stringify(err));
res.end();
});
});
// Start listening:
app.listen(process.env.PORT || 3000, function() {
console.log('Listening on port '+(process.env.PORT || 3000)+', goto http://localhost:'+(process.env.PORT || 3000)+' in your web browser.');
});