STDIO Training

45 bundles
2 files6 months ago
3

Download file and Zip

Download file and Zip

Without await

12345678910111213141516171819202122232425262728293031323334353637383940import * as fs from "fs";
import * as https from "https";
import * as url from "url";
import * as path from "path";
import { zip } from "zip-a-folder";

let imagePaths = [
  "https://laurierleweb.com/wp-content/uploads/2018/04/bo-anh-nen-thien-nhien.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-anh-thien-nhien-dep.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-anh-thien-nhien-dep-2018.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-nen-dekstop.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-nen-wallpaper-thien-nhien.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-thien-nhien-dep.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/kho-hinh-anh-nen.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/nen-thien-nhien-dep.jpg"
];

var download = function(url, dest, cb) {
  var file = fs.createWriteStream(dest);
  var request = https.get(url, function(response) {
    response.pipe(file);
    file.on("finish", function() {
      file.close(cb);
    });
  });
};

var getFileNameFromPath = function(fileUrl) {
  var parsed = url.parse(fileUrl);
  return path.basename(parsed.pathname);
};

console.log(imagePaths.length + " images");
for (let imagePath of imagePaths) {
  let fileName = getFileNameFromPath(imagePath);
  console.log("Downloading: " + fileName);
  download(imagePath, "./images/" + fileName);
}

zip("./images", "./images.zip");

Async/Await

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import * as fs from "fs";
import * as https from "https";
import * as url from "url";
import * as path from "path";
import { zip } from "zip-a-folder";

let imagePaths = [
  "https://laurierleweb.com/wp-content/uploads/2018/04/bo-anh-nen-thien-nhien.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-anh-thien-nhien-dep.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-anh-thien-nhien-dep-2018.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-nen-dekstop.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-nen-wallpaper-thien-nhien.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/hinh-thien-nhien-dep.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/kho-hinh-anh-nen.jpg",
  "https://laurierleweb.com/wp-content/uploads/2018/04/nen-thien-nhien-dep.jpg"
];

var download = function(url, dest) {
  return new Promise((res, rej) => {
    var file = fs.createWriteStream(dest);
    var request = https.get(url, function(response) {
      response.pipe(file);
      file.on("finish", function() {
        file.close(() => {
          res();
        });
      });
    });
  });
};

var getFileNameFromPath = function(fileUrl) {
  var parsed = url.parse(fileUrl);
  return path.basename(parsed.pathname);
};

async function main() {
  console.log(imagePaths.length + " images");

  let promises = [];

  for (let imagePath of imagePaths) {
    let fileName = getFileNameFromPath(imagePath);
    console.log("Downloading: " + fileName);
    let promise = download(imagePath, "./images/" + fileName);
    promises.push(promise);
  }

  await Promise.all(promises);

  await zip("./images", "./images.zip");

  console.log("DONE");
}

main();