-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
108 lines (94 loc) · 3.24 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
var express = require('express'),
ejs = require('ejs'),
fs = require('fs');
var port = Number(process.env.PORT || 5000),
app = express();
app.use('/assets', express.static(__dirname + '/assets'));
app.engine('html', ejs.renderFile);
app.get('/', function (req, res) {
res.render('chart.html');
});
app.get('/chart', function (req, res) {
res.render('chart.html');
});
app.get('/form', function(req, res) {
res.render('form.html');
});
app.get('/datasheet', function (req, res) {
var cacheFile = './cache.json';
if(!fs.existsSync(cacheFile)){
res.write(JSON.stringify({}));
res.end();
} else {
cachedData = JSON.parse(fs.readFileSync(cacheFile, 'utf-8'));
cacheModified = cachedData.lastModified;
res.write(JSON.stringify(cachedData.data));
res.end();
}
});
app.get('/update_datasheet', function (req, res) {
var spreadsheetKey = '0Ai29bxvOdvgjdDZuOEk0eWRGRUQ4ZmEwNnFEb3owVEE';
var spreadsheetURL = 'http://spreadsheets.google.com/feeds/list/'+spreadsheetKey+'/od6/public/values?alt=json';
var cacheFile = './cache.json';
var http = require('http');
// Get info about cache
var cachedData = false;
var cacheModified = 0;
var lastModified = Infinity; //getLastModified(spreadsheetKey);
if(!fs.existsSync(cacheFile)){
fs.writeFileSync(cacheFile, JSON.stringify({'lastModified' : 0, 'data':[]}));
} else {
cachedData = require(cacheFile);
cacheModified = cachedData.lastModified;
}
var options = {
method: 'HEAD',
host: 'spreadsheets.google.com',
port: 80,
path: '/feeds/list/'+spreadsheetKey+'/od6/public/values?alt=json'
};
var modReq = http.request(options, function(modRes) {
var mod = new Date(modRes.headers['last-modified']);
lastModified = mod.getTime() / 1000;
console.log('lastModified: ' + lastModified + ' cacheModified: ' + cacheModified);
if(cacheModified >= lastModified) {
console.log('Cache hit');
res.write(JSON.stringify({data:cachedData.data,new:false}));
res.end();
} else {
console.log('Cache miss');
var sheetReq = http.get(spreadsheetURL,function(sheetResult) {
var rawResp = '';
var writeDat = {'lastModified':lastModified,'data':''};
sheetResult.on('data',function(d){
rawResp += d;
});
sheetResult.on('end', function () {
// Format result
var links = [];
rawResp = JSON.parse(rawResp);
var allRows = rawResp.feed.entry;
for (var i = allRows.length - 1; i >= 0; i--) {
links[i] = {};
links[i].member = allRows[i].gsx$member.$t;
links[i].memberband = allRows[i].gsx$memberband.$t;
links[i].relationship = allRows[i].gsx$relationship.$t;
};
writeDat.data = links;
// Write result to browser
res.write(JSON.stringify({data:links,new:true}));
// Write result to file
fs.writeFile(cacheFile, JSON.stringify(writeDat), function (err) {
if (err) throw err;
console.log(new Date().toLocaleString() + ': Wrote ' + links.length +' rows');
});
res.end();
});
});
}
});
modReq.end();
});
app.listen(port, function () {
console.log('listening to on port %s', port);
});