Skip to content

Commit

Permalink
添加bilibili音乐 nondanee#711
Browse files Browse the repository at this point in the history
  • Loading branch information
rbao123 committed May 21, 2021
1 parent 822156a commit 682dc7f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (config.forceHost && require('net').isIP(config.forceHost) === 0) {
process.exit(1)
}
if (config.matchOrder) {
const provider = new Set(['netease', 'qq', 'xiami', 'baidu', 'kugou', 'kuwo', 'migu', 'joox', 'youtube'])
const provider = new Set(['netease', 'qq', 'xiami', 'baidu', 'kugou', 'kuwo', 'migu', 'joox', 'youtube','bilibili'])
const candidate = config.matchOrder
if (candidate.some((key, index) => index != candidate.indexOf(key))) {
console.log('Please check the duplication in match order.')
Expand Down
3 changes: 2 additions & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const router = {
kugou: require('./provider/kugou'),
kuwo: require('./provider/kuwo'),
migu: require('./provider/migu'),
joox: require('./provider/joox')
joox: require('./provider/joox'),
bilibili: require('./provider/bilibili')
}

const distribute = (url, router) =>
Expand Down
6 changes: 6 additions & 0 deletions src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ hook.request.before = ctx => {
})
.catch(error => console.log(error, req.url))
}
else if(req.url.includes("bilivideo.com")){
// console.log("rewrite mitm")
req.headers['referer'] = 'https://www.bilibili.com/'
req.headers['host'] = url.hostname
req.headers['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
else if ((hook.target.host.has(url.hostname)) && (url.path.startsWith('/weapi/') || url.path.startsWith('/api/'))) {
req.headers['X-Real-IP'] = '118.88.88.88'
ctx.netease = {web: true, path: url.path.replace(/^\/weapi\//, '/api/').replace(/\?.+$/, '').replace(/\/\d*$/, '')}
Expand Down
50 changes: 50 additions & 0 deletions src/provider/bilibili.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const cache = require('../cache')
const insure = require('./insure')
const select = require('./select')
const request = require('../request')

const format = song => {
return {
id: song.id,
name: song.title,
// album: {id: song.album_id, name: song.album_title},
artists: {id: song.mid, name: song.author}
}
}

const search = info => {
const url =
'https://api.bilibili.com/audio/music-service-c/s?' +
'search_type=music&page=1&pagesize=30&' +
`keyword=${encodeURIComponent(info.keyword)}`
return request('GET', url)
.then(response => response.json())
.then(jsonBody => {
const list = jsonBody.data.result.map(format)
const matched = select(list, info)
return matched ? matched.id : Promise.reject()
})
}

const track = id => {
// weburl
const url =
'https://www.bilibili.com/audio/music-service-c/web/url?rivilege=2&quality=2&' +
'sid=' + id

return request('GET', url)
.then(response => response.json())
.then(jsonBody => {
if (jsonBody.code === 0) {
// bb music require referer, connect do not support referer, so change to http
return jsonBody.data.cdns[0].replace("https","http")
} else {
return Promise.reject()
}
})
.catch(() => insure().bilibili.track(id))
}

const check = info => cache(search, info).then(track)

module.exports = {check, track}
11 changes: 9 additions & 2 deletions src/provider/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const provider = {

const match = (id, source, data) => {
let meta = {}
const candidate = (source || global.source || ['qq', 'kuwo', 'migu']).filter(name => name in provider)
const candidate = (source || global.source || ['qq', 'kuwo', 'migu','bilibili']).filter(name => name in provider)
return find(id, data)
.then(info => {
meta = info
Expand All @@ -35,7 +35,14 @@ const match = (id, source, data) => {

const check = url => {
const song = {size: 0, br: null, url: null, md5: null}
return Promise.race([request('GET', url, {'range': 'bytes=0-8191'}), new Promise((_, reject) => setTimeout(() => reject(504), 5 * 1000))])
//return Promise.race([request('GET', url, {'range': 'bytes=0-8191'}), new Promise((_, reject) => setTimeout(() => reject(504), 5 * 1000))])
let header = {'range': 'bytes=0-8191'}
if (url.includes("bilivideo.com")){
header = {'range': 'bytes=0-8191',
'referer':"https://www.bilibili.com/"
}
}
return Promise.race([request('GET', url, header), new Promise((_, reject) => setTimeout(() => reject(504), 5 * 1000))])
.then(response => {
if (!response.statusCode.toString().startsWith('2')) return Promise.reject()
if (url.includes('qq.com'))
Expand Down

0 comments on commit 682dc7f

Please sign in to comment.