You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

169 lines
4.6 KiB

/*
* vertretungsplan.io custom crawler
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as auth from 'basic-auth'
import { Router } from 'express'
import { config as baseConfig } from '../config'
import { wrapCrawler } from '../crawlutil'
import { InstitutionElement } from '../element'
import { crawlCategories } from './crawl'
export function lbzElementCreator (): InstitutionElement | null {
const configData = baseConfig.lbzBlindeHalle
if (!(configData.studentPassword && configData.teacherPassword)) {
return null
}
const id = 'lbz-fur-blinde-und-sehbehinderte'
const crawlUtil = wrapCrawler(() => crawlCategories())
const router = Router()
router.get('/file/:hash', (req, res, next) => {
const hash: string = req.params.hash
crawlUtil.getLastSuccessPromise().then((data) => {
const files = [data.student, data.teacher]
const file = files.find((file) => file.sha512 === hash)
if (file) {
res.type('html').end(file.content)
} else {
res.sendStatus(404)
}
}).catch((ex) => next(ex))
})
;(['student', 'teacher'] as Array<'student' | 'teacher'>).forEach((type) => {
const password = type === 'student' ? configData.studentPassword : configData.teacherPassword
router.get('/content/' + type, (req, res, next) => {
const authData = auth(req)
if ((!authData) || authData.pass !== password) {
res.setHeader('WWW-Authenticate', 'Basic realm="Login"')
res.sendStatus(401)
return
}
crawlUtil.getLastSuccessPromise().then((data) => {
const item = type === 'student' ? data.student : data.teacher
res.json({
file: [
{
type: 'plan',
mimeType: 'text/html',
// this does not work because the server does not report it
// lastModified: item.lastModified,
title: item.title,
file: [
{
url: baseConfig.publicUrl + '/vp-institution/' + id + '/file/' + item.sha512,
size: item.size,
sha512: item.sha512
}
],
id: 'plan',
notify: true
}
],
message: []
})
}).catch((ex) => next(ex))
})
})
router.get('/config/default', (_, res) => {
res.json({
config: [
{
param: 'userType',
type: 'radio',
value: 'student',
label: 'Schüler',
visibilityConditionId: '_true'
},
{
param: 'userType',
type: 'radio',
value: 'teacher',
label: 'Lehrer',
visibilityConditionId: '_true'
},
{
param: 'studentPassword',
type: 'password',
visibilityConditionId: 'isStudent',
value: '',
label: 'Schüler-Passwort'
},
{
param: 'teacherPassword',
type: 'password',
visibilityConditionId: 'isTeacher',
value: '',
label: 'Lehrer-Passwort'
}
],
configValidationConditionId: 'hasValidUserType',
contentBucketSets: [
{
id: 'student',
passwordParam: 'studentPassword',
usageConditionId: 'isStudent',
type: 'content'
},
{
id: 'teacher',
passwordParam: 'teacherPassword',
usageConditionId: 'isTeacher',
type: 'content'
}
],
conditionSets: [
{
id: 'isStudent',
type: 'paramIs',
left: 'userType',
right: 'student'
},
{
id: 'isTeacher',
type: 'paramIs',
left: 'userType',
right: 'teacher'
},
{
id: 'hasValidUserType',
type: 'or',
left: 'isStudent',
right: 'isTeacher'
}
]
})
})
return {
id,
title: 'LBZ für Blinde und Sehbehinderte Halle',
router,
getStatus: () => crawlUtil.getLastPromise()
}
}