aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/getMeetingpointData.js (renamed from lib/getUserIndex.js)33
1 files changed, 26 insertions, 7 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