UNPKG

dcupachatbot

Version:

DCU Personal Assistant chat bot for CA400 project

111 lines (96 loc) 4.7 kB
'use strict'; var request = require('request'); var cheerio = require('cheerio'); var HashMap = require('hashmap'); var courseID = 'case'; var year = 4; var requestUrl = 'https://www101.dcu.ie/timetables/feed.php?prog=case&per=4&week1=1&week2=19&hour=1-20&template=student'; var options = { url: requestUrl, 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 = 'Mon'; //daysOfWeek[date.getDay()]; var hourNow = date.getHours(); console.log(today); console.log(hourNow); 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 ; //console.log(reply); var classTime = parseInt(startTime,10); if(hourNow < classTime) { classTime = classTime - hourNow; map.set(classTime, reply); } //console.log('--------------------------------------------------------------------'); } }); var min = 1000; map.forEach(function(value, key) { if(key < min) min = key; }); map.forEach(function(value, key) { if(key == min) console.log (value); }); } else { // There was an error console.log(error); } });