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.
 

189 lines
5.0 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 } from '../config'
import { wrapCrawler } from '../crawlutil'
import { InstitutionElement } from '../element'
import { generateId } from '../generate-id'
import { crawl, isConfigured } from './crawler'
export function cwgElementCreator (): InstitutionElement | null {
if (!isConfigured) {
return null
}
const crawlUtil = wrapCrawler(() => crawl())
const router = Router()
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'
},
{
id: 'default',
usageConditionId: '_true',
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'
}
]
})
})
router.get('/content/default', (_, res, next) => {
crawlUtil.getLastSuccessPromise().then((data) => {
res.json({
file: data.downloads.map((item) => ({
type: 'download',
mimeType: item.mimeType,
lastModified: item.lastModified,
title: item.title,
file: item.file,
id: generateId('download_' + item.title),
notify: false
})),
message: data.messages.map((item) => ({
id: generateId(item.content),
title: item.title,
content: item.content,
notify: false
}))
})
}).catch((ex) => next(ex))
})
router.get('/content/student', (req, res, next) => {
const authData = auth(req)
if ((!authData) || authData.pass !== config.cwg.password.student) {
res.setHeader('WWW-Authenticate', 'Basic realm="Login"')
res.sendStatus(401)
return
}
crawlUtil.getLastSuccessPromise().then((data) => {
res.json({
file: data.studentPlans.map((item) => ({
type: 'plan',
mimeType: item.mimeType,
lastModified: item.lastModified,
title: item.title,
file: item.file,
id: generateId('plan_' + item.title),
notify: true
})),
message: []
})
}).catch((ex) => next(ex))
})
router.get('/content/teacher', (req, res, next) => {
const authData = auth(req)
if ((!authData) || authData.pass !== config.cwg.password.teacher) {
res.setHeader('WWW-Authenticate', 'Basic realm="Login"')
res.sendStatus(401)
return
}
crawlUtil.getLastSuccessPromise().then((data) => {
res.json({
file: data.teacherPlans.map((item) => ({
type: 'plan',
mimeType: item.mimeType,
lastModified: item.lastModified,
title: item.title,
file: item.file,
id: generateId('plan_' + item.title),
notify: true
})),
message: []
})
}).catch((ex) => next(ex))
})
return {
id: 'cwg-halle',
title: 'Christian-Wolff-Gymnasium',
router,
getStatus: () => crawlUtil.getLastPromise()
}
}