Skip to content

Instantly share code, notes, and snippets.

@tracend
Last active December 18, 2015 05:49
Show Gist options
  • Save tracend/5735129 to your computer and use it in GitHub Desktop.
Save tracend/5735129 to your computer and use it in GitHub Desktop.
Thumbnail Node.js mini app
node_modules

Thumbnail app

  1. Take the screenshot
  2. Upload it to an S3 bucket

Install

npm install s3
npm install webshot

Usage

node screen.js http://url/to/my/page

Credits

Created by Makis Tracend ( @tracend )

// configure
module.exports = {
aws: {
key: "0000_S3_KEY_0000",
secret: "0000_S3_SECRET_0000",
bucket: "S3_BUCKET"
},
screenSize: { width: 1024, height: 768 },
delay: 5000,
path: '/', // this is the path in the S3 bucket where the image will be saved
filename: false
}
{
"name": "thumbnail-app",
"version": "0.1.0",
"author": "tracend",
"contributors": [
{
"name": "Makis Tracend",
"email": "[email protected]"
}
],
"homepage": "http://makesites.org",
"repository": {
"type": "git",
"url": "git://gist.github.com/tracend/5735129.git"
},
"bugs": {
"url": "https://gist.github.com/tracend/5735129"
},
"licenses": [
{
"type": "MIT",
"url": "http://makesites.org/licenses/MIT"
}
],
"engines": {
"node": ">=0.8.x"
},
"main": "screen",
"scripts": {
"start": "screen.js"
},
"dependencies": {
"s3" : "*",
"webshot" : "*"
}
}
// dependencies
var fs = require('fs'),
s3 = require('s3'),
webshot = require('webshot'),
config = require('./config');
// createClient allows any options that knox does.
var client = s3.createClient( config.aws );
// pass the URL as a command line argument
var url = process.argv[2];
var file = ( config.filename ) ? config.filename : url.substring(url.lastIndexOf('/')+1)+'.png';
var options = {
renderDelay: config.delay || 0,
windowSize: config.screenSize,
shotSize : { width: 'window', height: 'window' }
}
webshot( url, file, options, function(err) {
// error check?
// upload a file to s3
var uploader = client.upload(file, config.path+file);
uploader.on('error', function(err) {
console.error("unable to upload:", err.stack);
});
uploader.on('end', function(url) {
// delete local file
fs.unlink( file );
console.log("file available at", url);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment