aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-05-26 20:08:45 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-05-26 20:08:45 +0200
commit457946531c57a546eeb3c42042ad192ac9826299 (patch)
treedae865d9b416513d4e4405186b483cb9446c6e39
parent065c4feb424046599f22960e0fa19898ad9cd6f7 (diff)
Autoupdate user index cache
-rw-r--r--lib/getUserIndex.js111
-rw-r--r--routes/index.js14
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/')
})
})