This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
/
index.js
126 lines (98 loc) · 3.04 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
var url = require('url');
var express = require('express');
var webshot = require('webshot');
var pkgcloud = require('pkgcloud');
/**
* CDN URL will get defined later in the process after asking CloudFiles
*/
var cdnUrl = '';
/**
* Base URL is the URL to render screenshots from
*/
var baseURL = process.env.BASE_URL || 'http://nbviewer.ipython.org';
var zoomFactor = process.env.ZOOM_FACTOR || 0.5;
var renderDelay = process.env.RENDER_DELAY || 100;
var defaultWidth = twitterCardWidth = 280;
var defaultHeight = twitterCardHeight = 150;
var client = pkgcloud.storage.createClient({
provider: 'rackspace',
username: process.env.OS_USERNAME,
apiKey: process.env.OS_PASSWORD,
region: process.env.OS_REGION_NAME
});
var containerName = process.env.CONTAINER;
function detectInteresting() {
$('#menubar').remove();
// YOLO, get the last image
img = $('img').last();
el = img[0];
// TODO: Actually get the screenshot setup where this image is.
//
// Scrolling does nothing since we have to set shot and screen size prior,
// and the "screen" is full size (Phantom pretty much ignores this).
el.scrollIntoView()
}
function screenshot(req, res, next) {
var width = req.params.width || defaultWidth;
var height = req.params.height || defaultHeight;
var captureURI = req.params.captureURI;
var captureURL = url.resolve(baseURL, captureURI);
var CDNPath = width + "/" + height + "/" + captureURI + ".png";
res.send({url: url.resolve(req.cdnUrl, CDNPath)});
console.log("Rendering " + captureURL);
console.log("At " + width + "x" + height);
var options = {
screenSize: {
width: width
, height: height
}
, shotSize: {
width: width
, height: height
}
, zoomFactor: zoomFactor
, renderDelay: renderDelay
, onLoadFinished: detectInteresting
}
webshot(captureURL, options, function(err, renderStream) {
var writeStream = client.upload({
container: containerName,
remote: decodeURI(CDNPath)
});
writeStream.on('error', function(err) {
console.log(err);
});
writeStream.on('success', function(file) {
console.log('Finished ' + CDNPath);
});
renderStream.pipe(writeStream);
});
}
var app = express();
app.use(function(req, res, next) {
// If we already have the url, just put it in the request, and then continue
if (cdnUrl) {
req.cdnUrl = cdnUrl;
next();
return;
}
// Note: there will be latency on the first request
client.getContainer(containerName, function(err, container) {
if (err) { next(err); return; }
req.cdnUrl = cdnUrl = container.cdnSslUri;
next();
return;
});
});
app.get('/api/screenshots/:width/:height/:captureURI(*)', screenshot);
app.get('/', function(req, res) {
res.json({
"screenshotsURL": req.headers.host + "/api/screenshots/{width}/{height}/{captureURI}",
"baseURL": baseURL
});
});
var server = app.listen(8181, function() {
var host = server.address().address
var port = server.address().port
console.log('nbshot listening at http://%s:%s', host, port)
});