-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsub-download.js
64 lines (61 loc) · 1.78 KB
/
sub-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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const request = require('request');
const progress = require('request-progress');
const fs = require('fs');
process.send = process.send || function(msg) {
console.log(msg)
};
const download = (key, url, filepath, range_start, range_end, throttle) => {
let d_err;
let r = request(url, {
'headers': {
'Range': 'bytes=' + range_start + '-' + range_end
}
});
progress(r, {
'throttle': throttle
})
.on('response', response => {
if (response.statusCode < 200 || response.statusCode > 210) {
r.abort();
d_err = 'http status code: ' + response.statusCode;
process.send({
'key': key,
'err': d_err
});
}
})
.on('progress', function(state) {
process.send({
'key': key,
'progress': state
});
})
.on('error', function(err) {
d_err = err;
process.send({
'key': key,
'err': err
});
})
.on('end', function() {
process.send({
'key': key,
'finish': true,
'err': d_err
});
})
.pipe(fs.createWriteStream(filepath));
}
if (module.parent) {
exports.download = download;
} else {
if (process.argv.length < 8) {
console.error('invalidate process arguments');
} else {
let [_1, _2, key, url, filepath, range_start, range_end, throttle] = process.argv;
range_start = parseInt(range_start);
range_end = parseInt(range_end);
throttle = parseInt(throttle);
download(key, url, filepath, range_start, range_end, throttle);
}
}