From f4ed51066f16df24db44c11aa0b2356299d07010 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sat, 17 Feb 2018 18:33:55 +0100 Subject: Migrate from request to axios --- src/server/lib/schools/hetmml/axios.js | 11 +++++++++++ src/server/lib/schools/hetmml/getScheduleData.js | 20 ++++++++------------ src/server/lib/schools/hetmml/getURLOfUser.js | 3 +-- src/server/routes/getSchedule.js | 18 +++++++----------- src/server/routes/index.js | 7 ++++--- 5 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 src/server/lib/schools/hetmml/axios.js (limited to 'src/server') diff --git a/src/server/lib/schools/hetmml/axios.js b/src/server/lib/schools/hetmml/axios.js new file mode 100644 index 0000000..3993890 --- /dev/null +++ b/src/server/lib/schools/hetmml/axios.js @@ -0,0 +1,11 @@ +const axios = require('axios'); +const iconv = require('iconv-lite'); + +const instance = axios.create({ + baseURL: 'http://www.meetingpointmco.nl/Roosters-AL/doc/', + timeout: 5000, + responseType: 'arraybuffer', + transformResponse: [responseBody => iconv.decode(responseBody, 'iso-8859-1')], +}); + +module.exports = instance; diff --git a/src/server/lib/schools/hetmml/getScheduleData.js b/src/server/lib/schools/hetmml/getScheduleData.js index 303af6d..6789479 100644 --- a/src/server/lib/schools/hetmml/getScheduleData.js +++ b/src/server/lib/schools/hetmml/getScheduleData.js @@ -1,11 +1,9 @@ -const Promise = require('bluebird'); const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); const debounce = require('promise-debounce'); const _ = require('lodash'); -const request = Promise.promisify(require('request')); const getUrlOfUser = require('./getURLOfUser'); +const axios = require('./axios'); let meetingpointData; let lastUpdate; @@ -66,13 +64,11 @@ function combineUsers(usersArrays) { function getAlts(users) { const requests = users.map(user => - request(getUrlOfUser('dag', user.type, user.index, 7), { timeout: 8000, encoding: null })); + axios.get(getUrlOfUser('dag', user.type, user.index, 7))); return Promise.all(requests).then(teacherResponses => teacherResponses.map((teacherResponse, index) => { - const teacherResponseBody = iconv.decode(teacherResponse.body, 'iso-8859-1'); - - const teacherName = scrapeAltText(teacherResponseBody); + const teacherName = scrapeAltText(teacherResponse.data); return { ...users[index], @@ -83,15 +79,15 @@ function getAlts(users) { function getScheduleData() { 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 }), + axios.get('/dagroosters/frames/navbar.htm'), + axios.get('/basisroosters/frames/navbar.htm'), ]; return Promise.all(navbarRequests) .then(([dailyScheduleResponse, basisScheduleResponse]) => { - const users = scrapeUsers(dailyScheduleResponse.body); - const dailyScheduleWeeks = scrapeWeeks(dailyScheduleResponse.body); - const basisScheduleWeeks = scrapeWeeks(basisScheduleResponse.body); + const users = scrapeUsers(dailyScheduleResponse.data); + const dailyScheduleWeeks = scrapeWeeks(dailyScheduleResponse.data); + const basisScheduleWeeks = scrapeWeeks(basisScheduleResponse.data); const teachers = users.filter(user => user.type === 't'); diff --git a/src/server/lib/schools/hetmml/getURLOfUser.js b/src/server/lib/schools/hetmml/getURLOfUser.js index f44d1a3..2e1e1e9 100644 --- a/src/server/lib/schools/hetmml/getURLOfUser.js +++ b/src/server/lib/schools/hetmml/getURLOfUser.js @@ -1,8 +1,7 @@ const leftPad = require('left-pad'); // I imported this just to piss you off ;) function getURLOfUser(scheduleType, type, index, week) { - return `http://www.meetingpointmco.nl/Roosters-AL/doc/${scheduleType}roosters/` + - `${leftPad(week, 2, '0')}/${type}/${type}${leftPad(index + 1, 5, '0')}.htm`; + return `/${scheduleType}roosters/${leftPad(week, 2, '0')}/${type}/${type}${leftPad(index + 1, 5, '0')}.htm`; } module.exports = getURLOfUser; diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js index 1734cd8..b98e76a 100644 --- a/src/server/routes/getSchedule.js +++ b/src/server/routes/getSchedule.js @@ -1,11 +1,10 @@ const express = require('express'); const router = express.Router(); -const request = require('request'); -const iconv = require('iconv-lite'); const getScheduleData = require('../lib/schools/hetmml/getScheduleData'); const getURLOfUser = require('../lib/schools/hetmml/getURLOfUser'); +const axios = require('../lib/schools/hetmml/axios'); // copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js, // were using the same code as they do to be sure that we always get the same @@ -45,16 +44,13 @@ router.get('/:type/:value', (req, res, next) => { const url = getURLOfUser(scheduleType, type, index, week); - request(url, { encoding: null }, (err, data) => { - if (err) { + axios.get(url) + .then((response) => { + res.status(response.status).end(response.data); + }) + .catch((err) => { next(err); - return; - } - - const utf8Body = iconv.decode(data.body, 'ISO-8859-1'); - - res.status(data.statusCode).end(utf8Body); - }); + }); }); }); diff --git a/src/server/routes/index.js b/src/server/routes/index.js index af82e1f..78f31f3 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -27,10 +27,11 @@ router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], (req, res) => { dailyScheduleWeeksStr, basisScheduleWeeksStr, }); - }).catch(() => { - console.error('Unable to get user info, emergency redirect!'); - res.render('redirect'); }); + // .catch(() => { + // console.error('Unable to get user info, emergency redirect!'); + // res.render('redirect'); + // }); }); module.exports = router; -- cgit v1.1