forked from vibster/hackernewstwo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
78 lines (67 loc) · 1.89 KB
/
renderer.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
var phantom = require('phantom'),
Item = require(__dirname + '/models/item.js');
var render = module.exports.render = function(url, filename, callback) {
phantom.create(function(ph) {
ph.createPage(function(page) {
page.open(url, function (status) {
if (status !== 'success') {
process.send('error - could not load page (' + url + ')');
} else {
page.set('viewportSize', { width: 1024, height: 768 });
page.set('clipRect', {
top: 10, left: 40,
width: 500,
height: 300
});
page.evaluate(function() {
document.body.style.webkitTransform = "scale(0.3)";
document.body.style.webkitTransformOrigin = "0% 0%";
});
setTimeout(function() {
page.render(filename, callback);
ph.exit();
}, 200);
}
});
});
});
}
var queue = [];
function queueItem(url, id) {
if(!id) return;
if(url.indexOf('http://') !== 0) url = 'http://news.ycombinator.com/' + url;
for(var i = 0; i < queue.length; i++) {
if(queue[i].id === id) return;
}
queue.push({url: url, id: id});
};
process.on('message', function(m) {
if(m.event === 'new') {
queueItem(m.body.url, m.body.id);
}
});
setInterval(function() {
Item.find({rendered: false}, function(err, docs) {
if(err) process.send('error - could not read db');
else {
for(var i = 0; i < docs.length; i++) {
queueItem(docs[i].url, docs[i].id);
}
}
});
}, 60 * 1000);
setInterval(function() {
if(queue.length) {
var item = queue.shift();
render(item.url, __dirname + '/public/img/' + item.id + '.png', function() {
process.send('finished render for ' + item.id);
Item.findById(item.id, function(err, doc) {
if(err) process.send('error - could not update db');
else {
doc.rendered = true;
doc.save();
}
});
});
}
}, 3000);