-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
75 lines (67 loc) · 1.57 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
var fs = require("fs");
var path = require("path");
var ical2json = require("ical2json");
var _ = require("lodash");
var icsDir = "ics";
var eventJson = "data/events.json";
function wholePath(file) {
return path.join(icsDir, file);
}
function readUtf8(path) {
return fs.readFileSync(path, { encoding: 'UTF8' });
}
function cal2vcal(cal) {
return cal.VCALENDAR;
}
function cal2tz(cal) {
return cal.VTIMEZONE || [];
}
function tz2event(tz) {
if (tz.VTIMEZONE) {
tz = tz.VTIMEZONE[0];
}
return tz.VEVENT;
}
var events = _.chain(fs.readdirSync(icsDir))
.map(wholePath)
.map(readUtf8)
.map(ical2json.convert)
.map(cal2vcal)
.flatten()
.map(cal2tz)
.flatten()
.map(tz2event)
.flatten()
.sortBy(function(event) {
return toDate(startTime(event));
})
.value();
function startTime(event) {
for (var prop in event) {
if (event.hasOwnProperty(prop)) {
if (prop.indexOf("DTSTART") === 0) {
return event[prop];
}
}
}
}
function toDate(time) {
var year = time.substr(0, 4);
var month = time.substr(4, 2) - 1;
var day = time.substr(6, 2);
var hour = time.substr(9, 2);
var min = time.substr(11, 2);
var sec = time.substr(13, 2);
return new Date(year, month, day, hour, min, sec);
}
function fixText(text) {
return text.replace(/\\n/g, "\n").replace(/\\,/g, ",");
}
function fixEvent(event) {
event.SUMMARY = fixText(event.SUMMARY);
event.DESCRIPTION = fixText(event.DESCRIPTION);
event.startDate = toDate(startTime(event)).toString();
return event;
}
events = events.map(fixEvent);
fs.writeFileSync(eventJson, JSON.stringify(events, null, 4), { encoding: 'UTF8' });