共计 2906 个字符,预计需要花费 8 分钟才能阅读完成。
方法一 ###(有点问题)
利用 axios 伪造,抓取的网页版 qq 音乐的接口,但这个接口有瑕疵 … 能获取到 Vkey,但不是每一首歌曲都能正常播放 … 后来发现网页版音乐中每个歌曲的播放地址的域名不是都一样的,不能播放的歌曲有可能是这个接口不对 … 还没找到解决办法 http://isure.stream.qqmusic.q…
song.js
export function createSong(musicData, songVkey) {
return new Song({
id: musicData.songid,
mid: musicData.songmid,
singer: filterSinger(musicData.singer),
name: musicData.songname,
album: musicData.albumname,
duration: musicData.interval,
image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
url: `http://isure.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?guid=9244517832&vkey=${songVkey}&uin=0&fromtag=66`
})
}
singer.js
export function getSongVkey(songmid) {
const url = ‘/api/getSongVkey’
const data = Object.assign({}, commonParams, {
songmid: songmid,
notice: 0,
platform: ‘yqq.json’,
needNewCode: 0,
format: ‘json’,
data: {“req_0”:{“module”:”vkey.GetVkeyServer”,”method”:”CgiGetVkey”,”param”:{“guid”:”9244517832″,”songmid”:[“001Qu4I30eVFYb”],”songtype”:[0],”uin”:”0″,”loginflag”:1,”platform”:”20″}},”comm”:{“uin”:0,”format”:”json”,”ct”:24,”cv”:0}}
})
return axios.get(url, {
params: data
}).then((res) => {
return Promise.resolve(res.data)
})
}
webpack.dev.config
app.get(‘/api/getSongVkey’, function (req, res) {
var url = ‘https://u.y.qq.com/cgi-bin/musicu.fcg’
axios.get(url, {
headers: {
referer: ‘https://u.y.qq.com/’,
host: ‘u.y.qq.com’
},
params: req.query
}).then((response) => {
res.json(response.data)
}).catch((e) => {
console.log(e)
})
})
singer-detail
_normallizeSongs(list, callback) {
let ret = []
let index = 1
list.forEach((item) => {
let {musicData} = item // 得到 music 对象
if (musicData.songid && musicData.albummid) {
getSongVkey(musicData.songmid).then((res) => {
if (res.code === ERR_OK) {
const sVkey = res.req_0.data.midurlinfo[0]
const songVkey = sVkey.vkey
const newSong = createSong(musicData, songVkey)
console.log(newSong)
ret.push(newSong)
if (index === list.length) {
callback && callback(ret)
}
index ++
}
})
}
})
}
方法二 ###(实测没有问题)
这个是抓取的是 h5 版 qq 音乐的 jsonp 接口 singer.js
export function getSongVkey(songmid) {
const url = ‘https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg’
const data = Object.assign({}, {
callback: ‘musicJsonCallback’,
loginUin: 3051522991,
format: ‘jsonp’,
platform: ‘yqq’,
needNewCode: 0,
cid: 205361747,
uin: 3051522991,
guid: 5931742855,
songmid: songmid,
filename: `C400${songmid}.m4a`
})
song.js
export function createSong(musicData, songVkey) {
return new Song({
id: musicData.songid,
mid: musicData.songmid,
singer: filterSinger(musicData.singer),
name: musicData.songname,
album: musicData.albumname,
duration: musicData.interval,
image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
url: `http://dl.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?fromtag=38&guid=5931742855&vkey=${songVkey}`
})
}
singer-detail
_normallizeSongs(list) {
let ret = []
list.forEach((item) => {
let {musicData} = item // 得到 music 对象
getSongVkey(musicData.songmid).then((res) => {
const songVkey = res.data.items[0].vkey
if (musicData.songid && musicData.albummid) {
ret.push(createSong(musicData, songVkey))
}
})
})
console.log(ret)
return ret
}