Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (57 loc) · 2.32 KB
/
Copy pathapp.js
File metadata and controls
64 lines (57 loc) · 2.32 KB
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
60
61
62
63
64
import Downloader from "nodejs-file-downloader";
const range = (max) => Array.from({ length: max }, (_, i) => 1 + i);
const readableBytes = (bytes) => {
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let i = 0;
while (bytes >= 1024) { bytes /= 1024; i++; }
return bytes.toFixed(1) + ' ' + units[i];
}
const isPromise = (obj) => { return (obj && typeof obj.then === 'function');}
const zeroFilledNumber = (arrayLength, i) => {
return arrayLength < 100 ? String(i).padStart(2, '0') : arrayLength < 1000 ? String(i).padStart(3, '0') : String(i).padStart(4, '0');
}
const fileScrap = (proxy, coreLink, filePrefix, fileSuffix, max, fileType, path, clone, savePrefix) => {
const maxNum = range(max);
const maxNumLength = maxNum.length;
const cloneFile = (clone === 'yes') ? true : (clone === 'no') ? false : false;
const filePath = path ? './downloads/' + path : './downloads';
const download = async (i) => {
let fileName = filePrefix + zeroFilledNumber(maxNumLength, i) + fileSuffix + fileType;
let fileLink = coreLink + fileName;
let fileSave = savePrefix + fileName;
let file = new Downloader({
url: fileLink,
directory: filePath,
proxy: proxy,
cloneFiles: cloneFile,
onProgress: (percentage, chunk, remainingSize) => {
console.clear();
console.log(`Downloading ${fileSave} to ${filePath} - %`, percentage + ' / Remaining: ' + readableBytes(remainingSize));
}
});
try {
await file.download().then(r => {
console.log(`${fileName} downloaded to ${fileSave}`);
}).catch(e => {
console.log(e);
});
} catch (error) {
console.log("Download failed", error);
}
};
const runSerial = () => {
if (maxNumLength < 15) {
maxNum.forEach(i => {
download(i).then(r => {console.log(r);});
});
} else {
let result = Promise.resolve();
maxNum.forEach(task => {
result = result.then(() => download(task))
});
return result;
}
}
isPromise(runSerial()) ? runSerial().then(r => {console.log(r);}) : runSerial();
};
export default fileScrap;