aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-02-07 16:04:31 +0100
committerNoah Loomans <noahloomans@gmail.com>2018-02-07 16:04:31 +0100
commit074b6e3cc6ce3917459175ebc7481b58d3ca728f (patch)
treed9d8859abaefceb419ca937c56b76ab633023c4d /src
parent5ebd34ed83f878976b89da78d6bdaa1dc9e935d7 (diff)
Fix the backend
Diffstat (limited to 'src')
-rw-r--r--src/server/app.js76
-rw-r--r--src/server/lib/getMeetingpointData.js87
-rw-r--r--src/server/lib/getURLOfUser.js10
-rw-r--r--src/server/lib/getUserIndex.js85
-rw-r--r--src/server/routes/getSchedule.js15
-rw-r--r--src/server/routes/index.js41
-rw-r--r--src/server/routes/manifest.js21
-rw-r--r--src/server/routes/opensearch.js12
-rw-r--r--src/server/routes/slack.js60
9 files changed, 110 insertions, 297 deletions
diff --git a/src/server/app.js b/src/server/app.js
index 3477ed5..df6ab4c 100644
--- a/src/server/app.js
+++ b/src/server/app.js
@@ -1,64 +1,62 @@
-const express = require('express')
-const path = require('path')
-const logger = require('morgan')
-const cookieParser = require('cookie-parser')
-const bodyParser = require('body-parser')
-const compression = require('compression')
+const express = require('express');
+const path = require('path');
+const logger = require('morgan');
+const cookieParser = require('cookie-parser');
+const bodyParser = require('body-parser');
+const compression = require('compression');
-const routes = require('./routes/index')
-const getSchedule = require('./routes/getSchedule')
-const manifest = require('./routes/manifest')
-const slack = require('./routes/slack')
+const routes = require('./routes/index');
+const getSchedule = require('./routes/getSchedule');
+const manifest = require('./routes/manifest');
-const app = express()
+const app = express();
-app.use(compression())
+app.use(compression());
// view engine setup
-app.set('views', path.join(__dirname, '../client/views'))
-app.set('view engine', 'jade')
+app.set('views', path.join(__dirname, '../client/views'));
+app.set('view engine', 'jade');
-app.use(logger('dev'))
-app.use(bodyParser.json())
-app.use(bodyParser.urlencoded({ extended: false }))
-app.use(cookieParser())
+app.use(logger('dev'));
+app.use(bodyParser.json());
+app.use(bodyParser.urlencoded({ extended: false }));
+app.use(cookieParser());
-app.use('/manifest.webmanifest', manifest)
-app.use(express.static(path.join(__dirname, '../client/static')))
+app.use('/manifest.webmanifest', manifest);
+app.use(express.static(path.join(__dirname, '../client/static')));
-app.use('/', routes)
-app.use('/get', getSchedule)
-app.use('/slack', slack)
+app.use('/', routes);
+app.use('/get', getSchedule);
// catch 404 and forward to error handler
-app.use(function (req, res, next) {
- const err = new Error('Not Found')
- err.status = 404
- next(err)
-})
+app.use((req, res, next) => {
+ const err = new Error('Not Found');
+ err.status = 404;
+ next(err);
+});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
- app.use(function (err, req, res, next) {
- res.status(err.status || 500)
+ app.use((err, req, res) => {
+ res.status(err.status || 500);
res.render('error', {
message: err.message,
- error: err
- })
- })
+ error: err,
+ });
+ });
}
// production error handler
// no stacktraces leaked to user
-app.use(function (err, req, res, next) {
- res.status(err.status || 500)
+app.use((err, req, res) => {
+ res.status(err.status || 500);
res.render('error', {
message: err.message,
- error: {}
- })
-})
+ error: {},
+ });
+});
-module.exports = app
+module.exports = app;
diff --git a/src/server/lib/getMeetingpointData.js b/src/server/lib/getMeetingpointData.js
index 94cf36c..34ed89f 100644
--- a/src/server/lib/getMeetingpointData.js
+++ b/src/server/lib/getMeetingpointData.js
@@ -1,83 +1,70 @@
-'use strict'
-const Promise = require('bluebird')
-const cheerio = require('cheerio')
-const _ = require('lodash')
-const request = Promise.promisify(require('request'))
-let meetingpointData
-let lastUpdate
+const Promise = require('bluebird');
+const cheerio = require('cheerio');
+const _ = require('lodash');
+const request = Promise.promisify(require('request'));
-function getUsers (page) {
- const script = page('script').eq(1).text()
+let meetingpointData;
+let lastUpdate;
- 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, '')
- })
- })
+function getUsers(page) {
+ const script = page('script').eq(1).text();
+
+ const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/];
+ const items = regexs.map(regex => script.match(regex)[1].split(',').map(item => item.replace(/"/g, '')));
return []
- .concat(items[0].map(function (item, index) {
- return {
+ .concat(items[0].map((item, index) => ({
type: 'c',
value: item,
- index: index
- }
- }))
- .concat(items[1].map(function (item, index) {
- return {
+ index,
+ })))
+ .concat(items[1].map((item, index) => ({
type: 't',
value: item,
- index: index
- }
- }))
- .concat(items[2].map(function (item, index) {
- return {
+ index,
+ })))
+ .concat(items[2].map((item, index) => ({
type: 'r',
value: item,
- index: index
- }
- }))
- .concat(items[3].map(function (item, index) {
- return {
+ index,
+ })))
+ .concat(items[3].map((item, index) => ({
type: 's',
value: item,
- index: index
- }
- }))
+ index,
+ })));
}
function getValidWeekNumbers(page) {
const weekSelector = page('select[name="week"]');
- const weekNumbers = _.map(weekSelector.children(), option => parseInt(option.attribs.value))
+ const weekNumbers = _.map(weekSelector.children(), option => parseInt(option.attribs.value, 10));
return weekNumbers;
}
function requestData() {
- lastUpdate = new Date()
+ lastUpdate = new Date();
- 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)
+ 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);
- meetingpointData = { users, validWeekNumbers }
+ meetingpointData = { users, validWeekNumbers };
- return meetingpointData
- })
+ return meetingpointData;
+ });
}
-function getMeetingpointData () {
+function getMeetingpointData() {
if (lastUpdate == null || new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes
- return requestData()
+ return requestData();
} else if (!meetingpointData) {
- return Promise.reject()
- } else {
- return Promise.resolve(meetingpointData)
+ return Promise.reject();
}
+ return Promise.resolve(meetingpointData);
}
-module.exports = getMeetingpointData
+module.exports = getMeetingpointData;
diff --git a/src/server/lib/getURLOfUser.js b/src/server/lib/getURLOfUser.js
index 2de48e6..82b0180 100644
--- a/src/server/lib/getURLOfUser.js
+++ b/src/server/lib/getURLOfUser.js
@@ -1,8 +1,8 @@
-const leftPad = require('left-pad') // I imported this just to piss you off ;)
+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`
+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
+module.exports = getURLOfUser;
diff --git a/src/server/lib/getUserIndex.js b/src/server/lib/getUserIndex.js
deleted file mode 100644
index db7daa8..0000000
--- a/src/server/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
diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js
index f6bc256..5e9bb29 100644
--- a/src/server/routes/getSchedule.js
+++ b/src/server/routes/getSchedule.js
@@ -3,9 +3,8 @@ const express = require('express');
const router = express.Router();
const request = require('request');
const iconv = require('iconv-lite');
-const webshot = require('webshot');
-const getUserIndex = require('../lib/getUserIndex');
+const getMeetingpointData = require('../lib/getMeetingpointData');
const getURLOfUser = require('../lib/getURLOfUser');
// copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js,
@@ -25,18 +24,8 @@ function getWeekNumber(target) {
return 1 + Math.ceil((firstThursday - target) / 604800000);
}
-// router.get('/:type/:value.png', (req, res) => {
-// const port = process.env.PORT || 3000;
-// const { type, value } = req.params;
-// const stream = webshot(
-// `http://localhost:${port}/get/${type}/${value}`,
-// { customCSS: 'body { background-color: white; }' },
-// );
-// stream.pipe(res);
-// });
-
router.get('/:type/:value', (req, res, next) => {
- getUserIndex().then((users) => {
+ getMeetingpointData().then(({ users }) => {
const { type, value } = req.params;
let { week } = req.query;
const user =
diff --git a/src/server/routes/index.js b/src/server/routes/index.js
index d2267ba..7e97fea 100644
--- a/src/server/routes/index.js
+++ b/src/server/routes/index.js
@@ -1,31 +1,30 @@
-'use strict'
+const express = require('express');
-const express = require('express')
-const router = express.Router()
-const getMeetingpointData = require('../lib/getMeetingpointData')
+const router = express.Router();
+const getMeetingpointData = require('../lib/getMeetingpointData');
/* GET home page. */
-router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) {
- getMeetingpointData().then(data => {
- const isBeta = process.env.BETA === '1'
+router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], (req, res) => {
+ getMeetingpointData().then((data) => {
+ const isBeta = process.env.BETA === '1';
- let flags = []
+ const flags = [];
if (isBeta) {
- flags.push('BETA')
- flags.push('NO_FEATURE_DETECT')
+ flags.push('BETA');
+ flags.push('NO_FEATURE_DETECT');
} else if (req.query.nfd != null) {
- flags.push('NO_FEATURE_DETECT')
+ flags.push('NO_FEATURE_DETECT');
}
- const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`
- const usersStr = `var USERS = ${JSON.stringify(data.users)};`
- const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`
+ const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`;
+ const usersStr = `var USERS = ${JSON.stringify(data.users)};`;
+ const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`;
- res.render('index', { flagsStr, usersStr, validWeekNumbersStr })
- }).catch(function () {
- console.error('Unable to get user info, emergency redirect!')
- res.render('redirect')
- })
-})
+ res.render('index', { flagsStr, usersStr, validWeekNumbersStr });
+ }).catch(() => {
+ console.error('Unable to get user info, emergency redirect!');
+ res.render('redirect');
+ });
+});
-module.exports = router
+module.exports = router;
diff --git a/src/server/routes/manifest.js b/src/server/routes/manifest.js
index b2ce55f..4cb6342 100644
--- a/src/server/routes/manifest.js
+++ b/src/server/routes/manifest.js
@@ -1,19 +1,16 @@
-'use strict'
+const express = require('express');
-const express = require('express')
-const router = express.Router()
-const path = require('path')
+const router = express.Router();
+const path = require('path');
-router.get('/', function (req, res, next) {
- console.log('got a request')
-
- const isBeta = process.env.BETA === '1'
+router.get('/', (req, res) => {
+ const isBeta = process.env.BETA === '1';
if (isBeta) {
- res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../public') })
+ res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../../client/static') });
} else {
- res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../public') })
+ res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../../client/static') });
}
-})
+});
-module.exports = router
+module.exports = router;
diff --git a/src/server/routes/opensearch.js b/src/server/routes/opensearch.js
deleted file mode 100644
index c3e2e57..0000000
--- a/src/server/routes/opensearch.js
+++ /dev/null
@@ -1,12 +0,0 @@
-'use strict'
-
-const express = require('express')
-const router = express.Router()
-const path = require('path')
-
-router.get('/', function (req, res, next) {
- res.setHeader('content-type', 'application/opensearchdescription+xml')
- res.sendFile('opensearch.xml', { root: path.join(__dirname, '../public') })
-})
-
-module.exports = router
diff --git a/src/server/routes/slack.js b/src/server/routes/slack.js
deleted file mode 100644
index e256f14..0000000
--- a/src/server/routes/slack.js
+++ /dev/null
@@ -1,60 +0,0 @@
-const express = require('express')
-const router = express.Router()
-
-const getUserIndex = require('../lib/getUserIndex')
-
-function generateResponse(query) {
- return getUserIndex().then(users => {
- const user =
- users.filter(user => user.value === query)[0]
-
- if (!user) {
- return {
- "response_type": "ephemeral",
- "mrkdwn": true,
- "text": `Sorry, I tried my best, but I couldn't find _${query}_`
- }
- }
-
- return {
- "response_type": "in_channel",
- "text": `Here is the schedule of _${query}_`,
- "mrkdwn": true,
- "attachments": [
- {
- "fallback": `https://beta.rooster.hetmml.nl/${user.type}/${user.value}`,
- "image_url": `https://beta.rooster.hetmml.nl/get/${user.type}/${user.value}.png`
- }
- ]
- }
- })
-}
-
-router.all('/', function (req, res, next) {
- const query = req.body.text || req.query.text
-
- if (query.indexOf('!') === 0) {
- switch (query) {
- case "!help":
- res.json({
- "response_type": "ephemeral",
- "mrkdwn": true,
- "text": "Here are some examples on how you can use me\n>/rooster 18561\n\n>/rooster akh\n\n>/rooster 6-5H2\n\n>/rooster 008-mk\n\nPlease note that the following does not work (yet)",
- "attachments": [{
- "text": "/rooster 5h2",
- "color": "danger"
- }]
- })
- default:
- res.json({
- "response_type": "ephemeral",
- "text": "Unrecognized command, try !help"
- })
- break;
- }
- }
-
- generateResponse(query).then((json) => res.json(json))
-})
-
-module.exports = router