-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathnewevents.js
executable file
·47 lines (41 loc) · 1.07 KB
/
newevents.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
var ical = require('ical');
ical.fromURL('https://www.google.com/calendar/ical/mengwong%40hackerspace.sg/public/basic.ics', {},
function(err, data) {
var futureEvents = [];
for (var k in data) {
if (data.hasOwnProperty(k)) {
var ev = data[k]
if (ev.start && ! isNaN(ev.start) && (ev.start > Date.now() - 60 * 60 * 1000)) {
var e = {
"Name": ev.summary,
"When": ev.start.toISOString()
}
if (ev.url && ev.url.indexOf("http") == 0) {
e["URL"] = ev.url;
}
else if (ev.description && ev.description.indexOf("http") >=0) {
e["URL"] = ev.description.match(/https?:\/\/[^\s"]+/)[0];
}
futureEvents.push(e);
}
}
}
if (!futureEvents) {
return;
}
futureEvents.sort(function(a, b) {
var keyA = new Date(a.When),
keyB = new Date(b.When);
// Compare the 2 dates
if (keyA < keyB) return - 1;
if (keyA > keyB) return 1;
return 0;
});
var j = JSON.stringify(futureEvents, undefined, 2);
var fs = require('fs');
fs.writeFile("contents/_index/events.json", j, function(err) {
if (err) {
console.log(err);
}
});
});