This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgenerate-ics.js
83 lines (75 loc) · 2.4 KB
/
generate-ics.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
// Generates ICS file from `public/calendar.json`
const fs = require('fs')
const data = JSON.parse(fs.readFileSync('public/calendar.json', 'utf8'))
const icalendar = require('icalendar')
const ICalendar = icalendar.iCalendar
function generateICS() {
const ical = new ICalendar()
for (const event of generateEvents()) {
ical.addComponent(event)
}
return ical.toString()
function generateEvents() {
const result = []
for (const event of data) {
const vEvent = new icalendar.VEvent(`${event.id}@thaiprogrammer-calendar`)
let startDate
let endDate
if (event.time != null) {
const total = event.time.length
startDate = new Date(
Date.UTC(
event.start.year,
event.start.month - 1,
event.start.date,
event.time[0].from.hour - 7,
event.time[0].from.minute
)
)
endDate = new Date(
Date.UTC(
event.start.year,
event.start.month - 1,
event.start.date,
event.time[total - 1].to.hour - 7,
event.time[total - 1].to.minute
)
)
vEvent.setDate(startDate, endDate)
} else {
// Full day support hacked from https://github.com/tritech/node-icalendar/pull/43
startDate = new Date(
event.start.year,
event.start.month - 1,
event.start.date
)
endDate = new Date(
event.end.year,
event.end.month - 1,
event.end.date + 1
)
const startProperty = vEvent.addProperty('DTSTART', startDate)
startProperty.setParameter('VALUE', 'DATE')
startProperty.type = 'DATE'
const endProperty = vEvent.addProperty('DTEND', endDate)
endProperty.setParameter('VALUE', 'DATE')
endProperty.type = 'DATE'
}
vEvent.setSummary(event.title)
vEvent.setLocation(event.location.title)
const url = `https://calendar.thaiprogrammer.org/event/${event.id}`
vEvent.addProperty('URL', url)
vEvent.setDescription(
`${event.summary}\n\n${event.description}\n\n${url}`
)
vEvent.addProperty('CATEGORIES', event.categories)
vEvent.addProperty('TRANSP', 'OPAQUE')
result.push(vEvent)
}
return result
}
}
const ics = generateICS()
const path = `public/calendar.ics`
fs.writeFileSync(path, ics)
console.log('* Generated', path)