-
Notifications
You must be signed in to change notification settings - Fork 5
/
build-agenda.js
92 lines (73 loc) · 2.47 KB
/
build-agenda.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
var csv = require('csv');
var _ = require('./bower_components/underscore/underscore-min.js');
/**
* For local use:
* % ✗ python -m SimpleHTTPServer
* var http = require('http');
* var csv_url = 'http://localhost:8000/agenda.csv';
* use http.get
*
*/
var https = require('https');
var csv_url = 'https://docs.google.com/spreadsheets/export?id=15Dtd8_Y14no-0KSxh8zNwiNnmo3LIJZyiS-je7liCTM&exportFormat=csv';
var language = process.argv[2] || "es";
https.get(csv_url, function(res) {
var response = "";
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
response = response.toString();
csv.parse(response, function(err, output) {
var events = csvArrayToJSON(output);
var html = buildScheduleHtml(events);
console.log(html);
});
});
}).on('error', function(e) {
console.error(e);
});
function csvArrayToJSON(csvArray) {
var headers = _.head(csvArray);
var rows = _.rest(csvArray);
var rowObjects = _.map(rows, function(value, key, list) {
return _.object(headers, value);
});
return rowObjects;
}
function buildScheduleHtml(events) {
var html = "";
var eventsByHour = _.groupBy(events, 'Hora Inicio');
_.each(eventsByHour, function(value, key) {
html += buildScheduleRow(eventsByHour[key], key);
});
return html;
}
function buildScheduleRow(eventsInAnHour, hour) {
var html = "<td>" + hour + "</td>";
html += _.reduce(_.where(eventsInAnHour, { 'Día': '1' }), function(memo, event) {
return memo + buildEventLink(event);
}, "<td>");
html += "</td>";
html += _.reduce(_.where(eventsInAnHour, { 'Día': '2' }), function(memo, event) {
return memo + buildEventLink(event);
}, "<td>");
html += "</td>";
return "<tr>" + html + "</tr>";
}
function buildEventLink(event) {
var trackClassMap = {
'Actores, políticas y principios': 'politicas',
'Innovación y uso': 'innovacion',
'Plataformas y herramientas': 'herramientas',
'Plenarias': 'Plenarias',
'Otros': 'otros'
};
event['trackClass'] = trackClassMap[event['Track']] || 'condatos';
event['title'] = language == 'en' ? event['Panel inglés'] : event['Panel español'];
var klass = "btn-<%= e['trackClass'] %> btn btn-default btn-s";
var href = "../agenda/" + language + "-<%= e.Día %>-<%= e['Hora Inicio'] %>-<%= e.trackClass %>.html";
var template = "<a class='" + klass + "' href='" + href + "'><%= e.title %></a>";
var compiled = _.template(template);
return compiled({ e: event });
}