Created
June 14, 2012 08:59
-
-
Save ArnaudD/2929175 to your computer and use it in GitHub Desktop.
Poorman parallelization of website rendering with phantomjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| urls = [] | |
| urls.push('www.website1.fr') | |
| # ... | |
| urls.push('www.websiteXXX.fr') | |
| jobs = [] | |
| total_jobs = 8 | |
| # Split jobs | |
| urls.each_index do |i| | |
| current_job = (i / (urls.size / total_jobs)).floor | |
| jobs[current_job] = [] if jobs.size == current_job | |
| jobs[current_job].push urls[i] | |
| end | |
| jobs.each do |domains| | |
| cmd = 'phantomjs render.js '+domains.join('; phantomjs render.js ') | |
| puts cmd | |
| fork do | |
| exec cmd | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Renders on page | |
| */ | |
| var page = require('webpage').create(), | |
| address, output, size; | |
| if (phantom.args.length < 1 || phantom.args.length > 2) { | |
| console.log('Usage: rasterize.js domain'); | |
| phantom.exit(); | |
| } else { | |
| domain = phantom.args[0]; | |
| page.viewportSize = { width: 940, height: 2000 }; | |
| page.open('http://'+domain, function (status) { | |
| if (status !== 'success') { | |
| console.log('Unable to load the address!'); | |
| } else { | |
| window.setTimeout(function () { // let's gmap tiles some time to load | |
| // Retrieve content height to adjust the height of the screenshot | |
| page.clipRect = { | |
| top: 0, left: 0, width: 940, | |
| height: page.evaluate(function () { | |
| return YAHOO.util.Dom.getRegion('content').height + 100; | |
| }) | |
| }; | |
| page.render('/home/arnaud/Bureau/websites/'+domain+'.jpg'); | |
| phantom.exit(); | |
| }, 2000); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment