diff --git a/src/renderer/utils/music/kg/index.js b/src/renderer/utils/music/kg/index.js index e3f0e87f..751a8143 100644 --- a/src/renderer/utils/music/kg/index.js +++ b/src/renderer/utils/music/kg/index.js @@ -2,6 +2,8 @@ import leaderboard from './leaderboard' import api_source from '../api-source' import songList from './songList' import musicSearch from './musicSearch' +import pic from './pic' +import lyric from './lyric' const kg = { leaderboard, @@ -11,11 +13,17 @@ const kg = { return api_source('kg').getMusicUrl(songInfo, type) }, getLyric(songInfo) { - return api_source('kg').getLyric(songInfo) + return lyric.getLyric(songInfo) }, + // getLyric(songInfo) { + // return api_source('kg').getLyric(songInfo) + // }, getPic(songInfo) { - return api_source('kg').getPic(songInfo) + return pic.getPic(songInfo) }, + // getPic(songInfo) { + // return api_source('kg').getPic(songInfo) + // }, } export default kg diff --git a/src/renderer/utils/music/kg/lyric.js b/src/renderer/utils/music/kg/lyric.js new file mode 100644 index 00000000..3b3bb45e --- /dev/null +++ b/src/renderer/utils/music/kg/lyric.js @@ -0,0 +1,33 @@ +import { httpFetch } from '../../request' + +export default { + getIntv(interval) { + let intvArr = interval.split(':') + let intv = 0 + let unit = 1 + while (intvArr.length) { + intv += (intvArr.pop()) * unit + unit *= 60 + } + return parseInt(intv) + }, + getLyric(songInfo, tryNum) { + let requestObj = httpFetch(`http://m.kugou.com/app/i/krc.php?cmd=100&keyword=${encodeURIComponent(songInfo.name)}&hash=${songInfo.hash}&timelength=${songInfo._interval || this.getIntv(songInfo.interval)}&d=0.38664927426725626`, { + headers: { + 'KG-RC': 1, + 'KG-THash': 'expand_search_manager.cpp:852736169:451', + 'User-Agent': 'KuGou2012-9020-ExpandSearchManager', + }, + }) + requestObj.promise = requestObj.promise.then(({ body, statusCode }) => { + if (statusCode !== 200) { + if (tryNum > 5) return Promise.reject('歌词获取失败') + let tryRequestObj = this.getLyric(songInfo, tryNum) + requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj) + return tryRequestObj.promise + } + return body + }) + return requestObj + }, +} diff --git a/src/renderer/utils/music/kg/musicSearch.js b/src/renderer/utils/music/kg/musicSearch.js index 680a3076..726a27cd 100644 --- a/src/renderer/utils/music/kg/musicSearch.js +++ b/src/renderer/utils/music/kg/musicSearch.js @@ -57,6 +57,7 @@ export default { songmid: item.audio_id, source: 'kg', interval: formatPlayTime(item.duration), + _interval: item.duration, img: null, lrc: null, hash: item.hash, diff --git a/src/renderer/utils/music/kg/pic.js b/src/renderer/utils/music/kg/pic.js new file mode 100644 index 00000000..5430ea60 --- /dev/null +++ b/src/renderer/utils/music/kg/pic.js @@ -0,0 +1,44 @@ +import { httpFetch } from '../../request' + +export default { + getPic(songInfo) { + const requestObj = httpFetch( + 'http://media.store.kugou.com/v1/get_res_privilege', + { + method: 'POST', + headers: { + 'KG-RC': 1, + 'KG-THash': 'expand_search_manager.cpp:852736169:451', + 'User-Agent': 'KuGou2012-9020-ExpandSearchManager', + }, + body: { + appid: 1001, + area_code: '1', + behavior: 'play', + clientver: '9020', + need_hash_offset: 1, + relate: 1, + resource: [ + { + album_audio_id: songInfo.songmid, + album_id: songInfo.albumId, + hash: songInfo.hash, + id: 0, + name: `${songInfo.singer} - ${songInfo.name}.mp3`, + type: 'audio', + }, + ], + token: '', + userid: 2626431536, + vip: 1, + }, + } + ) + requestObj.promise = requestObj.promise.then(({ body }) => { + if (body.error_code !== 0) return Promise.reject('图片获取失败') + let info = body.data[0].info + return info.image.replace('{size}', info.imgsize[0]) + }) + return requestObj + }, +}