-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownload.js
43 lines (41 loc) · 1.39 KB
/
Download.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
// Download media with specific extensions from inputFile
const fs = require('fs')
const https = require('https')
const path = require('path')
const inputFile = 'input.txt'
const urls = fs.readFileSync(inputFile, 'utf-8').split('\n')
urls.forEach((url, index) => {
if (!url || !url.match(/\.(mp4|webm)$/i)) {
// /\.(jpeg|jpg|png)$/i) /\.(mp4|webm)$/i) /\.(gif)$/i)
return console.log(`Skipping URL: ${url}`)
}
setTimeout(() => {
https.get(url, (response) => {
const filename = path.basename(url)
let outputPath = path.join('C:\\Media\\Downloaded', filename)
let count = 1
while (fs.existsSync(outputPath)) {
const extension = path.extname(filename)
const nameWithoutExtension = filename.slice(0, -extension.length)
outputPath = path.join(
'C:\\Media\\Downloaded',
`${nameWithoutExtension}_${count}${extension}`
)
count++
}
const file = fs.createWriteStream(outputPath)
response.pipe(file)
file.on('finish', () => {
const stats = fs.statSync(outputPath)
if (stats.isFile() && stats.size > 0) {
console.log(`Downloaded: ${url}`)
urls.splice(index, 1)
fs.writeFileSync(inputFile, urls.join('\n'))
} else {
console.log(`Error downloading: ${url}`)
fs.unlinkSync(outputPath)
}
})
})
}, index * 1000)
})