diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-09-04 20:12:23 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-09-04 20:12:23 +0200 |
commit | e3947f0d11864f40a2c0a202af99e0b14ff14dcf (patch) | |
tree | 38d1eaa9509cafa616c13ec60481418e8c1dc4cd /lib/getUserIndex.js | |
parent | 92a85bee3371702dd1a053dcc2f7bd8922802795 (diff) |
Only show weeks that are available
Diffstat (limited to 'lib/getUserIndex.js')
-rw-r--r-- | lib/getUserIndex.js | 104 |
1 files changed, 57 insertions, 47 deletions
diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js index d71cb3b..393eab0 100644 --- a/lib/getUserIndex.js +++ b/lib/getUserIndex.js @@ -2,61 +2,71 @@ const Promise = require('bluebird') const cheerio = require('cheerio') +const _ = require('lodash') const request = Promise.promisify(require('request')) exports = {} module.exports = exports -function getStandardUsers () { - return new Promise(function (resolve, reject) { - 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, '') - }) - }) - - resolve([] - .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 getUsers (page) { + const script = page('script').eq(1).text() + + const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/] + const items = regexs.map(function (regex) { + return script.match(regex)[1].split(',').map(function (item) { + return item.replace(/"/g, '') }) }) + + 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 getValidWeekNumbers(page) { + const weekSelector = page('select[name="week"]'); + const weekNumbers = _.map(weekSelector.children(), option => parseInt(option.attribs.value)) + + return weekNumbers; +} + +function requestData() { + 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 } + }) } -getStandardUsers().then(users => { +requestData().then(({ users, validWeekNumbers }) => { exports.users = users + exports.validWeekNumbers = validWeekNumbers }) |