Skip to content

Instantly share code, notes, and snippets.

@WeiChiaChang
Created February 11, 2020 08:49
Show Gist options
  • Save WeiChiaChang/c7ccd987543f5f3438f3772c5f29645b to your computer and use it in GitHub Desktop.
Save WeiChiaChang/c7ccd987543f5f3438f3772c5f29645b to your computer and use it in GitHub Desktop.
Batch multiple images
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