Actor picture

Icon Burglar

yuri/icon-burglar

This act takes the URLs prepared by a crawler and downloads the images to finally zip them

No credit card required

Author's avatarYuri Manders
  • Modified
  • Users1
  • Runs67

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

// Get all the libraries this crawler uses
const Apify = require('apify');
const _ = require('underscore');
const rp = require('request-promise');
const Promise = require('bluebird');

Apify.main(async () => {
    // Get act input and validate it
    const input = await Apify.getValue('INPUT');
    console.log('Input:')
    console.dir(input);
    if (!input || !input._id) {
        throw new Error('Input is missing the "_id" attribute. Did you start it from crawler finish webhook?');
    }
    const executionId = input._id;
    
    // Print info about crawler run
    const crawlerRunDetails = await Apify.client.crawlers.getExecutionDetails({ executionId });
    if (!crawlerRunDetails) {
        throw new Error(`There is no crawler run with ID: "${executionId}"`);
    }
    console.log(`Details of the crawler run (ID: ${executionId}):`);
    console.dir(crawlerRunDetails);
    
    // Iterate through all crawler results and count them
    // Here is the place where you can add something more adventurous :)
    console.log(`Counting results from crawler run...`);
    
    const limit = 100;
    let offset = 0;
    let totalItems = 0;
    let results;

    results = await Apify.client.crawlers.getExecutionResults({ 
        executionId,
        limit,
        offset
    });
    
    // Prepare each result for downloading
    await Promise.each(results.items[0].pageFunctionResult, async function(value, i){
           
           console.log('url', value.url)
           
           // Move each result in the key-value store
           const file = await rp ({
               url : value.url, encoding : null
           });
           
           // Attach the right filename and encoding
           await Apify.setValue(value.file+'.png', file, { contentType : 'image/png'})
    });
    
    // Take the key-value store and zip data (this uses a different Actor (https://www.apify.com/jaroslavhejlek/zip-key-value-store))
    const run = await Apify.call('jaroslavhejlek/zip-key-value-store', { "keyValueStoreId": process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID, "filesPerZipFile": 2000 });
    console.dir(run);
    
});