summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 3d7e588825512b03bade501718335813df67ccf7 (plain)
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
import * as moment from 'moment';
import * as BPromise from 'bluebird';
import * as fs from 'fs';

import {SplusApi} from './core/SplusApi';
import {IEvent} from './core/IEvent';
import {ILecture} from './core/ILecture';
import {IcalSink} from './sinks/IcalSink';
import {GoogleCalendarSink} from './sinks/GoogleCalendarSink';

const range = (upper: number): number[] => Array.from(Array(upper), (x, i) => i);
const xprod = (arr1, arr2) => [].concat(...arr1.map((e1) => arr2.map((e2) => [e1, e2])));
const isIncluded = (s1) => (s2) => s1.includes(s2);
const anyTrue = (arr, cond) => arr.filter(cond).length > 0;

async function main(configPath) {
    const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
    const sink = config.icsPath == undefined? new GoogleCalendarSink() : new IcalSink(config.icsPath);

    const baseDate = moment().startOf('week');

    await BPromise.map(xprod(config.courses, range(config.prefetchWeeks)), async ([course, weeksAhead]) => {
        const unfilteredLectures = await SplusApi.getData(course, baseDate.weeks() + weeksAhead);
        const lectures = unfilteredLectures.filter(lecture => anyTrue(config.titleWhitelist, isIncluded(lecture.title)));

        await BPromise.map(lectures, async (lec: ILecture) => {
            let beginDate = baseDate.clone();
            beginDate.add(moment.duration({
                days: lec.day + 1, // for moment: 1 = Monday
                hours: lec.begin,
                weeks: weeksAhead,
            }));

            let endDate = baseDate.clone();
            endDate.add(moment.duration({
                days: lec.day + 1,
                hours: lec.end,
                weeks: weeksAhead,
            }));
            const event: IEvent = {
                summary: lec.title,
                description: lec.lecturer + (lec.info !== '' ? ' - ' : '') + lec.info,
                location: lec.room,
                start: beginDate.toDate(),
                end: endDate.toDate(),
            };

            await sink.createEvent(event);

            console.log(lec.title, beginDate.toISOString(), endDate.toISOString());
        });
    });

    await sink.commit();
}

main(process.argv[2] || './config-example.json').catch(console.log);