2015-06-13 19:14:45 +02:00
|
|
|
//schedule.js
|
2015-06-24 17:07:52 +02:00
|
|
|
var http = require('socks5-http-client');
|
2015-06-13 19:14:45 +02:00
|
|
|
var cheerio = require('cheerio');
|
2015-06-14 23:32:57 +02:00
|
|
|
var config = require('./configuration');
|
2015-06-24 17:07:52 +02:00
|
|
|
var url = require('url');
|
2015-06-13 19:14:45 +02:00
|
|
|
|
2015-06-13 22:07:06 +02:00
|
|
|
//Wrapper function that is being called by express.
|
2015-06-13 22:36:18 +02:00
|
|
|
function schedule(req, res, next) {
|
|
|
|
get(req.match.url, function (json) {
|
|
|
|
req.match.json = json;
|
|
|
|
next();
|
2015-06-13 19:14:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:07:06 +02:00
|
|
|
//Function for getting the page via http.
|
2015-06-24 17:26:24 +02:00
|
|
|
function get(get_url, callback) {
|
|
|
|
|
|
|
|
var options = url.parse(get_url);
|
2015-06-24 17:07:52 +02:00
|
|
|
options.socksPort = config().tor_port;
|
2015-06-24 17:10:12 +02:00
|
|
|
options.socksHost = config().tor_host;
|
2015-06-24 17:07:52 +02:00
|
|
|
|
|
|
|
http.get(options, function (res) {
|
2015-06-13 19:14:45 +02:00
|
|
|
var _download = '';
|
|
|
|
|
|
|
|
res.on('data', function (data) {
|
|
|
|
_download += data;
|
2015-06-13 22:36:18 +02:00
|
|
|
});
|
2015-06-13 19:14:45 +02:00
|
|
|
|
|
|
|
res.on('end', function () {
|
|
|
|
callback(to_json(_download));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-21 00:21:25 +02:00
|
|
|
function schedule_types(page) {
|
|
|
|
var extract = cheerio('table tr td[valign="bottom"] table tr td b, table tr td[valign="bottom"] table tr td a', page).text().split(/\s\s/);
|
2015-06-21 17:06:45 +02:00
|
|
|
var tab = 0;
|
2015-06-21 00:21:25 +02:00
|
|
|
var types = [];
|
|
|
|
|
|
|
|
for (element of extract) {
|
2015-06-21 17:06:45 +02:00
|
|
|
element != '' ? types.push({
|
|
|
|
'letter': element.substr(0, 1),
|
2015-06-21 17:14:40 +02:00
|
|
|
'value' : element.match(/.*rooster|t\/m|\d\d\s\w{3}/g).join(' ').slice(1),
|
2015-06-21 17:06:45 +02:00
|
|
|
'tab': tab++
|
|
|
|
}) : null;
|
2015-06-21 00:21:25 +02:00
|
|
|
}
|
2015-06-21 17:06:45 +02:00
|
|
|
|
|
|
|
return types;
|
2015-06-21 00:21:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-13 22:07:06 +02:00
|
|
|
//Function for converting the page into a json dataset.
|
2015-06-13 19:14:45 +02:00
|
|
|
function to_json(page) {
|
|
|
|
var result = cheerio('td:nth-child(3) table', page);
|
2015-06-21 00:21:25 +02:00
|
|
|
var types = schedule_types(page);
|
2015-06-16 15:22:34 +02:00
|
|
|
var is_teacher = cheerio(cheerio(page).find('tr.CoreDark').find('td')[3]).find('a').html() == null;
|
2015-06-13 19:14:45 +02:00
|
|
|
var amount_of_days = cheerio(result).find('tr.AccentDark').find('td').length - 1;
|
2015-06-14 23:32:57 +02:00
|
|
|
var amount_of_hours = config().amount_of_hours;
|
2015-06-13 22:07:06 +02:00
|
|
|
|
2015-06-13 19:14:45 +02:00
|
|
|
var schedule_data = [];
|
|
|
|
|
2015-06-16 15:22:34 +02:00
|
|
|
var offset = is_teacher ? 5 : 6;
|
|
|
|
|
2015-06-13 19:14:45 +02:00
|
|
|
//Looping for amount of days
|
|
|
|
for (day = 0; day < amount_of_days; day++) {
|
2015-06-15 19:18:19 +02:00
|
|
|
schedule_data[day] = [];
|
2015-06-13 19:14:45 +02:00
|
|
|
|
|
|
|
//Looping for amount of hours
|
|
|
|
for (hour = 0; hour < amount_of_hours; hour++) {
|
2015-06-16 15:22:34 +02:00
|
|
|
var schedule = cheerio('tr:nth-child('+ (offset + hour) +')', result);
|
2015-06-13 19:14:45 +02:00
|
|
|
|
|
|
|
//Looping for (optional) specialhours
|
2015-06-14 18:45:34 +02:00
|
|
|
var amount_of_special_hours = schedule.find('table').eq(day).children().length;
|
2015-06-15 19:18:19 +02:00
|
|
|
schedule_data[day][hour] = {teacher: [], chamber: [], course: [], changed: []};
|
2015-06-19 21:05:35 +02:00
|
|
|
for (subhour = 0; subhour < amount_of_special_hours; subhour++) {
|
|
|
|
var selected_hour = schedule.find('table').eq(day).find('tr').eq(subhour).find('td');
|
2015-06-16 15:04:07 +02:00
|
|
|
//Give the value of the schedule hour to the fitting array.
|
2015-06-19 21:10:58 +02:00
|
|
|
schedule_data[day][hour].teacher[subhour] = selected_hour.eq(0).text().replace(/\r|\n/g, '');
|
2015-06-19 21:05:35 +02:00
|
|
|
schedule_data[day][hour].chamber[subhour] = selected_hour.eq(2).text();
|
|
|
|
schedule_data[day][hour].course[subhour] = selected_hour.eq(4).text();
|
2015-06-16 15:04:07 +02:00
|
|
|
//Check if the hour is 'changed' by the schedule authors, if so set to true.
|
2015-06-19 21:05:35 +02:00
|
|
|
schedule_data[day][hour].changed[subhour] = selected_hour.eq(0).attr().class == 'tableCellNew' ? true : false;
|
2015-06-13 19:14:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 17:06:45 +02:00
|
|
|
schedule_data.types = types;
|
|
|
|
|
2015-06-13 19:14:45 +02:00
|
|
|
return schedule_data;
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:07:06 +02:00
|
|
|
//Exporting the schedule function.
|
2015-06-13 19:14:45 +02:00
|
|
|
module.exports = schedule;
|