dcupachatbot
Version:
DCU Personal Assistant chat bot for CA400 project
112 lines (96 loc) • 4.78 kB
JavaScript
;
var request = require('request');
var cheerio = require('cheerio');
var HashMap = require('hashmap');
var getTimetableNext = function getTimetableNext(courseID, year, semester, callback) {
if(semester == 1)
var urlOption = 'https://www101.dcu.ie/timetables/feed.php?prog=' + courseID + '&per=' + year + '&week1=1&week2=19&hour=1-20&template=student';
else
var urlOption = 'https://www101.dcu.ie/timetables/feed.php?prog=' + courseID + '&per=' + year + '&week1=20&week2=31&hour=1-20&template=student';
var options = {
url: urlOption,
strictSSL: false
}
request(options, function (error, response, html) {
if (!error && response.statusCode == 200) {
var date = new Date();
var daysOfWeek = ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var today = 'Thu'; //daysOfWeek[date.getDay()];
var hourNow = date.getHours();
var map = new HashMap();
var $ = cheerio.load(html);
$("td[align='center']").each(function(i, element)
{
var day = $(this).parent().parent().parent().parent().parent().children().eq(0).text();
var timeSlotConflict = false;
if(!day)
{
day = $(this).parent().parent().parent().parent().parent().prev().children().eq(0).text();
timeSlotConflict = true;
}
if(day == today)
{
var module = $(this).text();
module = module.replace(/\s+/g, " ");
var lecturer = $(this).prev().text();
lecturer = lecturer.replace(/\s+/g, " ");
var location = $(this).parent().parent().parent().prev().find('tbody').children("[align='left']").eq(0).text();
location = location.replace(/\s+/g, " ");
var code = $(this).parent().parent().parent().next().find('tbody').find('tr').eq(0).text();
code = code.replace(/\s+/g, " ");
var weeks = $(this).parent().parent().parent().next().find('tbody').find('tr').eq(1).text();
weeks = weeks.replace(/\s+/g, " ");
var count = 0;
var timeSlot = $(this).parent().parent().parent().parent();
var timeSlotCheck = $(this).parent().children().eq(0).text();
var check = true;
while(check)
{
var columnLength = $(timeSlot).attr('colspan');
if(!columnLength)
{
count++;
}
else
{
columnLength = parseInt(columnLength,10);
count += columnLength;
if(columnLength == 4 && count == 4 && !timeSlotConflict)
count -= 2;
}
timeSlot = $(timeSlot).prev();
var checker1 = $(timeSlot).attr('rowspan');
var checker2 = $(timeSlot).attr('style');
if(checker1 && checker2 && checker2 == 'border-bottom:3px solid #000000;')
check = false;
if(timeSlotConflict == true && checker2 != 'undefined')
check = false;
}
count -= 1;
var startTime = $(this).parent().parent().parent().parent().parent().parent().children().eq(0).children().eq(count).text();
startTime = startTime.replace(/\s+/g, " ");
var reply = ' ' + module + '\n' + ' ' + startTime + '\n' + ' ' + lecturer + '\n' + location + '\n' + code ;
var classTime = parseInt(startTime,10);
if(hourNow < classTime)
{
classTime = classTime - hourNow;
map.set(classTime, reply);
}
}
});
var min = 1000;
map.forEach(function(value, key) {
if(key < min)
min = key;
});
map.forEach(function(value, key) {
if(key == min)
callback(error, value);
});
} else {
// There was an error
callback(error);
}
});
};
module.exports = getTimetableNext;