From 5321469c1d9b0af40b9db504295daa7cab54cb13 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 21 Sep 2019 20:05:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=81=9A=E5=90=88=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/renderer/store/modules/search.js | 12 +- src/renderer/utils/music/bd/index.js | 2 + src/renderer/utils/music/bd/musicSearch.js | 154 +++++++-------------- src/renderer/utils/music/kg/index.js | 2 + src/renderer/utils/music/kg/musicSearch.js | 92 ++++++++++++ 6 files changed, 151 insertions(+), 112 deletions(-) create mode 100644 src/renderer/utils/music/kg/musicSearch.js diff --git a/publish/changeLog.md b/publish/changeLog.md index 64c9cc26..92654d3a 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -1,5 +1,6 @@ ### 新增 +- 新增音乐**聚合搜索**,目前支持酷我、酷狗、百度源搜索 - 新增代理功能 ### 优化 diff --git a/src/renderer/store/modules/search.js b/src/renderer/store/modules/search.js index 598ca5a8..37d881f8 100644 --- a/src/renderer/store/modules/search.js +++ b/src/renderer/store/modules/search.js @@ -1,8 +1,5 @@ import music from '../../utils/music' -const sources = [{ - id: 'all', - name: '聚合搜索', -}] +const sources = [] const sourceList = {} const sourceMaxPage = {} for (const source of music.sources) { @@ -19,6 +16,11 @@ for (const source of music.sources) { sourceMaxPage[source.id] = 0 } +sources.push({ + id: 'all', + name: '聚合搜索', +}) + // state const state = { sourceList, @@ -80,7 +82,7 @@ const mutations = { total += source.total limit += source.limit } - list.sort() + list.sort((a, b) => b.name.charCodeAt(0) - a.name.charCodeAt(0)) state.allPage = Math.max(...pages) state.total = total state.limit = limit diff --git a/src/renderer/utils/music/bd/index.js b/src/renderer/utils/music/bd/index.js index 7a2df1ab..7506c5ae 100644 --- a/src/renderer/utils/music/bd/index.js +++ b/src/renderer/utils/music/bd/index.js @@ -3,10 +3,12 @@ import api_source from '../api-source' import musicInfo from './musicInfo' import songList from './songList' import { httpFatch } from '../../request' +import musicSearch from './musicSearch' const bd = { leaderboard, songList, + musicSearch, getMusicUrl(songInfo, type) { return api_source('bd').getMusicUrl(songInfo, type) }, diff --git a/src/renderer/utils/music/bd/musicSearch.js b/src/renderer/utils/music/bd/musicSearch.js index 39fae3ff..73a24f7e 100644 --- a/src/renderer/utils/music/bd/musicSearch.js +++ b/src/renderer/utils/music/bd/musicSearch.js @@ -1,143 +1,83 @@ // import '../../polyfill/array.find' // import jshtmlencode from 'js-htmlencode' -import { httpGet, cancelHttp } from '../../request' -import { formatPlayTime, decodeName } from '../../index' +import { httpFatch } from '../../request' +import { formatPlayTime } from '../../index' // import { debug } from '../../utils/env' -import { formatSinger } from './util' +// import { formatSinger } from './util' +let searchRequest export default { - regExps: { - mInfo: /bitrate:(\d+),format:(\w+),size:([\w.]+)/, - }, - _musicSearchRequestObj: null, - _musicSearchPromiseCancelFn: null, limit: 30, total: 0, page: 0, allPage: 1, - // cancelFn: null, musicSearch(str, page) { - if (this._musicSearchRequestObj != null) { - cancelHttp(this._musicSearchRequestObj) - this._musicSearchPromiseCancelFn(new Error('取消http请求')) - } - return new Promise((resolve, reject) => { - this._musicSearchPromiseCancelFn = reject - this._musicSearchRequestObj = httpGet(`http://search.kuwo.cn/r.s?client=kt&all=${encodeURIComponent(str)}&pn=${page - 1}&rn=${this.limit}&uid=794762570&ver=kwplayer_ar_9.2.2.1&vipver=1&show_copyright_off=1&newver=1&ft=music&cluster=0&strategy=2012&encoding=utf8&rformat=json&vermerge=1&mobi=1&issubtitle=1`, (err, resp, body) => { - this._musicSearchRequestObj = null - this._musicSearchPromiseCancelFn = null - if (err) { - console.log(err) - reject(err) - } - resolve(body) - }) - }) + if (searchRequest && searchRequest.cancelHttp) searchRequest.cancelHttp() + searchRequest = httpFatch(`http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.6&method=baidu.ting.search.merge&format=json&query=${encodeURIComponent(str)}&page_no=${page}&page_size=${this.limit}&type=0&data_source=0&use_cluster=1`) + return searchRequest.promise.then(({ body }) => body) }, - // getImg(songId) { - // return httpGet(`http://player.kuwo.cn/webmusic/sj/dtflagdate?flag=6&rid=MUSIC_${songId}`) - // }, - // getLrc(songId) { - // return httpGet(`http://mobile.kuwo.cn/mpage/html5/songinfoandlrc?mid=${songId}&flag=0`) - // }, handleResult(rawData) { - const result = [] - for (let i = 0; i < rawData.length; i++) { - const info = rawData[i] - let songId = info.MUSICRID.replace('MUSIC_', '') - // const format = (info.FORMATS || info.formats).split('|') - - if (!info.MINFO) { - console.log('mInfo is undefined') - return null - } - + // console.log(rawData) + return rawData.map(item => { const types = [] const _types = {} - - let infoArr = info.MINFO.split(';') - infoArr.forEach(info => { - info = info.match(this.regExps.mInfo) - if (info) { - switch (info[2]) { - case 'flac': - types.push({ type: 'flac', size: info[3] }) - _types.flac = { - size: info[3].toLocaleUpperCase(), - } - break - case 'ape': - types.push({ type: 'ape', size: info[3] }) - _types.ape = { - size: info[3].toLocaleUpperCase(), - } - break - case 'mp3': - switch (info[1]) { - case '320': - types.push({ type: '320k', size: info[3] }) - _types['320k'] = { - size: info[3].toLocaleUpperCase(), - } - break - case '192': - types.push({ type: '192k', size: info[3] }) - _types['192k'] = { - size: info[3].toLocaleUpperCase(), - } - break - case '128': - types.push({ type: '128k', size: info[3] }) - _types['128k'] = { - size: info[3].toLocaleUpperCase(), - } - break - } - break - } + let size = null + let itemTypes = item.all_rate.split(',') + if (itemTypes.includes('128')) { + types.push({ type: '128k', size }) + _types['128k'] = { + size, } - }) - types.reverse() + } + if (itemTypes.includes('320')) { + types.push({ type: '320k', size }) + _types['320k'] = { + size, + } + } + if (itemTypes.includes('flac')) { + types.push({ type: 'flac', size }) + _types.flac = { + size, + } + } + // types.reverse() - let interval = parseInt(info.DURATION) - - result.push({ - name: decodeName(info.SONGNAME), - singer: formatSinger(decodeName(info.ARTIST)), - source: 'kw', - // img = (info.album.name === '' || info.album.name === '空') - // ? `http://player.kuwo.cn/webmusic/sj/dtflagdate?flag=6&rid=MUSIC_160911.jpg` - // : `https://y.gtimg.cn/music/photo_new/T002R500x500M000${info.album.mid}.jpg` - songmid: songId, - albumId: decodeName(info.ALBUMID || ''), - interval: Number.isNaN(interval) ? 0 : formatPlayTime(interval), - albumName: info.ALBUM ? decodeName(info.ALBUM) : '', - lyric: null, + return { + singer: item.author.replace(',', '、'), + name: item.title, + albumName: item.album_title, + albumId: item.album_id, + source: 'bd', + interval: formatPlayTime(parseInt(item.file_duration)), + songmid: item.song_id, img: null, + lrc: null, types, _types, typeUrl: {}, - }) - } - return result + } + }) }, - search(str, page = 1, { limit }) { + search(str, page = 1, { limit } = {}) { if (limit != null) this.limit = limit - // http://newlyric.kuwo.cn/newlyric.lrc?62355680 + return this.musicSearch(str, page).then(result => { - if (!result || (result.TOTAL !== '0' && result.SHOW === '0')) return this.search(str, page, { limit }) - let list = this.handleResult(result.abslist) + if (!result || result.error_code !== 22000) return this.search(str, page, { limit }) + let list = this.handleResult(result.result.song_info.song_list) if (list == null) return this.search(str, page, { limit }) - this.total = parseInt(result.TOTAL) + this.total = result.result.song_info.total this.page = page this.allPage = Math.ceil(this.total / this.limit) return Promise.resolve({ list, allPage: this.allPage, + limit: this.limit, total: this.total, + source: 'bd', }) }) }, diff --git a/src/renderer/utils/music/kg/index.js b/src/renderer/utils/music/kg/index.js index c18d1db9..e3f0e87f 100644 --- a/src/renderer/utils/music/kg/index.js +++ b/src/renderer/utils/music/kg/index.js @@ -1,10 +1,12 @@ import leaderboard from './leaderboard' import api_source from '../api-source' import songList from './songList' +import musicSearch from './musicSearch' const kg = { leaderboard, songList, + musicSearch, getMusicUrl(songInfo, type) { return api_source('kg').getMusicUrl(songInfo, type) }, diff --git a/src/renderer/utils/music/kg/musicSearch.js b/src/renderer/utils/music/kg/musicSearch.js new file mode 100644 index 00000000..fcba26f0 --- /dev/null +++ b/src/renderer/utils/music/kg/musicSearch.js @@ -0,0 +1,92 @@ +// import '../../polyfill/array.find' +// import jshtmlencode from 'js-htmlencode' +import { httpFatch } from '../../request' +import { formatPlayTime, sizeFormate } from '../../index' +// import { debug } from '../../utils/env' +// import { formatSinger } from './util' + +let searchRequest +export default { + limit: 30, + total: 0, + page: 0, + allPage: 1, + musicSearch(str, page) { + if (searchRequest && searchRequest.cancelHttp) searchRequest.cancelHttp() + searchRequest = httpFatch(`http://ioscdn.kugou.com/api/v3/search/song?keyword=${encodeURIComponent(str)}&page=${page}&pagesize=${this.limit}&showtype=10&plat=2&version=7910&tag=1&correct=1&privilege=1&sver=5`) + return searchRequest.promise.then(({ body }) => body) + }, + handleResult(rawData) { + // console.log(rawData) + let ids = new Set() + const list = [] + rawData.forEach(item => { + if (ids.has(item.audio_id)) return + ids.add(item.audio_id) + const types = [] + const _types = {} + if (item.filesize !== 0) { + let size = sizeFormate(item.filesize) + types.push({ type: '128k', size, hash: item.hash }) + _types['128k'] = { + size, + hash: item.hash, + } + } + if (item['320filesize'] !== 0) { + let size = sizeFormate(item['320filesize']) + types.push({ type: '320k', size, hash: item['320hash'] }) + _types['320k'] = { + size, + hash: item['320hash'], + } + } + if (item.sqfilesize !== 0) { + let size = sizeFormate(item.sqfilesize) + types.push({ type: 'flac', size, hash: item.sqhash }) + _types.flac = { + size, + hash: item.sqhash, + } + } + list.push({ + singer: item.singername, + name: item.songname, + albumName: item.album_name, + albumId: item.album_id, + songmid: item.audio_id, + source: 'kg', + interval: formatPlayTime(item.duration), + img: null, + lrc: null, + hash: item.hash, + types, + _types, + typeUrl: {}, + }) + }) + return list + }, + search(str, page = 1, { limit } = {}) { + if (limit != null) this.limit = limit + // http://newlyric.kuwo.cn/newlyric.lrc?62355680 + return this.musicSearch(str, page).then(result => { + if (!result || result.errcode !== 0) return this.search(str, page, { limit }) + let list = this.handleResult(result.data.info) + + if (list == null) return this.search(str, page, { limit }) + + this.total = result.data.total + this.page = page + this.allPage = Math.ceil(this.total / this.limit) + + return Promise.resolve({ + list, + allPage: this.allPage, + limit: this.limit, + total: this.total, + source: 'kg', + }) + }) + }, +}