From 457946531c57a546eeb3c42042ad192ac9826299 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 26 May 2017 20:08:45 +0200 Subject: Autoupdate user index cache --- lib/getUserIndex.js | 111 ++++++++++++++++++++++++++++++++-------------------- routes/index.js | 14 +++---- 2 files changed, 73 insertions(+), 52 deletions(-) diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js index 6288dc3..cf409c0 100644 --- a/lib/getUserIndex.js +++ b/lib/getUserIndex.js @@ -4,52 +4,77 @@ const Promise = require('bluebird') const cheerio = require('cheerio') const request = Promise.promisify(require('request')) -function getUserIndex () { - return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`) - .then(function (page) { - 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, '') +let userIndex +let lastUpdate + +function updateUserIndex () { + return new Promise(function (resolve, reject) { + console.log('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 + } + }))) + + resolve(userIndex) }) + .catch(error => { reject(error) }) + }) +} - return ([] - .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 - } - }))) - }) +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 diff --git a/routes/index.js b/routes/index.js index 0fc0d30..3718015 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,9 +4,9 @@ const express = require('express') const router = express.Router() const getUserIndex = require('../lib/getUserIndex') -getUserIndex().then(users => { - /* GET home page. */ - router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { +/* GET home page. */ +router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { + getUserIndex().then(users => { const isBeta = process.env.BETA === '1' let flags = [] @@ -21,12 +21,8 @@ getUserIndex().then(users => { const usersStr = `var USERS = ${JSON.stringify(users)};` res.render('index', { flagsStr, usersStr, isBeta }) - }) -}, error => { - console.error('Unable to get user info, emergency redirect!') - console.error('Error:', error) - - router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { + }).catch(function () { + console.error('Unable to get user info, emergency redirect!') res.redirect('http://www.meetingpointmco.nl/Roosters-AL/doc/') }) }) -- cgit v1.1