img-crawler

A module to download images from a given URL

img-crawler

A Node module for downloading images to disk from a given URL.

Eventually you will be able to install this from npm. Until then you can use npm to install the dependencies by running this from the root of the project directory:

    npm install

From the module directory run:

    npm test

Without npm:

    make test

Download imgs from 'pearljam.com' and write them to the 'pj-imgs' directory. The dir will be created if not found and resolved to an absolute path.


     var crawler = require('img-crawler');
 var opts = {
     url: 'http://pearljam.com',
     dist: 'pj-imgs'
 };
 
 crawler.crawl(opts, function(err, data) {
     console.log('Downloaded %d from %s', data.imgs.length, opts.url);
 });    
</code>

Keeping inline with node convention the callback first accepts an error object followed by data representing the downloaded images. The err object will be provided if loading the web page fails. Failures are reported in the img responses.

Here's an example of a response:

    {
        imgs: [
            {
                src: 'img/a-img.png', 
                statusCode: 200,
                success: true,
                path: '/Users/radvieira/my-imgs/img/a-img.png'
            },
            {
                src: 'img/another-img.png', 
                statusCode: 404,
                success: false
            }            
        ]
    }
    

In this case the first image was downloaded and written to disk while the other failed. Notice how there is no path attribute for the failed download.