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 --- package.json | 3 +-- 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 ++++--- yarn.lock | 19 ++++++++++++++----- 7 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 src/server/lib/schools/hetmml/axios.js diff --git a/package.json b/package.json index bf5e80d..d9d8a1c 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ } }, "dependencies": { + "axios": "^0.17.1", "babel-loader": "^7.1.2", "babel-preset-react": "^6.24.1", "babel-preset-stage-2": "^6.24.1", - "bluebird": "^3.4.6", "body-parser": "^1.16.0", "browser-request": "^0.3.3", "cheerio": "^0.22.0", @@ -55,7 +55,6 @@ "redux": "^3.7.2", "redux-logger": "^3.0.6", "redux-thunk": "^2.2.0", - "request": "^2.74.0", "rmwc": "^1.1.2", "sass-loader": "^6.0.6", "smoothscroll-polyfill": "^0.3.4", 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; diff --git a/yarn.lock b/yarn.lock index a482674..d486dfb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -660,6 +660,13 @@ aws4@^1.2.1, aws4@^1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e" +axios@^0.17.1: + version "0.17.1" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.17.1.tgz#2d8e3e5d0bdbd7327f91bc814f5c57660f81824d" + dependencies: + follow-redirects "^1.2.5" + is-buffer "^1.1.5" + axobject-query@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-0.1.0.tgz#62f59dbc59c9f9242759ca349960e7a2fe3c36c0" @@ -1372,10 +1379,6 @@ block-stream@*: dependencies: inherits "~2.0.0" -bluebird@^3.4.6: - version "3.5.0" - resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c" - bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0: version "4.11.8" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f" @@ -3127,6 +3130,12 @@ focus-trap@^2.3.0: dependencies: tabbable "^1.0.3" +follow-redirects@^1.2.5: + version "1.4.1" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.4.1.tgz#d8120f4518190f55aac65bb6fc7b85fcd666d6aa" + dependencies: + debug "^3.1.0" + for-in@^0.1.3: version "0.1.8" resolved "https://registry.yarnpkg.com/for-in/-/for-in-0.1.8.tgz#d8773908e31256109952b1fdb9b3fa867d2775e1" @@ -6457,7 +6466,7 @@ request@2, request@^2.79.0: tunnel-agent "^0.6.0" uuid "^3.1.0" -request@2.81.0, request@^2.74.0, request@^2.81.0, request@~2.81.0: +request@2.81.0, request@^2.81.0, request@~2.81.0: version "2.81.0" resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0" dependencies: -- cgit v1.1