-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (53 loc) · 2.21 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require('dotenv').config()
const express = require('express')
, rp = require('request-promise-native')
, fs = require('fs')
, ical2json = require('ical2json')
, subjects = require('./subjects')
const updateCalFile = () => {
console.log('Updating cal.')
rp(process.env.CAL_URL)
.then(body => {
console.log('Fetched calendar:', body.length)
const converted = ical2json.convert(body)
const cal = converted['VCALENDAR'][0]
const events = Array.from(cal['VEVENT'])
.filter(x => x['SUMMARY'] && x['LOCATION'])
.map(event => {
event['SUMMARY'] = event['SUMMARY']
.split('Emne: ')
.filter(x => subjects.hasOwnProperty(x))
.map(x => `${x} | ${subjects[x]}`)[0]
event['LOCATION'] = event['LOCATION']
//.replace('\\, ', '')
.split('\\, ')
.filter(x => x !== '' && x !== undefined)
.reduce((acc, cur) => `${acc} + ${cur}`) + '\\, HVL Kronstad'
event['GEO'] = '60.369196;5.350845'
event['URL'] = 'https://hvl.no'
const description = event['DESCRIPTION']
.split('\\n')
event['DESCRIPTION'] = `Forelesning: ${description[description.length - 1]}`
description
.filter((x, i, arr) => i !== arr.length - 1)
.forEach(attendee => {
event[`ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=${attendee}`] = 'MAILTO:[email protected]'
})
return event
})
console.log(events.length + ' events')
cal['PRODID'] = 'TimeEdit'
cal['X-WR-CALNAME'] = 'Timeplan DATA-A H2017, HVL'
cal['VEVENT'] = events
fs.writeFile(process.cwd() + '/cal/cal.ics', ical2json.revert(converted), () => {
console.log('File written.\n')
})
})
.catch(err => console.log('Could not get calendar.'))
}
setTimeout(updateCalFile, 150000)
updateCalFile()
const app = express()
.get('/', (req, res) => res.redirect('/Calendar.ics'))
.get('/Calendar.ics', (req, res) => res.sendFile(process.cwd() + '/cal/cal.ics'))
.listen(process.env.PORT, () => console.log('Server started.'))