Skip to content

Commit

Permalink
修复kw搜索联想词 (#1452)
Browse files Browse the repository at this point in the history
* 修复kw搜索联想词

* 完善

---------

Co-authored-by: lyswhut <[email protected]>
  • Loading branch information
Folltoshe and lyswhut authored Jul 7, 2023
1 parent e8d8feb commit 6423066
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
2 changes: 1 addition & 1 deletion publish/changeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### 修复

- 修复字体设置某些字体无法应用的问题

- 修复搜索提示功能失效的问题(#1452, @Folltoshe
8 changes: 4 additions & 4 deletions src/renderer/utils/musicSdk/kw/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { httpFetch } from '../../request'
import tipSearch from './tipSearch'
import musicSearch from './musicSearch'
import { formatSinger, getToken } from './util'
import { formatSinger } from './util'
import leaderboard from './leaderboard'
import lyric from './lyric'
import pic from './pic'
Expand Down Expand Up @@ -94,9 +94,9 @@ const kw = {
return `http://www.kuwo.cn/play_detail/${songInfo.songmid}`
},

init() {
return getToken()
},
// init() {
// return getToken()
// },
}

export default kw
26 changes: 14 additions & 12 deletions src/renderer/utils/musicSdk/kw/tipSearch.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { decodeName } from '../../index'
import { tokenRequest } from './util'
// import { decodeName } from '../../index'
// import { tokenRequest } from './util'
import { httpFetch } from '../../request'

export default {
regExps: {
relWord: /RELWORD=(.+)/,
},
requestObj: null,
async tipSearchBySong(str) {
// 报错403,加了referer还是有问题(直接换一个
// this.requestObj = await tokenRequest(`http://www.kuwo.cn/api/www/search/searchKey?key=${encodeURIComponent(str)}`)

this.cancelTipSearch()
this.requestObj = await tokenRequest(`http://www.kuwo.cn/api/www/search/searchKey?key=${encodeURIComponent(str)}`)
return this.requestObj.promise.then(({ body }) => {
// console.log(body)
if (body.code !== 200) return Promise.reject(new Error('请求失败'))
return body
this.requestObj = httpFetch(`https://tips.kuwo.cn/t.s?corp=kuwo&newver=3&p2p=1&notrace=0&c=mbox&w=${encodeURIComponent(str)}&encoding=utf8&rformat=json`, {
Referer: 'http://www.kuwo.cn/',
})
return this.requestObj.promise.then(({ body, statusCode }) => {
if (statusCode != 200 || !body.WORDITEMS) return Promise.reject(new Error('请求失败'))
return body.WORDITEMS
})
},
handleResult(rawData) {
return rawData.map(info => {
let matchResult = info.match(this.regExps.relWord)
return matchResult ? decodeName(matchResult[1]) : ''
})
return rawData.map(item => item.RELWORD)
},
cancelTipSearch() {
if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp()
},
async search(str) {
return this.tipSearchBySong(str).then(result => this.handleResult(result.data))
return this.tipSearchBySong(str).then(result => this.handleResult(result))
},
}
78 changes: 39 additions & 39 deletions src/renderer/utils/musicSdk/kw/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { httpGet, httpFetch } from '../../request'
// import { httpGet, httpFetch } from '../../request'
import { WIN_MAIN_RENDERER_EVENT_NAME } from '@common/ipcNames'
import { rendererInvoke } from '@common/rendererIpc'

const kw_token = {
token: null,
isGetingToken: false,
}
// const kw_token = {
// token: null,
// isGetingToken: false,
// }

// const translationMap = {
// "{'": '{"',
Expand Down Expand Up @@ -43,46 +43,46 @@ export const matchToken = headers => {
}
}

const wait = time => new Promise(resolve => setTimeout(() => resolve(), time))
// const wait = time => new Promise(resolve => setTimeout(() => resolve(), time))


export const getToken = (retryNum = 0) => new Promise((resolve, reject) => {
if (retryNum > 2) return Promise.reject(new Error('try max num'))
// export const getToken = (retryNum = 0) => new Promise((resolve, reject) => {
// if (retryNum > 2) return Promise.reject(new Error('try max num'))

if (kw_token.isGetingToken) return wait(1000).then(() => getToken(retryNum).then(token => resolve(token)))
if (kw_token.token) return resolve(kw_token.token)
kw_token.isGetingToken = true
httpGet('http://www.kuwo.cn/', (err, resp) => {
kw_token.isGetingToken = false
if (err) return getToken(++retryNum)
if (resp.statusCode != 200) return reject(new Error('获取失败'))
const token = kw_token.token = matchToken(resp.headers)
resolve(token)
})
})
// if (kw_token.isGetingToken) return wait(1000).then(() => getToken(retryNum).then(token => resolve(token)))
// if (kw_token.token) return resolve(kw_token.token)
// kw_token.isGetingToken = true
// httpGet('http://www.kuwo.cn/', (err, resp) => {
// kw_token.isGetingToken = false
// if (err) return getToken(++retryNum)
// if (resp.statusCode != 200) return reject(new Error('获取失败'))
// const token = kw_token.token = matchToken(resp.headers)
// resolve(token)
// })
// })

export const decodeLyric = base64Data => rendererInvoke(WIN_MAIN_RENDERER_EVENT_NAME.handle_kw_decode_lyric, base64Data)

export const tokenRequest = async(url, options = {}) => {
let token = kw_token.token
if (!token) token = await getToken()
if (!options.headers) {
options.headers = {
Referer: 'http://www.kuwo.cn/',
csrf: token,
cookie: 'kw_token=' + token,
}
}
const requestObj = httpFetch(url, options)
requestObj.promise = requestObj.promise.then(resp => {
// console.log(resp)
if (resp.statusCode == 200) {
kw_token.token = matchToken(resp.headers)
}
return resp
})
return requestObj
}
// export const tokenRequest = async(url, options = {}) => {
// let token = kw_token.token
// if (!token) token = await getToken()
// if (!options.headers) {
// options.headers = {
// Referer: 'http://www.kuwo.cn/',
// csrf: token,
// cookie: 'kw_token=' + token,
// }
// }
// const requestObj = httpFetch(url, options)
// requestObj.promise = requestObj.promise.then(resp => {
// // console.log(resp)
// if (resp.statusCode == 200) {
// kw_token.token = matchToken(resp.headers)
// }
// return resp
// })
// return requestObj
// }

export const lrcTools = {
rxps: {
Expand Down

0 comments on commit 6423066

Please sign in to comment.