From ac0081a4bbb9426856443ca64d345fd964df6d2a Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 16 Feb 2018 21:26:32 +0100 Subject: Add full name to teacher object --- src/server/lib/getMeetingpointData.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/server/lib') diff --git a/src/server/lib/getMeetingpointData.js b/src/server/lib/getMeetingpointData.js index 0272398..c70b3fa 100644 --- a/src/server/lib/getMeetingpointData.js +++ b/src/server/lib/getMeetingpointData.js @@ -1,8 +1,11 @@ const Promise = require('bluebird'); const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); const _ = require('lodash'); const request = Promise.promisify(require('request')); +const getUrlOfUser = require('./getURLOfUser'); + let meetingpointData; let lastUpdate; @@ -52,24 +55,45 @@ function getWeeks(page) { function requestData() { lastUpdate = new Date(); - const requests = [ + const navbarRequests = [ request('http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm', { timeout: 5000 }), request('http://www.meetingpointmco.nl/Roosters-AL/doc/basisroosters/frames/navbar.htm', { timeout: 5000 }), ]; - return Promise.all(requests).then(([dailyScheduleResponse, basisScheduleResponse]) => { + return Promise.all(navbarRequests) + .then(([dailyScheduleResponse, basisScheduleResponse]) => { const dailySchedulePage = cheerio.load(dailyScheduleResponse.body); const basisSchedulePage = cheerio.load(basisScheduleResponse.body); const users = getUsers(dailySchedulePage); const dailyScheduleWeeks = getWeeks(dailySchedulePage); const basisScheduleWeeks = getWeeks(basisSchedulePage); - meetingpointData = { users, dailyScheduleWeeks, basisScheduleWeeks }; + const teachers = users.filter(user => user.type === 't'); + + const teacherRequests = teachers.map(teacher => + request(getUrlOfUser('dag', teacher.type, teacher.index, 7), { timeout: 5000, encoding: null })); + + return Promise.all(teacherRequests).then((teacherResponses) => { + const teachersWithAlts = teacherResponses.map((teacherResponse, index) => { + const utf8Body = iconv.decode(teacherResponse.body, 'iso-8859-1'); + const teacherResponseBody = cheerio.load(utf8Body); + const teacherName = teacherResponseBody('center > font').eq(2).text().trim(); + + return { + ...teachers[index], + alt: teacherName, + }; + }); - console.log(meetingpointData); + meetingpointData = { + users: _.defaults(teachersWithAlts, users), + dailyScheduleWeeks, + basisScheduleWeeks, + }; return meetingpointData; }); + }); } function getMeetingpointData() { -- cgit v1.1