-
Notifications
You must be signed in to change notification settings - Fork 57
/
downloader.js
114 lines (101 loc) · 3.01 KB
/
downloader.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require("fs");
const http = require("http");
const https = require("https");
const progress = require("progress-stream");
const { requestWeb } = require("./request");
const REGEX_PLAY_INFO = /<script>window\.__playinfo__=(.*?)<\/script>/;
const REGEX_INITIAL_STATE = /__INITIAL_STATE__=(.*?);\(function\(\)/;
class Task {
constructor(url) {
this.url = url;
this.finished = false;
}
}
class Downloader {
constructor() {
this.aid = -1;
this.bvid = -1;
this.pid = 1;
this.cid = -1;
this.playUrl = null;
this.videoData = null;
this.name = "";
this.links = [];
this.tasks = [];
}
getVideoInfoFromInitialState(state) {
this.aid = state.aid;
this.bvid = state.bvid;
this.pid = state.p;
this.cid = state.videoData.cid;
this.videoData = state.videoData;
}
async getPlayUrlWebPage(url) {
const referer = 'https://www.bilibili.com';
const response = await requestWeb(url, referer);
const matchInitialState = response.match(REGEX_INITIAL_STATE);
const matchPlayInfo = response.match(REGEX_PLAY_INFO);
if (!matchInitialState || !matchPlayInfo) return;
const initialState = JSON.parse(matchInitialState[1]);
const playUrl = JSON.parse(matchPlayInfo[1]);
this.getVideoInfoFromInitialState(initialState);
if (!playUrl) {
this.playUrl = null;
} else if (playUrl.data) {
this.playUrl = playUrl.data;
} else if (playUrl.result) {
this.playUrl = playUrl.result;
}
}
downloadByIndex(part, file, callback = () => {}) {
const { url } = this;
if (this.tasks.some(item => item.url === this.links[part])) return "DUPLICATE";
this.tasks.push(new Task(this.links[part]));
let state;
try {
state = fs.statSync(file);
}
catch (error) {
}
const options = {
url: this.links[part],
headers: {
"Range": `bytes=${state ? state.size : 0}-`, //断点续传
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"Referer": url
}
};
const stream = fs.createWriteStream(file, state ? { flags: "a" } : {});
this.download(options, stream, callback);
return state;
}
download(options, stream, callback) {
// https://www.npmjs.com/package/progress-stream
const index = this.tasks.findIndex(item => item.url === options.url);
const proStream = progress({
time: 250 //单位ms
}).on("progress", progress => {
const { percentage } = progress; //显示进度条
if (percentage === 100) {
this.tasks[index].finished = true;
}
callback(progress, index);
});
let { url } = options;
function downloadLink(url) {
(url.startsWith("https") ? https : http).get(url, options, res => {
if (res.statusCode === 302) {
url = res.headers.location;
return downloadLink(url);
}
proStream.setLength(res.headers["content-length"]);
//先pipe到proStream再pipe到文件的写入流中
res.pipe(proStream).pipe(stream).on("error", error => {
console.error(error);
});
});
}
downloadLink(url);
}
}
module.exports = { Task, Downloader };