|
| 1 | +const { |
| 2 | + cacheStorage, |
| 3 | + CacheStorageGroup, |
| 4 | + getManagedCacheStorage, |
| 5 | +} = require('../cache'); |
| 6 | +const insure = require('./insure'); |
| 7 | +const select = require('./select'); |
| 8 | +const request = require('../request'); |
| 9 | +const crypto = require('../crypto'); |
| 10 | +const { logScope } = require('../logger'); |
| 11 | + |
| 12 | +const logger = logScope('provider/bilivideo'); |
| 13 | +const cs = getManagedCacheStorage('provider/bilivideo'); |
| 14 | + |
| 15 | +//Wbi 和 API 部分参考: https://github.com/SocialSisterYi/bilibili-API-collect |
| 16 | + |
| 17 | +const mixinKeyEncTab = [ |
| 18 | + 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, |
| 19 | + 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, |
| 20 | + 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, |
| 21 | + 36, 20, 34, 44, 52, |
| 22 | +]; |
| 23 | + |
| 24 | +// 对 imgKey 和 subKey 进行字符顺序打乱编码 |
| 25 | +const getMixinKey = (orig) => |
| 26 | + mixinKeyEncTab |
| 27 | + .map((n) => orig[n]) |
| 28 | + .join('') |
| 29 | + .slice(0, 32); |
| 30 | + |
| 31 | +// 为请求参数进行 wbi 签名 |
| 32 | +function encWbi(params, img_key, sub_key) { |
| 33 | + const mixin_key = getMixinKey(img_key + sub_key), |
| 34 | + curr_time = Math.round(Date.now() / 1000), |
| 35 | + chr_filter = /[!'()*]/g; |
| 36 | + |
| 37 | + Object.assign(params, { wts: curr_time }); // 添加 wts 字段 |
| 38 | + // 按照 key 重排参数 |
| 39 | + const query = Object.keys(params) |
| 40 | + .sort() |
| 41 | + .map((key) => { |
| 42 | + // 过滤 value 中的 "!'()*" 字符 |
| 43 | + const value = params[key].toString().replace(chr_filter, ''); |
| 44 | + return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; |
| 45 | + }) |
| 46 | + .join('&'); |
| 47 | + |
| 48 | + const wbi_sign = crypto.md5.digest(query + mixin_key); // 计算 w_rid |
| 49 | + |
| 50 | + return query + '&w_rid=' + wbi_sign; |
| 51 | +} |
| 52 | + |
| 53 | +// 获取最新的 img_key 和 sub_key |
| 54 | +async function getWbiKeys() { |
| 55 | + const res = await request( |
| 56 | + 'GET', |
| 57 | + 'https://api.bilibili.com/x/web-interface/nav', |
| 58 | + { |
| 59 | + // SESSDATA 字段 |
| 60 | + 'User-Agent': |
| 61 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', |
| 62 | + Referer: 'https://www.bilibili.com/', //对于直接浏览器调用可能不适用 |
| 63 | + } |
| 64 | + ); |
| 65 | + const { |
| 66 | + data: { |
| 67 | + wbi_img: { img_url, sub_url }, |
| 68 | + }, |
| 69 | + } = await res.json(); |
| 70 | + |
| 71 | + return { |
| 72 | + img_key: img_url.slice( |
| 73 | + img_url.lastIndexOf('/') + 1, |
| 74 | + img_url.lastIndexOf('.') |
| 75 | + ), |
| 76 | + sub_key: sub_url.slice( |
| 77 | + sub_url.lastIndexOf('/') + 1, |
| 78 | + sub_url.lastIndexOf('.') |
| 79 | + ), |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +const signParam = async (param) => { |
| 84 | + const { img_key, sub_key } = await cs.cache( |
| 85 | + 'wbikey', |
| 86 | + async () => await getWbiKeys() |
| 87 | + ); |
| 88 | + |
| 89 | + return encWbi(param, img_key, sub_key); |
| 90 | +}; |
| 91 | + |
| 92 | +const format = (song) => { |
| 93 | + return { |
| 94 | + id: song.bvid, |
| 95 | + name: song.title, |
| 96 | + // album: {id: song.album_id, name: song.album_title}, |
| 97 | + artists: { id: song.typeid, name: song.typename }, |
| 98 | + }; |
| 99 | +}; |
| 100 | + |
| 101 | +const getBiliVideoHeader = async () => { |
| 102 | + const url = 'https://www.bilibili.com'; |
| 103 | + |
| 104 | + return cs.cache('bilicookie', () => |
| 105 | + request('GET', url).then((response) => |
| 106 | + response.headers['set-cookie'] |
| 107 | + .map((cookie) => cookie.split(';')[0]) |
| 108 | + .join('; ') |
| 109 | + ) |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +const search = (info) => { |
| 114 | + return getBiliVideoHeader().then((cookies) => { |
| 115 | + return signParam({ |
| 116 | + search_type: 'video', |
| 117 | + keyword: info.keyword, |
| 118 | + }).then((param) => { |
| 119 | + const url = |
| 120 | + 'https://api.bilibili.com/x/web-interface/wbi/search/type?' + |
| 121 | + param; |
| 122 | + return request('GET', url, { |
| 123 | + cookie: cookies, |
| 124 | + referer: 'https://search.bilibili.com', |
| 125 | + }) |
| 126 | + .then((response) => response.json()) |
| 127 | + .then((jsonBody) => { |
| 128 | + const list = jsonBody.data.result.map(format); |
| 129 | + const matched = select(list, info); |
| 130 | + |
| 131 | + return matched ? matched.id : Promise.reject(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}; |
| 136 | + |
| 137 | +const track = (id) => { |
| 138 | + return signParam({ bvid: id }).then((param) => { |
| 139 | + const url = |
| 140 | + 'https://api.bilibili.com/x/web-interface/wbi/view?' + param; |
| 141 | + |
| 142 | + return request('GET', url) |
| 143 | + .then((response) => response.json()) |
| 144 | + .then((jsonBody) => { |
| 145 | + if (jsonBody.code === 0) { |
| 146 | + // bilibili music requires referer, connect do not support referer, so change to http |
| 147 | + |
| 148 | + return signParam({ |
| 149 | + bvid: id, |
| 150 | + cid: jsonBody.data.cid, |
| 151 | + fnval: 16, |
| 152 | + platform: 'pc', |
| 153 | + }).then((param) => { |
| 154 | + const url = |
| 155 | + 'https://api.bilibili.com/x/player/wbi/playurl?' + |
| 156 | + param; |
| 157 | + |
| 158 | + return request('GET', url) |
| 159 | + .then((response) => response.json()) |
| 160 | + .then((jsonBody) => { |
| 161 | + if (jsonBody.code === 0) { |
| 162 | + if (jsonBody.data.dash.audio != null) { |
| 163 | + return jsonBody.data.dash.audio[0] |
| 164 | + .base_url; |
| 165 | + } |
| 166 | + return Promise.reject(); |
| 167 | + } else { |
| 168 | + return Promise.reject(); |
| 169 | + } |
| 170 | + }) |
| 171 | + .catch(() => insure().bilibili.track(id)); |
| 172 | + }); |
| 173 | + } else { |
| 174 | + return Promise.reject(); |
| 175 | + } |
| 176 | + }) |
| 177 | + .catch(() => insure().bilibili.track(id)); |
| 178 | + }); |
| 179 | +}; |
| 180 | + |
| 181 | +const check = (info) => cs.cache(info, () => search(info)).then(track); |
| 182 | + |
| 183 | +module.exports = { check, track }; |
0 commit comments