-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdown.js
More file actions
56 lines (52 loc) · 1.76 KB
/
down.js
File metadata and controls
56 lines (52 loc) · 1.76 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
const axios = require('axios');
const fs = require('fs');
let results = [
{
url: 'https://downloads.scratch.mit.edu/desktop/Scratch%20Setup.exe',
description: 'Windows 下载',
fileName: 'scratch-win.exe',
icon: 'mdi-microsoft-windows'
}, {
url: 'https://downloads.scratch.mit.edu/desktop/Scratch.dmg',
description: 'macOS 下载',
fileName: 'scratch-mac.dmg',
icon: 'mdi-apple'
}
];
if (!(fs.existsSync('output') && fs.statSync('output').isDirectory)) fs.mkdirSync('output');
else fs.readdirSync('output').forEach(fileName => fs.rmSync(`output/${fileName}`, { force: true }));
results.forEach(t => {
axios({
url: t.url,
method: 'get',
responseType: 'stream'
})
.then((response) => {
let byteCount = 0, byteSum = 0, partCount = 0;
let interval = setInterval(() => {
console.log(`${t.description}: ${((byteSum + byteCount) / 1024 / 1024).toFixed(2)} MB downloaded.`);
}, 1000);
response.data.on('data', (chunk) => {
fs.appendFileSync(`output/${t.fileName}.${partCount}`, chunk);
byteCount += chunk.byteLength;
if (byteCount > 16 * 1024 * 1024) {
partCount++;
byteSum += byteCount;
byteCount = 0;
}
});
response.data.on('end', () => {
byteSum += byteCount;
t.download = [];
t.size = byteSum;
t.time = (new Date()).getTime();
for (let i = 0; i <= partCount; i++) {
t.download.push(`${t.fileName}.${i}`);
}
clearInterval(interval);
})
})
})
process.on("exit", () => {
fs.writeFileSync("output/data.json", JSON.stringify(results));
});