{"size": 1682, "ext": "js", "max_stars_repo_path": "scripts/takeScreenshots.js", "max_stars_repo_name": "RhoInc/viz-library", "max_stars_repo_stars_event_min_datetime": "2017-06-05T13:26:27.000Z", "max_stars_repo_stars_event_max_datetime": "2018-09-25T20:38:44.000Z", "max_issues_repo_path": "scripts/takeScreenshots.js", "max_issues_repo_name": "RhoInc/viz-library", "max_issues_repo_issues_event_min_datetime": "2017-03-06T16:50:54.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-22T04:21:55.000Z", "max_forks_repo_path": "scripts/takeScreenshots.js", "max_forks_repo_name": "RhoInc/viz-library", "max_forks_repo_forks_event_min_datetime": "2017-12-07T21:42:41.000Z", "max_forks_repo_forks_event_max_datetime": "2018-12-10T02:19:50.000Z"}
{"max_stars_count": 5, "max_issues_count": 58, "max_forks_count": 3, "avg_line_length": 30.5818181818, "max_line_length": 165, "alphanum_fraction": 0.6248513674}
const captureWebsite = require("capture-website");
const fs = require("fs");
const options = {
  width: 1920,
  height: 1080,
  overwrite: true,
  type: "jpeg",
  quality: 0.1,
  delay: 3
};

const overwrite = process.argv[2] == "-o";
console.log("Overwriting? " + overwrite);

async function makeExampleList() {
  const examples = await require("../data/examples.json")
    .filter(repo => repo.examples && repo.examples.length) // keep only those repos with one or more examples
    .map(repo => {
      return repo.examples.map((example, i) => {
        return {
          url: example.example_url,
          filename: `${repo.name}-${i}`
        };
      }); // return's each example's URL and screenshot filename
    }) // returns each repo's example list
    .reduce((acc, val) => acc.concat(val), []) // flattens repo array to one item per example
    .filter(function(example) {
      //overwrite exsiting screenshots if -o flag is set
      var path = `./img/${example.filename}.jpg`;
      if (fs.existsSync(path)) {
        console.log(path + " exists");
        return overwrite ? true : false;
      } else {
        console.log(path + " doesn't exist");
        return true;
      }
    });
  return examples;
}

async function takeScreenshots() {
  const examples = await makeExampleList();

  for (const example of examples) {
    const filename = `./img/${example.filename}.${options.type.replace(
      "peg",
      "pg"
    )}`;
    await captureWebsite.file(example.url, filename, options); // gotta await each call to captureWebsite.file(); Promise.all() throws some weird Node process errors
    console.log(`Captured ${filename}!`);
  }
}

takeScreenshots();
