aboutsummaryrefslogtreecommitdiff
path: root/src/ics.js
blob: ebf64808fb18d59496e7384a9b384a07559f751e (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
58
59
60
61
62
import * as ics from "ics"

import { prepare } from "./main.js"
import { c, writeICS } from "./files.js"

const daymap = {
  "Mo": "MO",
  "Di": "TU",
  "Mi": "WE",
  "Do": "TH",
  "Fr": "FR",
}

let start = new Date(c.vorlesungszeit[0])
let end = new Date(c.vorlesungszeit[1])

function getDateList(date, time) {
  let year = date.getFullYear() - 0
  let month = date.getMonth() - 0
  let day = date.getDate() - 0

  let timelist = time.split(":")

  let hour = timelist[0] - 0
  let minute = timelist[1] - 0

  return [year, month, day, hour, minute]
}

function generateICS() {
  let tage = prepare(true);
  let veranstaltungen = []

  for (let tag in tage) {
    for (let eintrag of tage[tag]) {
      let until = end.toISOString().replace(/-|\.|:/g, "").replace("000000000", "000000");
      let day = daymap[tag];

      if (!eintrag.name.includes("STYLECLASS")) {
        veranstaltungen.push({
          title: eintrag.name,
          location: eintrag.raum,
          start: getDateList(start, eintrag.von),
          end: getDateList(start, eintrag.bis), // yes this is correct
          recurrenceRule: `FREQ=WEEKLY;INTERVAL=1;BYDAY=${day};UNTIL=${until}`
        })
      }
    }

    start.setDate(start.getDate() + 1);
  }

  const { error, value } = ics.createEvents(veranstaltungen)

  if (error) {
    console.log(error)
  } else {
    writeICS(value)
  }
}

generateICS()