forked from augnustin/google-docs-publisher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (63 loc) · 1.87 KB
/
server.js
File metadata and controls
72 lines (63 loc) · 1.87 KB
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
var express = require('express');
var ejs = require('ejs');
var https = require('https');
var memjs = require('memjs');
var path = require('path');
var app = express();
var mc = memjs.Client.create()
app.set('view engine', 'ejs');
app.set('views', __dirname);
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
var processGoogleHTML = function(html) {
return html
.replace(/(<script[\s\S]+?<\/script>)/gi, '')
.replace(/(<div id="header">[\s\S]+?<\/div>)/, '')
.replace(/(<div id="footer">[\s\S]+?<\/div>)/, '')
};
var generateIframe = function(hash, callback) {
https.get('https://docs.google.com/document/d/'+hash+'/pub', function(crawled) {
var html = '';
crawled.on('data', function(data){
html = html + data.toString();
});
crawled.on('end', function(){
var result = processGoogleHTML(html);
mc.set(hash, result, function(err, val) {
console.log('Wrote', hash);
}, 30);
callback(result);
});
}).end();
}
var getIframeContent = function(hash, useCache, callback) {
if (useCache) {
mc.get(hash, function(err, val) {
if (val) {
console.log('Read', hash);
callback(val.toString())
} else {
generateIframe(hash, callback);
}
})
} else {
generateIframe(hash, callback);
}
}
app.get('/:hash/raw', function(req, res){
getIframeContent(req.params.hash, true, function(html){
res.write(html);
res.end();
});
});
app.get('/:hash', function(req, res){
getIframeContent(req.params.hash, true, function(html){
res.render('googledoc', {
title: (html.match(/<title>(.+)<\/title>/i) || [])[0],
hash: req.params.hash,
});
});
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});