-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
download-images.js
59 lines (45 loc) · 1.39 KB
/
download-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
Download images from a file containing URLS.
The URL file must contain a single URL per line.
*/
const path = require('path')
const fs = require('fs')
const http = require('http')
const https = require('https')
const Stream = require('stream').Transform
const inputFile = path.join(process.cwd(), process.argv[2])
const outputDir = path.join(process.cwd(), process.argv[3])
const basename = path.basename(inputFile)
const imageURLS = fs.readFileSync(inputFile, 'utf8').split('\n').map(url => url.trim()).filter(url => url)
let successCount = 0
let errorCount = 0
function onEnd(err) {
if (err) {
errorCount += 1
} else {
successCount += 1
}
console.log(`${err ? '☠️' : '✅'} ${successCount + errorCount} / ${imageURLS.length}`)
if (successCount + errorCount === imageURLS.length) {
console.log()
console.log(`${errorCount} fail\n${successCount} success`)
process.exit()
}
}
imageURLS.forEach((url, i) => {
const { request } = url.indexOf('http:') === 0 ? http : https
const ext = path.extname(url)
const r = request(url.trim(), (response) => {
const data = new Stream()
response.on('data', (chunk) => {
data.push(chunk);
})
response.on('error', onEnd)
response.on('end', () => {
fs.writeFileSync(path.join(outputDir, `${basename}_${i}${ext}`), data.read())
onEnd()
})
})
r.on('error', onEnd)
r.end()
})