-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
164 lines (153 loc) · 4.6 KB
/
api.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Name: bilibili视频下载器
* Author: machunning
* Date: 2019-10-20
*/
'use strict'
const https = require('https');
const http = require('http');
const fs = require('fs');
const defaultHeaders ={
UserAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
}
let aid = '80758461';
let download_options = {
headers:{
'User-Agent': defaultHeaders.UserAgent,
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Range': 'bytes=0-',
'Referer': 'https://www.bilibili.com/video/av' + aid,
'Origin': 'https://www.bilibili.com',
'Connection': 'keep-alive'
}
};
// 请求头需要携带信息
let headers = {
'User-Agent': defaultHeaders.UserAgent,
'Cookie':'SESSDATA=19a5290e%2C1574498919%2C1656cfa1',
'Host':'api.bilibili.com'
};
function get_cid(aid){
// cid 获取接口
const b_cid_URL = 'https://api.bilibili.com/x/web-interface/view';
https.get(b_cid_URL + '?aid=' + aid , (res) => {
res.setEncoding('utf-8');
let finaldata = '';
res.on('data', (chunk) => {
finaldata += chunk;
});
res.on('end',()=>{
finaldata = JSON.parse(finaldata).data;
fs.readFile('./json/videoMessage.json', function(err, data){
if (err) {
console.log('读文件出错\n', err);
}
let oldvalue = JSON.parse(data.toString());
oldvalue = {
aid: finaldata.aid,
cid: finaldata.cid,
title: finaldata.title
};
let newValue = JSON.stringify(oldvalue);
fs.writeFile('./json/videoMessage.json', newValue, function(err){
if (err) {
console.log('写文件出错\n', err);
}
get_video_list();
});
});
});
});
}
function get_video_list(){
// 视频下载接口
const b_download_URL = 'https://api.bilibili.com/x/player/playurl';
let options = {};
fs.readFile('./json/videoMessage.json', function(err, data){
if (err) {
console.log('读取文件失败\n', err);
}
console.log('读取文件成功');
options = JSON.parse(data.toString());
https.get(b_download_URL + '?avid=' + options.aid + '&cid=' + options.cid + '&qn=80', { headers: headers },(res)=>{
// 设置编码
res.setEncoding('utf-8');
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end',() =>{
let finalData = JSON.parse(data).data;
// console.log(finalData);
fs.readFile('./json/videoMessage.json', function(err, data){
if (err) {
console.log(err);
}
let oldValue = JSON.parse(data.toString());
oldValue.qn = finalData.accept_quality;
oldValue.list = finalData.durl;
let newValue = JSON.stringify(oldValue);
fs.writeFile('./json/videoMessage.json', newValue, function(err){
if (err) {
console.log('视频下载列表写入失败');
}
main();
})
});
});
});
});
}
function download(dirpath, opt){
http.get(opt.url, download_options, (res)=> {
let writerStream = fs.createWriteStream('./video/' + dirpath + '/' + opt.order + '.flv');
// 视频总大小
let size = opt.size;
// 1M = 1048576B
let total = (size / 1048576).toFixed(2);
// 视频下载完成大小
let cur = 0;
// 视频完成比例
let curPercent = 0.00;
console.log('开始下载');
res.on('data', (chunk) =>{
writerStream.write(chunk);
cur += chunk.length;
let nowPercent = (100.0 * cur / size).toFixed(2);
if (nowPercent - curPercent > 1.00) {
curPercent = nowPercent;
console.log("Downloading" + nowPercent + '% ' + (cur / 1048576).toFixed(2) + 'MB, 共 ' + total + ' MB')
}
});
res.on('end',()=>{
writerStream.end();
writerStream.on('finish', function(){
console.log('Downloading complete!');
});
writerStream.on('error', function(error){
console.log(error);
});
})
});
}
function main(){
fs.readFile('./json/videoMessage.json',function(err, data){
if (err) {
console.log('视频下载列表获取失败');
}
let videolist = JSON.parse(data.toString());
fs.mkdir('./video/' + videolist.title, function (error) {
if(error){
console.log(error);
return false;
}
console.log('创建目录 ['+ videolist.title +'] 成功');
});
for(let i = 0; i < videolist.list.length; i++){
download(videolist.title, videolist.list[i]);
}
});
}
get_cid(aid);