diff options
-rw-r--r-- | lib/getMeetingpointData.js (renamed from lib/getUserIndex.js) | 33 | ||||
-rw-r--r-- | routes/index.js | 23 |
2 files changed, 40 insertions, 16 deletions
diff --git a/lib/getUserIndex.js b/lib/getMeetingpointData.js index 393eab0..fb4c6e4 100644 --- a/lib/getUserIndex.js +++ b/lib/getMeetingpointData.js @@ -5,8 +5,8 @@ const cheerio = require('cheerio') const _ = require('lodash') const request = Promise.promisify(require('request')) -exports = {} -module.exports = exports +let meetingpointData +let lastUpdate function getUsers (page) { const script = page('script').eq(1).text() @@ -57,16 +57,35 @@ function getValidWeekNumbers(page) { } function requestData() { + lastUpdate = new Date() + return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`).then((response) => { const page = cheerio.load(response.body) const users = getUsers(page) const validWeekNumbers = getValidWeekNumbers(page) - return { users, validWeekNumbers } + meetingpointData = { users, validWeekNumbers } + + return meetingpointData }) } -requestData().then(({ users, validWeekNumbers }) => { - exports.users = users - exports.validWeekNumbers = validWeekNumbers -}) +function getMeetingpointData () { + if (lastUpdate == null) { + return requestData() + } else if (!meetingpointData) { + // The first request hasn't resolved yet, throw an error. + return Promise.reject() + } else if (new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes + return requestData().catch(function () { + console.warn('Unable to update userIndex, using cached.') + + return meetingpointData + }) + } + else { + return Promise.resolve(meetingpointData) + } +} + +module.exports = getMeetingpointData diff --git a/routes/index.js b/routes/index.js index 570fe5f..68f29b7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,19 +2,24 @@ const express = require('express') const router = express.Router() -const data = require('../lib/getUserIndex') +const getMeetingpointData = require('../lib/getMeetingpointData') /* GET home page. */ router.get('/', function (req, res, next) { - let flags = [] - if (req.query.nfd != null) { - flags.push('NO_FEATURE_DETECT') - } + getMeetingpointData().then(data => { + let flags = [] + if (req.query.nfd != null) { + flags.push('NO_FEATURE_DETECT') + } - const flagsStr = `var FLAGS = ${JSON.stringify(flags)};` - const usersStr = `var USERS = ${JSON.stringify(data.users)};` - const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}` - res.render('index', { flagsStr, usersStr, validWeekNumbersStr }) + const flagsStr = `var FLAGS = ${JSON.stringify(flags)};` + const usersStr = `var USERS = ${JSON.stringify(data.users)};` + const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}` + res.render('index', { flagsStr, usersStr, validWeekNumbersStr }) + }).catch(function () { + console.error('Unable to get user info, emergency redirect!') + res.redirect('http://www.meetingpointmco.nl/Roosters-AL/doc/') + }) }) module.exports = router |