diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-02-16 21:26:32 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-02-16 21:26:32 +0100 |
commit | ac0081a4bbb9426856443ca64d345fd964df6d2a (patch) | |
tree | ac121602c15ee0bc56661f8ff1838d1ea8657a19 /src | |
parent | 9d9e717261eb3ba37129a37676bc2bbfa2a39573 (diff) |
Add full name to teacher object
Diffstat (limited to 'src')
-rw-r--r-- | src/server/lib/getMeetingpointData.js | 32 |
1 files changed, 28 insertions, 4 deletions
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() { |