修复kw搜索联想词 (#1452)

* 修复kw搜索联想词

* 完善

---------

Co-authored-by: lyswhut <lyswhut@qq.com>
pull/1460/head
Folltoshe 2023-07-07 23:54:32 +08:00 committed by GitHub
parent e8d8feb042
commit 6423066cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 56 deletions

View File

@ -1,4 +1,4 @@
### 修复
- 修复字体设置某些字体无法应用的问题
- 修复搜索提示功能失效的问题(#1452, @Folltoshe

View File

@ -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'
@ -94,9 +94,9 @@ const kw = {
return `http://www.kuwo.cn/play_detail/${songInfo.songmid}`
},
init() {
return getToken()
},
// init() {
// return getToken()
// },
}
export default kw

View File

@ -1,5 +1,6 @@
import { decodeName } from '../../index'
import { tokenRequest } from './util'
// import { decodeName } from '../../index'
// import { tokenRequest } from './util'
import { httpFetch } from '../../request'
export default {
regExps: {
@ -7,24 +8,25 @@ export default {
},
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))
},
}

View File

@ -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 = {
// "{'": '{"',
@ -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: {