From 074b6e3cc6ce3917459175ebc7481b58d3ca728f Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Wed, 7 Feb 2018 16:04:31 +0100 Subject: Fix the backend --- src/server/lib/getMeetingpointData.js | 87 +++++++++++++++-------------------- src/server/lib/getURLOfUser.js | 10 ++-- src/server/lib/getUserIndex.js | 85 ---------------------------------- 3 files changed, 42 insertions(+), 140 deletions(-) delete mode 100644 src/server/lib/getUserIndex.js (limited to 'src/server/lib') diff --git a/src/server/lib/getMeetingpointData.js b/src/server/lib/getMeetingpointData.js index 94cf36c..34ed89f 100644 --- a/src/server/lib/getMeetingpointData.js +++ b/src/server/lib/getMeetingpointData.js @@ -1,83 +1,70 @@ -'use strict' -const Promise = require('bluebird') -const cheerio = require('cheerio') -const _ = require('lodash') -const request = Promise.promisify(require('request')) -let meetingpointData -let lastUpdate +const Promise = require('bluebird'); +const cheerio = require('cheerio'); +const _ = require('lodash'); +const request = Promise.promisify(require('request')); -function getUsers (page) { - const script = page('script').eq(1).text() +let meetingpointData; +let lastUpdate; - const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/] - const items = regexs.map(function (regex) { - return script.match(regex)[1].split(',').map(function (item) { - return item.replace(/"/g, '') - }) - }) +function getUsers(page) { + 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, ''))); return [] - .concat(items[0].map(function (item, index) { - return { + .concat(items[0].map((item, index) => ({ type: 'c', value: item, - index: index - } - })) - .concat(items[1].map(function (item, index) { - return { + index, + }))) + .concat(items[1].map((item, index) => ({ type: 't', value: item, - index: index - } - })) - .concat(items[2].map(function (item, index) { - return { + index, + }))) + .concat(items[2].map((item, index) => ({ type: 'r', value: item, - index: index - } - })) - .concat(items[3].map(function (item, index) { - return { + index, + }))) + .concat(items[3].map((item, index) => ({ type: 's', value: item, - index: index - } - })) + index, + }))); } function getValidWeekNumbers(page) { const weekSelector = page('select[name="week"]'); - const weekNumbers = _.map(weekSelector.children(), option => parseInt(option.attribs.value)) + const weekNumbers = _.map(weekSelector.children(), option => parseInt(option.attribs.value, 10)); return weekNumbers; } function requestData() { - lastUpdate = new Date() + lastUpdate = new Date(); - return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`, { timeout: 5000 }).then((response) => { - const page = cheerio.load(response.body) - const users = getUsers(page) - const validWeekNumbers = getValidWeekNumbers(page) + return request('http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm', { timeout: 5000 }).then((response) => { + const page = cheerio.load(response.body); + const users = getUsers(page); + const validWeekNumbers = getValidWeekNumbers(page); - meetingpointData = { users, validWeekNumbers } + meetingpointData = { users, validWeekNumbers }; - return meetingpointData - }) + return meetingpointData; + }); } -function getMeetingpointData () { +function getMeetingpointData() { if (lastUpdate == null || new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes - return requestData() + return requestData(); } else if (!meetingpointData) { - return Promise.reject() - } else { - return Promise.resolve(meetingpointData) + return Promise.reject(); } + return Promise.resolve(meetingpointData); } -module.exports = getMeetingpointData +module.exports = getMeetingpointData; diff --git a/src/server/lib/getURLOfUser.js b/src/server/lib/getURLOfUser.js index 2de48e6..82b0180 100644 --- a/src/server/lib/getURLOfUser.js +++ b/src/server/lib/getURLOfUser.js @@ -1,8 +1,8 @@ -const leftPad = require('left-pad') // I imported this just to piss you off ;) +const leftPad = require('left-pad'); // I imported this just to piss you off ;) -function getURLOfUser (type, index, week) { - return `http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/` + - `${leftPad(week, 2, '0')}/${type}/${type}${leftPad(index + 1, 5, '0')}.htm` +function getURLOfUser(type, index, week) { + return 'http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/' + + `${leftPad(week, 2, '0')}/${type}/${type}${leftPad(index + 1, 5, '0')}.htm`; } -module.exports = getURLOfUser +module.exports = getURLOfUser; diff --git a/src/server/lib/getUserIndex.js b/src/server/lib/getUserIndex.js deleted file mode 100644 index db7daa8..0000000 --- a/src/server/lib/getUserIndex.js +++ /dev/null @@ -1,85 +0,0 @@ -'use strict' - -const Promise = require('bluebird') -const cheerio = require('cheerio') -const request = Promise.promisify(require('request')) - -let userIndex -let lastUpdate - -function updateUserIndex () { - return new Promise(function (resolve, reject) { - process.stdout.write('Updating user index... ') - request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`) - .then(function (page) { - lastUpdate = new Date() - page = page.body - - const $ = cheerio.load(page) - const $script = $('script').eq(1) - const scriptText = $script.text() - - const regexs = [/var classes = \[(.+)];/, /var teachers = \[(.+)];/, /var rooms = \[(.+)];/, /var students = \[(.+)];/] - const items = regexs.map(function (regex) { - return scriptText.match(regex)[1].split(',').map(function (item) { - return item.replace(/"/g, '') - }) - }) - - userIndex = ([] - .concat(items[0].map(function (item, index) { - return { - type: 'c', - value: item, - index: index - } - })) - .concat(items[1].map(function (item, index) { - return { - type: 't', - value: item, - index: index - } - })) - .concat(items[2].map(function (item, index) { - return { - type: 'r', - value: item, - index: index - } - })) - .concat(items[3].map(function (item, index) { - return { - type: 's', - value: item, - index: index - } - }))) - - process.stdout.write('done.\n') - - resolve(userIndex) - }) - .catch(error => { - process.stdout.write('failed.\n') - reject(error) - }) - }) -} - -function getUserIndex () { - return new Promise((resolve, reject) => { - if (lastUpdate == null) { - updateUserIndex().then(resolve, reject) - } else if (new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes - updateUserIndex().then(resolve, function () { - console.warn('Unable to update userIndex, using cached.') - resolve(userIndex) - }) - } else { - resolve(userIndex) - } - }) -} - -module.exports = getUserIndex -- cgit v1.1