This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (77 loc) · 2.65 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
var ical = require('ical'),
fs = require('fs'),
XDate = require('xdate'),
express = require('express'),
_ = require('lodash');
var PORT = 3000;
var schedulePeriods = JSON.parse(fs.readFileSync('periods.json'));
var cyclePeriods = JSON.parse(fs.readFileSync('cycles.json'));
var app = express();
app.get('/:date?', function (req, res) {
// get the argument date or today if absent //
var argdate = (req.params['date'])?
new XDate(req.params['date']):
XDate.today();
// compute start of the week //
var wstart = new XDate(argdate);
wstart.addDays(-wstart.getDay() + 1); // subtract day of week to get Sunday, add one for Monday //
var wend = new XDate(wstart).addDays(5); // add 5 days to start for end of school week //
var iCalURL = 'http://iolani.org/bellSchedule_calendar/ical.ics';
ical.fromURL(iCalURL, {}, function(err, data) {
// filter to just events this week, transform format to something simpler //
var weekEvents = _(data)
.values()
.filter(function (vevent) {
var zdate = new XDate(vevent.start);
return vevent.type == 'VEVENT'
&& zdate >= wstart
&& zdate < wend;
})
.map(function (vevent) {
return {
date: vevent.start,
name: vevent.summary
}
})
.sortBy('date')
.value();
// get the cycle by finding first match //
var cycleNumber = _(weekEvents)
.map(function (ev) {
var matches = /\(Cycle (\d)\)/.exec(ev.name);
return matches? parseInt(matches[1]): null;
})
.compact()
.first();
// get the schedule item for today //
var scheduleName = _(weekEvents)
.filter(function (ev) {
var zdate = new XDate(ev.date);
return zdate.toString('yyyy-MM-dd') == argdate.toString('yyyy-MM-dd')
&& ev.name.indexOf('Schedule') >= 0;
})
.pluck('name')
.map(function (name) {
// remove random stuff in parens after schedule name //
var schedIndex = name.indexOf(' Schedule');
return schedIndex >= 0? name.substring(0, schedIndex): name;
})
.first();
var ret = {
date: argdate.toString('yyyy-MM-dd'),
cycle: cycleNumber,
scheduleName: scheduleName
};
// check for a matching (by name) schedule in the master file //
if(ret.scheduleName) {
ret.periodTimes = schedulePeriods[ret.scheduleName];
}
// check for cycle //
if(ret.cycle) {
ret.cyclePeriods = cyclePeriods[ret.cycle.toString()];
}
res.json(ret);
});
});
app.listen(PORT);
console.log('Started calendar service on HTTP port ' + PORT);