-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (83 loc) · 2.79 KB
/
Copy pathserver.js
File metadata and controls
98 lines (83 loc) · 2.79 KB
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
'use strict';
var http = require('http'),
https = require('https'),
ical = require('ical');
require('sugar');
function respond(req, res) {
var calData = '',
urlParts = req.url.match(/\/([A-z@\.]+)\/calendar\.(html|ical|js)/),
account = urlParts && urlParts[1],
format = urlParts && urlParts[2];
if (req.method === 'GET' && account) {
https.get(
'https://calendar.google.com/calendar/ical/' + account + '/public/basic.ics?orderby=starttime&sortorder=ascending&futureevents=true&max-results=100',
function(calRes) {
calRes.on('data', function(data) { calData += data; });
calRes.on('end', function() {
switch(format) {
case 'ical':
res.end(calData);
break;
case 'html':
res.end(renderHTML(calData));
break;
case 'js':
res.end(wrapJS(renderHTML(calData)));
break;
}
});
}
).on('error', function(e) {
console.error(e);
});
} else {
res.end('Account name required');
}
}
function renderHTML(rawData) {
var data = ical.parseICS(rawData),
gigs = [],
output = '<table class="gig-table">',
k;
for (k in data) {
if (data.hasOwnProperty(k) && data[k].start && (data[k].start > new Date()) && data[k].summary !== 'Private') {
gigs.push(data[k]);
}
}
gigs.sort(function(a, b) {
return a.start - b.start;
});
gigs.forEach(function (gig) {
var address, addressParts;
if (gig['location']) {
address = gig['location'];
addressParts = address.match(/[^,\d]*, [A-Z]{2}(?=\s*\d{0,5}-?\d{0,4}(,\s(United States|USA|US))?)/);
}
output +=
'<tr class="gig-row-1">' +
'<td class="gig-summary">' + gig.summary + '</td>' +
'<td class="gig-date">' +
'<span class="gig-date-day">' + gig.start.format('{Dow}') + '</span>, ' +
'<span class="gig-date-date">' + gig.start.format('{Mon} {date}, {year}') + '</span>' +
' <span class="gig-date-time">' + gig.start.format('{h}:{mm}{t}') + '</span>' +
'</td>' +
'</tr>' +
'<tr class="gig-row-2">' +
'<td class="gig-address">' + (addressParts ? addressParts[0] : '') + '</td>' +
'<td class="gig-address-link-cell">' +
(address ? '<a class="gig-address-link" href="https://www.google.com/maps/?q=' + address + '">Map</a>' : '') +
'</td>' +
'</tr>';
});
output += '</table>' +
'<style>' +
'.gig-table { width: 100%; } ' +
'.gig-address { padding-bottom: 1em; } ' +
'.gig-address-link-cell { padding-bottom: 1em; } ' +
'</style>';
return output;
}
function wrapJS(html) {
return 'document.write(\'' + html + '\');';
}
http.createServer(respond).listen(process.env.PORT);