-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
302 lines (260 loc) · 14.7 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const fs = require("fs");
const ICAL = require("ical.js");
const xmlbuilder = require("xmlbuilder");
const sanitizeHtml = require("sanitize-html");
const { Settings, DateTime } = require("luxon");
const TZ = "Europe/Berlin";
Settings.defaultZoneName = TZ;
// using a bit of ical.timezones.js from a gh actions build of https://github.com/kewisch/ical.js
(function() {
function register(tzdata) { ICAL.TimezoneService.register(ICAL.Component.fromString("BEGIN:VTIMEZONE\r\n" + tzdata + "\r\nEND:VTIMEZONE")) };
ICAL.TimezoneService.IANA_TZDB_VERSION = "2024a";
register("TZID:CET\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:CET\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD");
register("TZID:CST6CDT\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:CST6CDT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CDT\r\nTZOFFSETFROM:-0600\r\nTZOFFSETTO:-0500\r\nDTSTART:19700308T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CST\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0600\r\nDTSTART:19701101T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\r\nEND:STANDARD");
register("TZID:EET\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:EET\r\nBEGIN:DAYLIGHT\r\nTZNAME:EEST\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nDTSTART:19700329T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:EET\r\nTZOFFSETFROM:+0300\r\nTZOFFSETTO:+0200\r\nDTSTART:19701025T040000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD");
register("TZID:EST\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:EST\r\nBEGIN:STANDARD\r\nTZNAME:EST\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0500\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:EST5EDT\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:EST5EDT\r\nBEGIN:DAYLIGHT\r\nTZNAME:EDT\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nDTSTART:19700308T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:EST\r\nTZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nDTSTART:19701101T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\r\nEND:STANDARD");
register("TZID:Etc/GMT\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT\r\nBEGIN:STANDARD\r\nTZNAME:GMT\r\nTZOFFSETFROM:+0000\r\nTZOFFSETTO:+0000\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+1\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+1\r\nBEGIN:STANDARD\r\nTZNAME:-01\r\nTZOFFSETFROM:-0100\r\nTZOFFSETTO:-0100\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+10\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+10\r\nBEGIN:STANDARD\r\nTZNAME:-10\r\nTZOFFSETFROM:-1000\r\nTZOFFSETTO:-1000\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+11\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+11\r\nBEGIN:STANDARD\r\nTZNAME:-11\r\nTZOFFSETFROM:-1100\r\nTZOFFSETTO:-1100\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+12\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+12\r\nBEGIN:STANDARD\r\nTZNAME:-12\r\nTZOFFSETFROM:-1200\r\nTZOFFSETTO:-1200\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+2\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+2\r\nBEGIN:STANDARD\r\nTZNAME:-02\r\nTZOFFSETFROM:-0200\r\nTZOFFSETTO:-0200\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+3\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+3\r\nBEGIN:STANDARD\r\nTZNAME:-03\r\nTZOFFSETFROM:-0300\r\nTZOFFSETTO:-0300\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+4\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+4\r\nBEGIN:STANDARD\r\nTZNAME:-04\r\nTZOFFSETFROM:-0400\r\nTZOFFSETTO:-0400\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+5\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+5\r\nBEGIN:STANDARD\r\nTZNAME:-05\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0500\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+6\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+6\r\nBEGIN:STANDARD\r\nTZNAME:-06\r\nTZOFFSETFROM:-0600\r\nTZOFFSETTO:-0600\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+7\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+7\r\nBEGIN:STANDARD\r\nTZNAME:-07\r\nTZOFFSETFROM:-0700\r\nTZOFFSETTO:-0700\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+8\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+8\r\nBEGIN:STANDARD\r\nTZNAME:-08\r\nTZOFFSETFROM:-0800\r\nTZOFFSETTO:-0800\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT+9\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT+9\r\nBEGIN:STANDARD\r\nTZNAME:-09\r\nTZOFFSETFROM:-0900\r\nTZOFFSETTO:-0900\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-1\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-1\r\nBEGIN:STANDARD\r\nTZNAME:+01\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-10\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-10\r\nBEGIN:STANDARD\r\nTZNAME:+10\r\nTZOFFSETFROM:+1000\r\nTZOFFSETTO:+1000\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-11\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-11\r\nBEGIN:STANDARD\r\nTZNAME:+11\r\nTZOFFSETFROM:+1100\r\nTZOFFSETTO:+1100\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-12\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-12\r\nBEGIN:STANDARD\r\nTZNAME:+12\r\nTZOFFSETFROM:+1200\r\nTZOFFSETTO:+1200\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-13\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-13\r\nBEGIN:STANDARD\r\nTZNAME:+13\r\nTZOFFSETFROM:+1300\r\nTZOFFSETTO:+1300\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-14\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-14\r\nBEGIN:STANDARD\r\nTZNAME:+14\r\nTZOFFSETFROM:+1400\r\nTZOFFSETTO:+1400\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-2\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-2\r\nBEGIN:STANDARD\r\nTZNAME:+02\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0200\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-3\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-3\r\nBEGIN:STANDARD\r\nTZNAME:+03\r\nTZOFFSETFROM:+0300\r\nTZOFFSETTO:+0300\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-4\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-4\r\nBEGIN:STANDARD\r\nTZNAME:+04\r\nTZOFFSETFROM:+0400\r\nTZOFFSETTO:+0400\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-5\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-5\r\nBEGIN:STANDARD\r\nTZNAME:+05\r\nTZOFFSETFROM:+0500\r\nTZOFFSETTO:+0500\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-6\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-6\r\nBEGIN:STANDARD\r\nTZNAME:+06\r\nTZOFFSETFROM:+0600\r\nTZOFFSETTO:+0600\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-7\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-7\r\nBEGIN:STANDARD\r\nTZNAME:+07\r\nTZOFFSETFROM:+0700\r\nTZOFFSETTO:+0700\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-8\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-8\r\nBEGIN:STANDARD\r\nTZNAME:+08\r\nTZOFFSETFROM:+0800\r\nTZOFFSETTO:+0800\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/GMT-9\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/GMT-9\r\nBEGIN:STANDARD\r\nTZNAME:+09\r\nTZOFFSETFROM:+0900\r\nTZOFFSETTO:+0900\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Etc/UTC\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Etc/UTC\r\nBEGIN:STANDARD\r\nTZNAME:UTC\r\nTZOFFSETFROM:+0000\r\nTZOFFSETTO:+0000\r\nDTSTART:19700101T000000\r\nEND:STANDARD");
register("TZID:Europe/Berlin\r\nLAST-MODIFIED:20240522T121408Z\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD");
})();
const DEFAULT_ROOM = "Salon";
// FILTER_START: set to null if all events should be used
const FILTER_START = DateTime.local().startOf('month');
var data = fs.readFileSync("basic.ics", "utf8");
var jCalData = ICAL.parse(data);
var comp = new ICAL.Component(jCalData);
var DATES = {};
comp.getAllSubcomponents("vevent").forEach(vevent => {
var event = new ICAL.Event(vevent);
if (event.isRecurring()) {
// recurring events get handled below
return;
}
let startDate = DateTime.fromJSDate(event.startDate.toJSDate(), {
zone: TZ
});
let key = startDate.toISODate();
if (typeof DATES[key] === "undefined") {
DATES[key] = [];
}
DATES[key].push({ start: startDate, event: event });
});
// add recurring events
comp.getAllSubcomponents("vevent").forEach(vevent => {
var event = new ICAL.Event(vevent);
if (!event.isRecurring()) {
return;
}
var expand = event.iterator();
var eventEndOfYearOfFirstStartDate, next;
while ((next = expand.next())) {
let nextStartDate = DateTime.fromJSDate(next.toJSDate(), {
zone: TZ
});
if (!eventEndOfYearOfFirstStartDate) {
eventEndOfYearOfFirstStartDate = nextStartDate.plus({ years: 1 }).endOf("year");
}
if (nextStartDate > eventEndOfYearOfFirstStartDate) {
break;
}
let key = nextStartDate.toISODate();
if (typeof DATES[key] === "undefined") {
DATES[key] = [];
}
let hasException = DATES[key].some(obj => {
return (
obj.event.isRecurrenceException() &&
~obj.start === ~nextStartDate
);
});
if (!hasException) {
DATES[key].push({ start: nextStartDate, event: event });
}
}
});
const parseDescription = description => {
let room = DEFAULT_ROOM;
description = sanitizeHtml(description, { allowedTags: ['br'] })
.replace(/\<br\s*\/?\>/g, "\n")
.trim();
let roomMatch = description.match(/^\u{1F4CD}\s*(.+)$/gmu);
if (roomMatch) {
room = roomMatch[roomMatch.length - 1]
.replace(/\u{1F4CD}\s*/u, "")
.trim();
description = description.replace(roomMatch[roomMatch.length - 1], "");
}
let groups = [];
let groupMatch = description.match(/^\u{1F3E2}\s*(.+)$/gmu);
if (groupMatch) {
groupMatch.forEach(m => {
groups.push(m.replace(/\u{1F3E2}\s*/u, "").trim());
description = description.replace(m, "");
});
}
let people = [];
let personMatch = description.match(/^\u{1F642}\s*(.+)$/gmu);
if (personMatch) {
personMatch.forEach(p => {
people.push(p.replace(/\u{1F642}\s*/u, "").trim());
description = description.replace(p, "");
});
}
let subtitle = description.trim();
let subMatch = description.match(/^\u{1F5A5}\s*(.+)$/gmu);
if (subMatch) {
subtitle = subMatch[subMatch.length - 1]
.replace(/\u{1F5A5}\s*/u, "")
.trim();
description = description.replace(subMatch[subMatch.length - 1], "");
}
description = description.trim();
return {
description: description,
subtitle: subtitle,
room: room,
groups: groups,
people: people
};
};
var schedulexml = xmlbuilder.create("schedule");
schedulexml.ele("version", 1);
var conference = schedulexml.ele("conference");
conference.ele("acronym", "TPH");
conference.ele("title", "temporärhaus");
var sortedDates = Object.keys(DATES).sort();
var begin = sortedDates[0];
var end = sortedDates[sortedDates.length - 1];
var beginDT = DateTime.fromISO(begin);
var endDT = DateTime.fromISO(end).endOf("year");
conference.ele("start", beginDT.toISODate());
conference.ele("end", endDT.toISODate());
var diff = endDT.diff(beginDT, ["days"]);
conference.ele("days", Math.ceil(diff.days));
conference.ele("timeslot_duration", "00:15");
var xmlDayIndex = 0;
Object.keys(DATES)
.sort()
.filter(key => {
if (FILTER_START === null) {
return true;
}
return DateTime.fromISO(key) >= FILTER_START;
})
.filter(key => {
return !DATES[key].every(obj => {
return obj.event.summary == "Busy";
});
})
.forEach(function(key) {
var xmlday = schedulexml.ele("day");
xmlday.att("index", ++xmlDayIndex);
xmlday.att("date", key);
let startOfDay = DateTime.fromISO(key).set({ hour: 7 });
xmlday.att("start", startOfDay.toISO());
let endOfDay = DateTime.fromISO(key)
.plus({ day: 1 })
.set({ hour: 3 });
xmlday.att("end", endOfDay.toISO());
var xmlroom = xmlday.ele("room");
xmlroom.att("name", "temporärhaus");
var eventId = 1;
DATES[key]
.sort((a, b) => {
if (a.start < b.start) {
return -1;
}
if (a.start > b.start) {
return 1;
}
return 0;
})
.forEach(obj => {
let realStartDate = obj.start;
let event = obj.event;
// skip hidden events
if (event.summary == "Busy") return;
var xmlevent = xmlroom.ele("event");
xmlevent.att(
"id",
startOfDay.toFormat("LLdd") +
(eventId < 10 ? "0" : "") +
eventId
);
// calculate duration with the real event timestamps (not the repeated one)
let eventStartDate = DateTime.fromJSDate(
event.startDate.toJSDate(),
{
zone: TZ
}
);
let eventEndDate = DateTime.fromJSDate(
event.endDate.toJSDate(),
{
zone: TZ
}
);
let diff = eventEndDate.diff(eventStartDate, [
"hours",
"minutes"
]);
let desc = parseDescription(event.description);
const room = event._firstProp('categories');
if (room && desc.room === DEFAULT_ROOM) {
desc.room = room;
}
if (desc.description.length == 0) {
console.warn(realStartDate.toISO(), event.summary, "Description empty");
} else if (desc.description == desc.subtitle) {
console.warn(realStartDate.toISO(), event.summary, "Subtitle override missing");
}
if (desc.groups.length == 0 && desc.people.length == 0) {
console.warn(realStartDate.toISO(), event.summary, "People missing");
}
xmlevent.ele("date", realStartDate.toISO());
xmlevent.ele(
"start",
realStartDate.toLocaleString(DateTime.TIME_24_SIMPLE)
);
xmlevent.ele("duration", diff.toFormat("hh:mm"));
xmlevent.ele("room", desc.room);
xmlevent.ele("title", event.summary);
xmlevent.ele("subtitle", desc.subtitle);
var xmlpersons = xmlevent.ele("persons");
desc.groups.forEach(group => {
xmlpersons.ele("person", group);
});
desc.people.forEach(person => {
xmlpersons.ele("person", person);
});
eventId++;
});
});
fs.writeFileSync("schedule.xml", schedulexml.end({ pretty: true }), "utf8");