aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-09-05 20:28:32 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-09-05 20:28:32 +0200
commit600128bda0390f2051ad20ee42930f036c79323c (patch)
treed8af17fd1f30dc4b11f44763c3addba104636f40 /lib
parente3947f0d11864f40a2c0a202af99e0b14ff14dcf (diff)
Clear cache automaticly
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