-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·115 lines (97 loc) · 2.6 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//express
var express = require('express'),
app = express(),
oneDay = 86400000,
halfHour = 1800000,
tabletop = require('tabletop');
//GOOGLE SPREADSHEETS WHIT THE FACT CHECK OF EACH TOWN
var spreadsheets = {
jalisco: '1ZezQRrxGSrSOSfeFuyyI-8wEga85BuKM8uPm2cnd1X0',
guadalajara : '19F4xqdTfV1Xse0DF0g3lLFrLFkV43agtCJNhBAu9HgM',
zapopan : '1CMTFGw3eTFDtoEaz8QAH4u9y5H3wKaUMt29K5YAoZhY',
ecatepec : '1DbdmXzv1udHddfCkSC_FdF9OihNL84no4hpUaiX9F3c',
naucalpan : '1pmaUoAOn_j2EuIEZKi10PZ5_m00q5jB6WrX0TkP38n4',
toluca : '1-JZg1hX8OIe9cDB6hHKdsTClUBMZhyC16iw0QFsg5Cs'
};
// Check all spreadsheets, get the data and then send it to the view manager
function checkAllData(sheets, call){
var keys = Object.keys(sheets);
var x = 0;
var actualkey = keys[x];
//Check every single spreadsheet and get the data before rendering de website
checkSpreadSheets(sheets)
function checkSpreadSheets(sheets) {
console.log('obteniendo datos de: ' + actualkey);
//executes getdata
getData(sheets[actualkey], function(){
x++;
actualkey = keys[x];
if(x < keys.length) {
checkSpreadSheets(sheets);
}
else {
call(sheets);
}
})
}//end check each spreadsheet
//GETDATA function (tabletop async request)
function getData(municipio, callingback){
tabletop.init( {
key: municipio,
simpleSheet: true,
callback: data => {
console.log('Datos conseguidos con éxito');
sheets[actualkey] = data;
callingback();
}
});
}//end getData
}// check all data end
//CHECKS FOR NEW INFO EVERY DAY
checkAllData(spreadsheets, setViews);
setInterval(() => {
checkAllData(spreadsheets, setViews)
}, halfHour);
//SET VIEWS
function setViews(data){
app.use('/src', express.static(__dirname + '/src', { maxAge: oneDay }));
app.set('view engine', 'pug');
app.get('/', function (req, res) {
res.render('index', {
data: data,
header: "inicio"
})
});
app.get('/acerca', function (req, res) {
res.render('acerca');
});
// dynamic routing
app.get('/:municipio', function (req, res) {
var municipio = req.params.municipio;
if(!data[municipio]){
res.send('404: Not found')
}
if(municipio == 'jalisco'){
res.render('ejes_header', {
data: data[municipio],
header: municipio
})
}
else{
res.render('results_template', {
data: data[municipio],
header: municipio
});
}
});
app.get('/api/:municipio', function (req, res) {
var municipio = req.params.municipio;
if(!data[municipio]){
res.send('404: Not found')
}
res.send(data[municipio]);
});
app.listen(process.env.PORT || 5000, function () {
console.log('#informebajolupa en funcionamiento');
});
}