From e3947f0d11864f40a2c0a202af99e0b14ff14dcf Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Mon, 4 Sep 2017 20:12:23 +0200 Subject: Only show weeks that are available --- lib/getUserIndex.js | 104 ++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 47 deletions(-) (limited to 'lib') 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 }) -- cgit v1.1 From 600128bda0390f2051ad20ee42930f036c79323c Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 5 Sep 2017 20:28:32 +0200 Subject: Clear cache automaticly --- lib/getMeetingpointData.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++ lib/getUserIndex.js | 72 ------------------------------------ 2 files changed, 91 insertions(+), 72 deletions(-) create mode 100644 lib/getMeetingpointData.js delete mode 100644 lib/getUserIndex.js (limited to 'lib') diff --git a/lib/getMeetingpointData.js b/lib/getMeetingpointData.js new file mode 100644 index 0000000..fb4c6e4 --- /dev/null +++ b/lib/getMeetingpointData.js @@ -0,0 +1,91 @@ +'use strict' + +const Promise = require('bluebird') +const cheerio = require('cheerio') +const _ = require('lodash') +const request = Promise.promisify(require('request')) + +let meetingpointData +let lastUpdate + +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() { + 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) + + meetingpointData = { users, validWeekNumbers } + + return meetingpointData + }) +} + +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 diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js deleted file mode 100644 index 393eab0..0000000 --- a/lib/getUserIndex.js +++ /dev/null @@ -1,72 +0,0 @@ -'use strict' - -const Promise = require('bluebird') -const cheerio = require('cheerio') -const _ = require('lodash') -const request = Promise.promisify(require('request')) - -exports = {} -module.exports = exports - -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 } - }) -} - -requestData().then(({ users, validWeekNumbers }) => { - exports.users = users - exports.validWeekNumbers = validWeekNumbers -}) -- cgit v1.1 From 4cab46700720f4787d958a258be43e042f2c65e7 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 5 Sep 2017 20:31:59 +0200 Subject: Fix formatting --- lib/getMeetingpointData.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/getMeetingpointData.js b/lib/getMeetingpointData.js index fb4c6e4..9a51cfb 100644 --- a/lib/getMeetingpointData.js +++ b/lib/getMeetingpointData.js @@ -82,8 +82,7 @@ function getMeetingpointData () { return meetingpointData }) - } - else { + } else { return Promise.resolve(meetingpointData) } } -- cgit v1.1 From 702794c8feab387772a8d9913df847c55e8842d5 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Wed, 6 Sep 2017 18:37:42 +0200 Subject: Improve error handling --- lib/getMeetingpointData.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/getMeetingpointData.js b/lib/getMeetingpointData.js index 9a51cfb..94cf36c 100644 --- a/lib/getMeetingpointData.js +++ b/lib/getMeetingpointData.js @@ -59,7 +59,7 @@ function getValidWeekNumbers(page) { function requestData() { lastUpdate = new Date() - return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`).then((response) => { + return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`, { timeout: 5000 }).then((response) => { const page = cheerio.load(response.body) const users = getUsers(page) const validWeekNumbers = getValidWeekNumbers(page) @@ -71,17 +71,10 @@ function requestData() { } function getMeetingpointData () { - if (lastUpdate == null) { + if (lastUpdate == null || new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes 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) } -- cgit v1.1