还原kg搜索接口

This commit is contained in:
Folltoshe 2023-04-27 17:46:18 +08:00
parent 35e3dafb4b
commit 9bd0e38df7
4 changed files with 143 additions and 34 deletions

View File

@ -1,5 +1,5 @@
import { httpFetch } from '../../request'
import { decodeName, formatPlayTime, sizeFormate } from '../../index' import { decodeName, formatPlayTime, sizeFormate } from '../../index'
import { signatureParams, createHttpFetch } from './util'
export default { export default {
limit: 30, limit: 30,
@ -7,13 +7,8 @@ export default {
page: 0, page: 0,
allPage: 1, allPage: 1,
musicSearch(str, page, limit) { musicSearch(str, page, limit) {
const sign = signatureParams(`userid=0&area_code=1&appid=1005&dopicfull=1&page=${page}&token=0&privilegefilter=0&requestid=0&pagesize=${limit}&user_labels=&clienttime=0&sec_aggre=1&iscorrection=1&uuid=0&mid=0&keyword=${str}&dfid=-&clientver=11409&platform=AndroidFilter&tag=`, 3) const searchRequest = httpFetch(`https://songsearch.kugou.com/song_search_v2?keyword=${encodeURIComponent(str)}&page=${page}&pagesize=${limit}&userid=0&clientver=&platform=WebFilter&filter=2&iscorrection=1&privilege_filter=0`)
return createHttpFetch(`https://gateway.kugou.com/complexsearch/v3/search/song?userid=0&area_code=1&appid=1005&dopicfull=1&page=${page}&token=0&privilegefilter=0&requestid=0&pagesize=${limit}&user_labels=&clienttime=0&sec_aggre=1&iscorrection=1&uuid=0&mid=0&dfid=-&clientver=11409&platform=AndroidFilter&tag=&keyword=${encodeURIComponent(str)}&signature=${sign}`, { return searchRequest.promise.then(({ body }) => body)
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
referer: 'https://kugou.com',
},
}).then(body => body)
}, },
filterList(raw) { filterList(raw) {
let ids = new Set() let ids = new Set()
@ -33,33 +28,33 @@ export default {
hash: item.FileHash, hash: item.FileHash,
} }
} }
if (item.HQ != undefined) { if (item.HQFileSize !== 0) {
let size = sizeFormate(item.HQ.FileSize) let size = sizeFormate(item.HQFileSize)
types.push({ type: '320k', size, hash: item.HQ.Hash }) types.push({ type: '320k', size, hash: item.HQFileHash })
_types['320k'] = { _types['320k'] = {
size, size,
hash: item.HQ.Hash, hash: item.HQFileHash,
} }
} }
if (item.SQ != undefined) { if (item.SQFileSize !== 0) {
let size = sizeFormate(item.SQ.FileSize) let size = sizeFormate(item.SQFileSize)
types.push({ type: 'flac', size, hash: item.SQ.Hash }) types.push({ type: 'flac', size, hash: item.SQFileHash })
_types.flac = { _types.flac = {
size, size,
hash: item.SQ.Hash, hash: item.SQFileHash,
} }
} }
if (item.Res != undefined) { if (item.ResFileSize !== 0) {
let size = sizeFormate(item.Res.FileSize) let size = sizeFormate(item.ResFileSize)
types.push({ type: 'flac24bit', size, hash: item.Res.Hash }) types.push({ type: 'flac24bit', size, hash: item.ResFileHash })
_types.flac24bit = { _types.flac24bit = {
size, size,
hash: item.Res.Hash, hash: item.ResFileHash,
} }
} }
list.push({ list.push({
singer: decodeName(item.SingerName), singer: decodeName(item.SingerName),
name: decodeName(item.OriSongName), name: decodeName(item.SongName),
albumName: decodeName(item.AlbumName), albumName: decodeName(item.AlbumName),
albumId: item.AlbumID, albumId: item.AlbumID,
songmid: item.Audioid, songmid: item.Audioid,
@ -75,27 +70,30 @@ export default {
typeUrl: {}, typeUrl: {},
}) })
}) })
return list return list
}, },
handleResult(rawData) { handleResult(raw) {
const rawList = [] const handleList = []
rawData.forEach(item => {
rawList.push(item)
item.Grp.forEach(e => rawList.push(e))
})
return this.filterList(rawList) raw.forEach(item => {
handleList.push(item)
for (e in item.Grp) {
handleList.push(e)
}
})
return this.filterList(handleList)
}, },
search(str, page = 1, limit, retryNum = 0) { search(str, page = 1, limit, retryNum = 0) {
if (++retryNum > 3) return Promise.reject(new Error('try max num')) if (++retryNum > 3) return Promise.reject(new Error('try max num'))
if (limit == null) limit = this.limit if (limit == null) limit = this.limit
// http://newlyric.kuwo.cn/newlyric.lrc?62355680
return this.musicSearch(str, page, limit).then(result => {
if (!result || result.error_code !== 0) return this.search(str, page, limit, retryNum)
let list = this.handleResult(result.data.lists)
return this.musicSearch(str, page, limit).then(data => { if (list == null) return this.search(str, page, limit, retryNum)
let list = this.handleResult(data.lists)
if (!list) return this.search(str, page, limit, retryNum)
this.total = data.total this.total = result.data.total
this.page = page this.page = page
this.allPage = Math.ceil(this.total / limit) this.allPage = Math.ceil(this.total / limit)

View File

@ -0,0 +1,111 @@
import { decodeName, formatPlayTime, sizeFormate } from '../../index'
import { signatureParams, createHttpFetch } from './util'
export default {
limit: 30,
total: 0,
page: 0,
allPage: 1,
musicSearch(str, page, limit) {
const sign = signatureParams(`userid=0&area_code=1&appid=1005&dopicfull=1&page=${page}&token=0&privilegefilter=0&requestid=0&pagesize=${limit}&user_labels=&clienttime=0&sec_aggre=1&iscorrection=1&uuid=0&mid=0&keyword=${str}&dfid=-&clientver=11409&platform=AndroidFilter&tag=`, 3)
return createHttpFetch(`https://gateway.kugou.com/complexsearch/v3/search/song?userid=0&area_code=1&appid=1005&dopicfull=1&page=${page}&token=0&privilegefilter=0&requestid=0&pagesize=${limit}&user_labels=&clienttime=0&sec_aggre=1&iscorrection=1&uuid=0&mid=0&dfid=-&clientver=11409&platform=AndroidFilter&tag=&keyword=${encodeURIComponent(str)}&signature=${sign}`, {
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
referer: 'https://kugou.com',
},
}).then(body => body)
},
filterList(raw) {
let ids = new Set()
const list = []
raw.forEach(item => {
if (ids.has(item.Audioid)) return
ids.add(item.Audioid)
const types = []
const _types = {}
if (item.FileSize !== 0) {
let size = sizeFormate(item.FileSize)
types.push({ type: '128k', size, hash: item.FileHash })
_types['128k'] = {
size,
hash: item.FileHash,
}
}
if (item.HQ != undefined) {
let size = sizeFormate(item.HQ.FileSize)
types.push({ type: '320k', size, hash: item.HQ.Hash })
_types['320k'] = {
size,
hash: item.HQ.Hash,
}
}
if (item.SQ != undefined) {
let size = sizeFormate(item.SQ.FileSize)
types.push({ type: 'flac', size, hash: item.SQ.Hash })
_types.flac = {
size,
hash: item.SQ.Hash,
}
}
if (item.Res != undefined) {
let size = sizeFormate(item.Res.FileSize)
types.push({ type: 'flac24bit', size, hash: item.Res.Hash })
_types.flac24bit = {
size,
hash: item.Res.Hash,
}
}
list.push({
singer: decodeName(item.SingerName),
name: decodeName(item.OriSongName),
albumName: decodeName(item.AlbumName),
albumId: item.AlbumID,
songmid: item.Audioid,
source: 'kg',
interval: formatPlayTime(item.Duration),
_interval: item.Duration,
img: null,
lrc: null,
otherSource: null,
hash: item.FileHash,
types,
_types,
typeUrl: {},
})
})
return list
},
handleResult(rawData) {
const rawList = []
rawData.forEach(item => {
rawList.push(item)
item.Grp.forEach(e => rawList.push(e))
})
return this.filterList(rawList)
},
search(str, page = 1, limit, retryNum = 0) {
if (++retryNum > 3) return Promise.reject(new Error('try max num'))
if (limit == null) limit = this.limit
return this.musicSearch(str, page, limit).then(data => {
let list = this.handleResult(data.lists)
if (!list) return this.search(str, page, limit, retryNum)
this.total = data.total
this.page = page
this.allPage = Math.ceil(this.total / limit)
return Promise.resolve({
list,
allPage: this.allPage,
limit,
total: this.total,
source: 'kg',
})
})
},
}