SHARE
TWEET

Untitled

a guest Jun 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const puppeteer = require('puppeteer');
  2. const fs        = require('fs');
  3. const fs_extra  = require('fs-extra');
  4. const loader    = require('csv-load-sync');
  5.  
  6. getScreenshot();
  7.  
  8. async function getScreenshot() {
  9.     const maxNumberOfSessions = 1;
  10.     let counter = 0;
  11.  
  12.     await Promise.all(Array.from({length: maxNumberOfSessions}, processArray));
  13.     console.log("All records have been processed.");
  14.  
  15.  
  16.  
  17.  
  18.     async function processArray() {
  19.         var csv = loader('file.csv');
  20.         for (const item of csv) {
  21.             nextRecord = item;
  22.         }
  23.  
  24.         if (nextRecord) {
  25.             return shoot(nextRecord.URL, nextRecord.MasterID).then(processArray);
  26.         }
  27.  
  28.  
  29.         console.log('Done!');
  30.     }
  31.  
  32.     async function shoot(url, name) {
  33.         const number = ++counter;
  34.         console.log("Processing record #" + number + ": " + url);
  35.  
  36.         const browser = await puppeteer.launch({defaultViewport: {width: 1024, height: 768}});
  37.         const page = await browser.newPage();
  38.         await page.goto(url).catch(function(err) {
  39.             // Do something with you error
  40.             console.log(`See error: ${err.message}`);
  41.         });
  42.  
  43.         await page.screenshot({path: `./${name}.png`});
  44.  
  45.         await browser.close();
  46.     }
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top