diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/getURLOfUser.js | 8 | ||||
-rw-r--r-- | lib/getURLOfUsers.js | 45 | ||||
-rw-r--r-- | lib/getUserIndex.js | 85 |
3 files changed, 93 insertions, 45 deletions
diff --git a/lib/getURLOfUser.js b/lib/getURLOfUser.js new file mode 100644 index 0000000..2de48e6 --- /dev/null +++ b/lib/getURLOfUser.js @@ -0,0 +1,8 @@ +const leftPad = require('left-pad') // I imported this just to piss you off ;) + +function getURLOfUser (type, index, week) { + return `http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/` + + `${leftPad(week, 2, '0')}/${type}/${type}${leftPad(index + 1, 5, '0')}.htm` +} + +module.exports = getURLOfUser diff --git a/lib/getURLOfUsers.js b/lib/getURLOfUsers.js deleted file mode 100644 index 8590ee4..0000000 --- a/lib/getURLOfUsers.js +++ /dev/null @@ -1,45 +0,0 @@ -const leftPad = require('left-pad') - -// copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js, -// were using the same code as they do to be sure that we always get the same -// week number. -function getWeek () { - // Create a copy of this date object - var target = new Date() - - // ISO week date weeks start on monday - // so correct the day number - var dayNr = (target.getDay() + 6) % 7 - - // ISO 8601 states that week 1 is the week - // with the first thursday of that year. - // Set the target date to the thursday in the target week - target.setDate(target.getDate() - dayNr + 3) - - // Store the millisecond value of the target date - var firstThursday = target.valueOf() - - // Set the target to the first thursday of the year - // First set the target to january first - target.setMonth(0, 1) - // Not a thursday? Correct the date to the next thursday - if (target.getDay() !== 4) { - target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7) - } - - // The weeknumber is the number of weeks between the - // first thursday of the year and the thursday in the target week - return 1 + Math.ceil((firstThursday - target) / 604800000) // 604800000 = 7 * 24 * 3600 * 1000 -} - -function getURLOfUsers (weekOffset, type, id) { - return `http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/` + - `${getWeek() + weekOffset}/${type}/${type}${leftPad(id, 5, '0')}.htm` -} - -module.exports = getURLOfUsers - -module.exports.CLASS = 'c' -module.exports.TEACHERS = 't' -module.exports.ROOMS = 'r' -module.exports.STUDENTS = 's' diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js new file mode 100644 index 0000000..db7daa8 --- /dev/null +++ b/lib/getUserIndex.js @@ -0,0 +1,85 @@ +'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 |