Created
February 11, 2020 08:49
-
-
Save WeiChiaChang/c7ccd987543f5f3438f3772c5f29645b to your computer and use it in GitHub Desktop.
Batch multiple images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let imageLinks = [] | |
const async = require('async') | |
let fs = require('fs') | |
let request = require('request') | |
let path = require('path') | |
const downloadImage = (src, dest, callback) => { | |
request.head(src, (err, res, body) => { | |
if (err) { console.log(err); return } | |
src && request(src).pipe(fs.createWriteStream(dest)).on('close', () => { | |
callback && callback(null, dest) | |
}) | |
}) | |
} | |
const getSuffix = str => str.slice(str.lastIndexOf('.')) | |
async.mapSeries(imageLinks, function (item, callback) { | |
setTimeout(function () { | |
var destImage = `${item.obs_code}.png` | |
downloadImage( | |
item, | |
destImage, | |
(err, data) => { | |
err ? console.log(err) : console.log(path.resolve(data)) | |
}) | |
callback && callback(null, item) | |
}, 100) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment