aboutsummaryrefslogtreecommitdiff
path: root/lib/getUserIndex.js
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-09-13 16:28:53 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-09-13 16:28:53 +0200
commit3fb86482404e11942cd83c3500a297a3991db0e4 (patch)
treec5e7261de72c2b7f871580784525d06d036a6219 /lib/getUserIndex.js
parent5aac32f72eca8c66e879583ce653d07bb3c7370f (diff)
Restructure project
Diffstat (limited to 'lib/getUserIndex.js')
-rw-r--r--lib/getUserIndex.js85
1 files changed, 0 insertions, 85 deletions
diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js
deleted file mode 100644
index db7daa8..0000000
--- a/lib/getUserIndex.js
+++ /dev/null
@@ -1,85 +0,0 @@
-'use strict'
-
-const Promise = require('bluebird')
-const cheerio = require('cheerio')
-const request = Promise.promisify(require('request'))
-
-let userIndex
-let lastUpdate
-
-function updateUserIndex () {
- return new Promise(function (resolve, reject) {
- process.stdout.write('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
- }
- })))
-
- process.stdout.write('done.\n')
-
- resolve(userIndex)
- })
- .catch(error => {
- process.stdout.write('failed.\n')
- reject(error)
- })
- })
-}
-
-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