From 2e4b9dc1cc48e5d35243bcef32b8acd29171bf23 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sat, 17 Feb 2018 18:11:46 +0100 Subject: Move school spesific files to school spesifc folders --- src/server/lib/getMeetingpointData.js | 127 ----------------------- src/server/lib/getURLOfUser.js | 8 -- src/server/lib/schools/hetmml/getScheduleData.js | 127 +++++++++++++++++++++++ src/server/lib/schools/hetmml/getURLOfUser.js | 8 ++ src/server/routes/getSchedule.js | 6 +- src/server/routes/index.js | 4 +- 6 files changed, 140 insertions(+), 140 deletions(-) delete mode 100644 src/server/lib/getMeetingpointData.js delete mode 100644 src/server/lib/getURLOfUser.js create mode 100644 src/server/lib/schools/hetmml/getScheduleData.js create mode 100644 src/server/lib/schools/hetmml/getURLOfUser.js diff --git a/src/server/lib/getMeetingpointData.js b/src/server/lib/getMeetingpointData.js deleted file mode 100644 index c1de28a..0000000 --- a/src/server/lib/getMeetingpointData.js +++ /dev/null @@ -1,127 +0,0 @@ -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'); - -let meetingpointData; -let lastUpdate; - -function scrapeUsers(html) { - const page = cheerio.load(html); - const script = page('script').eq(1).text(); - - const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/]; - const items = regexs.map(regex => script.match(regex)[1].split(',').map(item => item.replace(/"/g, ''))); - - const classes = items[0].map((item, index) => ({ - type: 'c', - value: item, - index, - })); - - const teachers = items[1].map((item, index) => ({ - type: 't', - value: item, - index, - })); - - const rooms = items[2].map((item, index) => ({ - type: 'r', - value: item, - index, - })); - - const students = items[3].map((item, index) => ({ - type: 's', - value: item, - index, - })); - - return _.flatten([classes, teachers, rooms, students]); -} - -function scrapeWeeks(html) { - const page = cheerio.load(html); - const weekSelector = page('select[name="week"]'); - const weeks = _.map(weekSelector.children(), option => ({ - id: cheerio(option).attr('value'), - text: cheerio(option).text(), - })); - - return weeks; -} - -function scrapeAltText(html) { - const page = cheerio.load(html); - return page('center > font').eq(2).text().trim(); -} - -function combineUsers(usersArrays) { - return _.uniqBy(_.flatten(usersArrays), user => `${user.type}/${user.value}`); -} - -function getAlts(users) { - const requests = users.map(user => - request(getUrlOfUser('dag', user.type, user.index, 7), { timeout: 8000, encoding: null })); - - return Promise.all(requests).then(teacherResponses => - teacherResponses.map((teacherResponse, index) => { - const teacherResponseBody = iconv.decode(teacherResponse.body, 'iso-8859-1'); - - const teacherName = scrapeAltText(teacherResponseBody); - - return { - ...users[index], - alt: teacherName, - }; - })); -} - -function getMeetingpointData() { - 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(navbarRequests) - .then(([dailyScheduleResponse, basisScheduleResponse]) => { - const users = scrapeUsers(dailyScheduleResponse.body); - const dailyScheduleWeeks = scrapeWeeks(dailyScheduleResponse.body); - const basisScheduleWeeks = scrapeWeeks(basisScheduleResponse.body); - - const teachers = users.filter(user => user.type === 't'); - - return getAlts(teachers) - .then(teachersWithAlts => ({ - users: combineUsers([teachersWithAlts, users]), - dailyScheduleWeeks, - basisScheduleWeeks, - })) - .catch(() => ({ - // Just return the user data without the alts if getAlts fails, since - // the alts are non-essential. - users, - dailyScheduleWeeks, - basisScheduleWeeks, - })); - }); -} - -function getMeetingpointDataCacheWrapper() { - if (meetingpointData == null || new Date() - lastUpdate > 30 * 60 * 1000) { // 30 minutes - return getMeetingpointData().then((meetingpointData_) => { - lastUpdate = new Date(); - meetingpointData = meetingpointData_; - - return meetingpointData; - }); - } - - return Promise.resolve(meetingpointData); -} - -module.exports = debounce(getMeetingpointDataCacheWrapper); diff --git a/src/server/lib/getURLOfUser.js b/src/server/lib/getURLOfUser.js deleted file mode 100644 index f44d1a3..0000000 --- a/src/server/lib/getURLOfUser.js +++ /dev/null @@ -1,8 +0,0 @@ -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`; -} - -module.exports = getURLOfUser; diff --git a/src/server/lib/schools/hetmml/getScheduleData.js b/src/server/lib/schools/hetmml/getScheduleData.js new file mode 100644 index 0000000..303af6d --- /dev/null +++ b/src/server/lib/schools/hetmml/getScheduleData.js @@ -0,0 +1,127 @@ +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'); + +let meetingpointData; +let lastUpdate; + +function scrapeUsers(html) { + const page = cheerio.load(html); + const script = page('script').eq(1).text(); + + const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/]; + const items = regexs.map(regex => script.match(regex)[1].split(',').map(item => item.replace(/"/g, ''))); + + const classes = items[0].map((item, index) => ({ + type: 'c', + value: item, + index, + })); + + const teachers = items[1].map((item, index) => ({ + type: 't', + value: item, + index, + })); + + const rooms = items[2].map((item, index) => ({ + type: 'r', + value: item, + index, + })); + + const students = items[3].map((item, index) => ({ + type: 's', + value: item, + index, + })); + + return _.flatten([classes, teachers, rooms, students]); +} + +function scrapeWeeks(html) { + const page = cheerio.load(html); + const weekSelector = page('select[name="week"]'); + const weeks = _.map(weekSelector.children(), option => ({ + id: cheerio(option).attr('value'), + text: cheerio(option).text(), + })); + + return weeks; +} + +function scrapeAltText(html) { + const page = cheerio.load(html); + return page('center > font').eq(2).text().trim(); +} + +function combineUsers(usersArrays) { + return _.uniqBy(_.flatten(usersArrays), user => `${user.type}/${user.value}`); +} + +function getAlts(users) { + const requests = users.map(user => + request(getUrlOfUser('dag', user.type, user.index, 7), { timeout: 8000, encoding: null })); + + return Promise.all(requests).then(teacherResponses => + teacherResponses.map((teacherResponse, index) => { + const teacherResponseBody = iconv.decode(teacherResponse.body, 'iso-8859-1'); + + const teacherName = scrapeAltText(teacherResponseBody); + + return { + ...users[index], + alt: teacherName, + }; + })); +} + +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 }), + ]; + + return Promise.all(navbarRequests) + .then(([dailyScheduleResponse, basisScheduleResponse]) => { + const users = scrapeUsers(dailyScheduleResponse.body); + const dailyScheduleWeeks = scrapeWeeks(dailyScheduleResponse.body); + const basisScheduleWeeks = scrapeWeeks(basisScheduleResponse.body); + + const teachers = users.filter(user => user.type === 't'); + + return getAlts(teachers) + .then(teachersWithAlts => ({ + users: combineUsers([teachersWithAlts, users]), + dailyScheduleWeeks, + basisScheduleWeeks, + })) + .catch(() => ({ + // Just return the user data without the alts if getAlts fails, since + // the alts are non-essential. + users, + dailyScheduleWeeks, + basisScheduleWeeks, + })); + }); +} + +function getScheduleDataCacheWrapper() { + if (meetingpointData == null || new Date() - lastUpdate > 30 * 60 * 1000) { // 30 minutes + return getScheduleData().then((meetingpointData_) => { + lastUpdate = new Date(); + meetingpointData = meetingpointData_; + + return meetingpointData; + }); + } + + return Promise.resolve(meetingpointData); +} + +module.exports = debounce(getScheduleDataCacheWrapper); diff --git a/src/server/lib/schools/hetmml/getURLOfUser.js b/src/server/lib/schools/hetmml/getURLOfUser.js new file mode 100644 index 0000000..f44d1a3 --- /dev/null +++ b/src/server/lib/schools/hetmml/getURLOfUser.js @@ -0,0 +1,8 @@ +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`; +} + +module.exports = getURLOfUser; diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js index 9d965bf..1734cd8 100644 --- a/src/server/routes/getSchedule.js +++ b/src/server/routes/getSchedule.js @@ -4,8 +4,8 @@ const router = express.Router(); const request = require('request'); const iconv = require('iconv-lite'); -const getMeetingpointData = require('../lib/getMeetingpointData'); -const getURLOfUser = require('../lib/getURLOfUser'); +const getScheduleData = require('../lib/schools/hetmml/getScheduleData'); +const getURLOfUser = require('../lib/schools/hetmml/getURLOfUser'); // 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 @@ -25,7 +25,7 @@ function getWeekNumber(target) { } router.get('/:type/:value', (req, res, next) => { - getMeetingpointData().then(({ users }) => { + getScheduleData().then(({ users }) => { const { type, value } = req.params; let { week } = req.query; const user = diff --git a/src/server/routes/index.js b/src/server/routes/index.js index d58962d..af82e1f 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -1,11 +1,11 @@ const express = require('express'); const router = express.Router(); -const getMeetingpointData = require('../lib/getMeetingpointData'); +const getScheduleData = require('../lib/schools/hetmml/getScheduleData'); /* GET home page. */ router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], (req, res) => { - getMeetingpointData().then(({ users, dailyScheduleWeeks, basisScheduleWeeks }) => { + getScheduleData().then(({ users, dailyScheduleWeeks, basisScheduleWeeks }) => { const isBeta = process.env.BETA === '1'; const flags = []; -- cgit v1.1