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