-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (76 loc) · 2.54 KB
/
app.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
91
'use strict';
/**
* Module dependencies
*/
var express = require('express');
var async = require('async');
var ical = require('ical');
var icalGeneratorModule = require('ical-generator');
var config = require('./config/config.js');
var modules = [].concat(
config.modules.common.classes,
config.modules.common.td,
config.modules.common.other,
config.modules.projets,
config.modules.seminaires.insa,
config.modules.seminaires.huma,
config.modules.seminaires.entreprises,
config.modules.seminaires.etrangers,
config.modules.seminaires.facultatifs
);
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.static(__dirname + '/public'));
app.get('/lessons', function(req, res) {
res.json(config.modules);
res.end();
});
app.get('/calendar', function(req, res) {
var icalGenerator = icalGeneratorModule();
icalGenerator.setName(config.name);
icalGenerator.setDomain(config.domain);
icalGenerator.setProdID(config.prodID);
var patterns = [];
async.each(modules, function(module, callback) {
if (req.param(module.urlParam)) {
patterns.push(module.pattern);
}
callback();
}, function() {
ical.fromURL(config.url, {}, function(err, events) {
// Loop on all events and add only lessons in modules
async.each(Object.keys(events), function(k, callback) {
var ev = events[k];
async.each(patterns, function(pattern, cb) {
// Test if the lesson is in modules
if (ev.summary.match(pattern) !== null) {
cb(true);
} else {
cb();
}
}, function(result) {
if (result) {
async.eachSeries(config.replace, function(replace, cb) {
ev.summary = ev.summary.replace(replace.match, replace.replacement);
cb();
}, function() {
icalGenerator.addEvent(ev);
callback();
});
} else {
callback();
}
});
}, function() {
res.send(icalGenerator.toString());
res.end();
});
});
});
});
/**
* Start Server
*/
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});