Skip to content

Commit

Permalink
🍻 添加歌曲资源
Browse files Browse the repository at this point in the history
  • Loading branch information
fengT-T committed Apr 23, 2020
1 parent 085298a commit 036048a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 21 deletions.
53 changes: 36 additions & 17 deletions src/renderer/store/modules/Player.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSongVkey, getLyric, getCdn } from '../../../spider/index'
import { throttle, random } from 'lodash'
import { throttle, random, findIndex, findLastIndex } from 'lodash'
import { setMprisProp, setPosition, mpris } from '../../mpris'
import { formatPlayerTime } from '../../util'

Expand Down Expand Up @@ -178,21 +178,33 @@ const actions = {
* @param {Object} store
*/
previous ({ state, dispatch }) {
let playListLength = state.playList.length
let previous = state.mode === 'random'
? random(playListLength)
: state.currentPlayIndex <= 0 ? playListLength - 1 : state.currentPlayIndex - 1
let previous = 0
if (state.mode === 'random') {
let canPlayList = state.playList.filter(song => song.pay !== 1)
let canplayListLength = canPlayList.length
previous = state.playList.findIndex(song => song.songMid === canPlayList[random(canplayListLength - 1)].songMid)
} else {
previous = state.currentPlayIndex >= 0
? findLastIndex(state.playList, song => song.pay !== 1, state.currentPlayIndex - 1)
: findLastIndex(state.playList, song => song.pay !== 1)
}
dispatch('setPlay', previous)
},
/**
* 下一首
* @param {Object} state
*/
next ({ state, dispatch }) {
let playListLength = state.playList.length
let next = state.mode === 'random'
? random(playListLength)
: state.currentPlayIndex < playListLength - 1 ? state.currentPlayIndex + 1 : 0
next ({ state, dispatch, getters }) {
let next = 0
if (state.mode === 'random') {
let canPlayList = state.playList.filter(song => song.pay !== 1)
let canplayListLength = canPlayList.length
next = state.playList.findIndex(song => song.songMid === canPlayList[random(canplayListLength - 1)].songMid)
} else {
next = state.currentPlayIndex < state.playList.length - 1
? findIndex(state.playList, song => song.pay !== 1, state.currentPlayIndex + 1)
: findIndex(state.playList, song => song.pay !== 1)
}
dispatch('setPlay', next)
},
/**
Expand All @@ -204,20 +216,27 @@ const actions = {
const { guid, cdn } = state
const current = state.playList[index] // 保存当前播放的引用

const song = state.playList[index]
// 开始获取token播放歌曲

const purl = await getSongVkey({
guid, ...song
})

if (!purl) {
commit('setPlayerState', { loading: false })
return
}

commit('setPlayerState', { loading: true })
preLoadImg.src = `https://y.gtimg.cn/music/photo_new/T002R300x300M000${current.album.albumMid}.jpg?max_age=2592000` // 预加载图片,不然显得很突兀的说

state.player.pause()
state.player.currentTime = 0 // 暂停,据说这个是有效的停止缓冲数据,不过好像也没提供方法我也就不知道了

// 开始获取token播放歌曲
const song = state.playList[index]
const { vkey } = await getSongVkey({
guid, ...song
})

// eslint-disable-next-line no-unreachable
commit('setPlayerSrc', [
`${cdn}${song.fileName}?vkey=${vkey}&guid=${guid}&uin=0&fromtag=66`
cdn + purl
])

state.player.load()
Expand Down
58 changes: 54 additions & 4 deletions src/spider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import request from 'axios'
import http from 'http'
import https from 'https'
import { Singer, Album, Music, Mv, Lyric, Category, PlayList } from './commonObject'
import flow from 'lodash/flow'

const keepAliveConf = {
keepAlive: true
Expand All @@ -51,6 +52,10 @@ const baseRequest = request.create({
httpAgent,
httpsAgent
// timeout: 2000 // 这个时间不好把握,我只能说小霸王服务器该换了
// proxy: {
// host: '39.137.69.9',
// port: 80
// }
})

// page 从 1 开始
Expand Down Expand Up @@ -166,11 +171,56 @@ export async function getKey (guid) {
let {key} = JSON.parse((await baseRequest(url)).data.slice(13, -2))
return key
}
/**
* 对象 转成 url 字符串
*/
const urlDecodeObjectUrl = flow(JSON.stringify, decodeURIComponent)

const getMusicParams = (songmid) => urlDecodeObjectUrl({
req: {
module: 'CDN.SrfCdnDispatchServer',
method: 'GetCdnDispatch',
param: {
guid: '899052935',
calltype: 0,
userip: ''
}
},
// eslint-disable-next-line @typescript-eslint/camelcase
req_0: {
module: 'vkey.GetVkeyServer',
method: 'CgiGetVkey',
param: {
guid: '899052935',
songmid: [
songmid
],
songtype: [
0
],
uin: '0',
loginflag: 1,
platform: '20'
}
},
comm: {
uin: 0,
format: 'json',
ct: 24,
cv: 0
}
})
export async function getSongVkey({fileName, guid, songMid}) {
// cid 是啥? 我也不造啊 qq 那群人写死了
let url = `http://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?cid=205361747&songmid=${songMid}&filename=${fileName}&guid=${guid}`
let {vkey} = (await baseRequest(url)).data.data.items[0]
return {vkey}
const url = 'http://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0'
const params = getMusicParams(songMid)
const result = (await baseRequest({
url,
params: {
data: params
}
})).data
return result.req_0.data.midurlinfo[0].purl
}
// 从 1 开始
export async function getSearch ({keyword, page}) {
Expand Down Expand Up @@ -257,7 +307,7 @@ export async function getPlayListInfo (playListMid) {
}

export async function getCdn (guid) {
const url = `https://u.y.qq.com/cgi-bin/musicu.fcg?data=${encodeURIComponent(`{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":${guid},"calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"${guid}","songmid":["000KDHyB23K7Eq"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}`)}`
const url = `http://u.y.qq.com/cgi-bin/musicu.fcg?data=${encodeURIComponent(`{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":${guid},"calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"${guid}","songmid":["000KDHyB23K7Eq"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}`)}`
const { data: { req: { data: { sip: cdnList, testfilewifi: testUrl } } }} = await baseRequest(url)
return new Promise((resolve, reject) => {
cdnList.map(cdn => {
Expand Down

0 comments on commit 036048a

Please sign in to comment.