aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-06-20 12:51:37 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-06-20 12:51:37 +0200
commit9a6983d2977dd20944626f9c23f9e02d80b1ca85 (patch)
tree8b24b4fd6964503eb2d1f63473176243b728b6ef
parent69406c43ea899c3956aa16a9e0834aff40c8510f (diff)
Make ?week on /get optional
-rw-r--r--routes/getSchedule.js21
-rw-r--r--routes/opensearch.js12
2 files changed, 32 insertions, 1 deletions
diff --git a/routes/getSchedule.js b/routes/getSchedule.js
index ed9f1a9..f6c3cb6 100644
--- a/routes/getSchedule.js
+++ b/routes/getSchedule.js
@@ -6,10 +6,25 @@ const iconv = require('iconv-lite')
const getUserIndex = require('../lib/getUserIndex')
const getURLOfUser = require('../lib/getURLOfUser')
+// 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 getWeekNumber (target) {
+ const dayNr = (target.getDay() + 6) % 7
+ target.setDate(target.getDate() - dayNr + 3)
+ const firstThursday = target.valueOf()
+ target.setMonth(0, 1)
+ if (target.getDay() !== 4) {
+ target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7)
+ }
+
+ return 1 + Math.ceil((firstThursday - target) / 604800000)
+}
+
router.get('/:type/:value', function (req, res, next) {
getUserIndex().then(users => {
const { type, value } = req.params
- const { week } = req.query
+ let { week } = req.query
const user =
users.filter(user => user.type === type && user.value === value)[0]
@@ -17,6 +32,10 @@ router.get('/:type/:value', function (req, res, next) {
next(new Error(`${type}${value} is not in the user index.`))
}
+ if (!week) {
+ week = getWeekNumber(new Date())
+ }
+
const { index } = user
const url = getURLOfUser(type, index, week)
diff --git a/routes/opensearch.js b/routes/opensearch.js
new file mode 100644
index 0000000..c3e2e57
--- /dev/null
+++ b/routes/opensearch.js
@@ -0,0 +1,12 @@
+'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