From 908815def3f966b0322f6c9a8e614b960febe7f5 Mon Sep 17 00:00:00 2001 From: Folltoshe Date: Sat, 29 Apr 2023 15:09:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=9F=B3=E6=BA=90API=20(#135?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新API --------- Co-authored-by: lyswhut --- .../components/layout/Toolbar/SearchInput.vue | 4 +- src/renderer/utils/index.ts | 11 + src/renderer/utils/musicSdk/kg/album.js | 63 ++ src/renderer/utils/musicSdk/kg/index.js | 2 + src/renderer/utils/musicSdk/kg/leaderboard.js | 5 +- src/renderer/utils/musicSdk/kg/musicInfo.js | 104 +++ src/renderer/utils/musicSdk/kg/musicSearch.js | 7 +- src/renderer/utils/musicSdk/kg/songList.js | 53 +- .../utils/musicSdk/kg/temp/musicSearch-new.js | 112 +++ .../utils/musicSdk/kg/temp/songList-new.js | 794 ++++++++++++++++++ src/renderer/utils/musicSdk/kg/tipSearch.js | 25 + src/renderer/utils/musicSdk/kg/util.js | 33 + src/renderer/utils/musicSdk/kw/index.js | 4 +- src/renderer/utils/musicSdk/kw/leaderboard.js | 2 +- .../kw/{tempSearch.js => tipSearch.js} | 10 +- src/renderer/utils/musicSdk/mg/album.js | 61 +- src/renderer/utils/musicSdk/mg/index.js | 2 + src/renderer/utils/musicSdk/mg/leaderboard.js | 5 +- src/renderer/utils/musicSdk/mg/lyric.js | 2 +- src/renderer/utils/musicSdk/mg/musicInfo.js | 50 +- src/renderer/utils/musicSdk/mg/musicSearch.js | 10 +- src/renderer/utils/musicSdk/mg/songList.js | 22 +- .../leaderboard-old.js} | 4 +- src/renderer/utils/musicSdk/mg/tipSearch.js | 25 + src/renderer/utils/musicSdk/mg/utils/index.js | 29 + .../utils/musicSdk/mg/{ => utils}/mrc.js | 0 src/renderer/utils/musicSdk/tx/index.js | 2 + src/renderer/utils/musicSdk/tx/leaderboard.js | 10 +- src/renderer/utils/musicSdk/tx/musicSearch.js | 14 +- src/renderer/utils/musicSdk/tx/songList.js | 30 +- src/renderer/utils/musicSdk/tx/tipSearch.js | 15 +- src/renderer/utils/musicSdk/utils.js | 27 + src/renderer/utils/musicSdk/wy/index.js | 2 + src/renderer/utils/musicSdk/wy/leaderboard.js | 30 +- src/renderer/utils/musicSdk/wy/songList.js | 27 +- src/renderer/utils/musicSdk/wy/tipSearch.js | 33 + 36 files changed, 1422 insertions(+), 207 deletions(-) create mode 100644 src/renderer/utils/musicSdk/kg/album.js create mode 100644 src/renderer/utils/musicSdk/kg/musicInfo.js create mode 100644 src/renderer/utils/musicSdk/kg/temp/musicSearch-new.js create mode 100644 src/renderer/utils/musicSdk/kg/temp/songList-new.js create mode 100644 src/renderer/utils/musicSdk/kg/tipSearch.js rename src/renderer/utils/musicSdk/kw/{tempSearch.js => tipSearch.js} (79%) rename src/renderer/utils/musicSdk/mg/{leaderboard2.js => temp/leaderboard-old.js} (98%) create mode 100644 src/renderer/utils/musicSdk/mg/tipSearch.js create mode 100644 src/renderer/utils/musicSdk/mg/utils/index.js rename src/renderer/utils/musicSdk/mg/{ => utils}/mrc.js (100%) create mode 100644 src/renderer/utils/musicSdk/wy/tipSearch.js diff --git a/src/renderer/components/layout/Toolbar/SearchInput.vue b/src/renderer/components/layout/Toolbar/SearchInput.vue index 24da7200..ac2ef4e7 100644 --- a/src/renderer/components/layout/Toolbar/SearchInput.vue +++ b/src/renderer/components/layout/Toolbar/SearchInput.vue @@ -48,12 +48,12 @@ export default { const tipSearch = debounce(async() => { if (searchText.value === '' && prevTempSearchSource) { tipList.value = [] - music[prevTempSearchSource].tempSearch.cancelTempSearch() + music[prevTempSearchSource].tipSearch.cancelTipSearch() return } const { temp_source } = await getSearchSetting() prevTempSearchSource = temp_source - music[prevTempSearchSource].tempSearch.search(searchText.value).then(list => { + music[prevTempSearchSource].tipSearch.search(searchText.value).then(list => { tipList.value = list }).catch(() => {}) }, 50) diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index efdfa182..fe4adf76 100644 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -5,6 +5,17 @@ export * from '@common/utils/nodejs' export * from '@common/utils/common' export * from '@common/utils/tools' +/** + * 格式化播放数量 + * @param {*} num 数字 + */ +export const formatPlayCount = (num: number): string => { + if (num > 100000000) return `${Math.trunc(num / 10000000) / 10}亿` + if (num > 10000) return `${Math.trunc(num / 1000) / 10}万` + return String(num) +} + + /** * 时间格式化 */ diff --git a/src/renderer/utils/musicSdk/kg/album.js b/src/renderer/utils/musicSdk/kg/album.js new file mode 100644 index 00000000..1d9642f8 --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/album.js @@ -0,0 +1,63 @@ +import { getMusicInfosByList } from './musicInfo' +import { createHttpFetch } from './util' + +export default { + /** + * 通过AlbumId获取专辑信息 + * @param {*} id + */ + async getAlbumInfo(id) { + const albumInfoRequest = await createHttpFetch('http://kmrserviceretry.kugou.com/container/v1/album?dfid=1tT5He3kxrNC4D29ad1MMb6F&mid=22945702112173152889429073101964063697&userid=0&appid=1005&clientver=11589', { + method: 'POST', + body: { + appid: 1005, + clienttime: 1681833686, + clientver: 11589, + data: [{ album_id: id }], + fields: 'language,grade_count,intro,mix_intro,heat,category,sizable_cover,cover,album_name,type,quality,publish_company,grade,special_tag,author_name,publish_date,language_id,album_id,exclusive,is_publish,trans_param,authors,album_tag', + isBuy: 0, + key: 'e6f3306ff7e2afb494e89fbbda0becbf', + mid: '22945702112173152889429073101964063697', + show_album_tag: 0, + }, + }) + if (!albumInfoRequest) return Promise.reject(new Error('get album info failed.')) + const albumInfo = albumInfoRequest[0] + + return { + name: albumInfo.album_name, + image: albumInfo.sizable_cover.replace('{size}', 240), + desc: albumInfo.intro, + authorName: albumInfo.author_name, + // play_count: this.formatPlayCount(info.count), + } + }, + /** + * 通过AlbumId获取专辑 + * @param {*} id + * @param {*} page + */ + async getAlbumDetail(id, page = 1, limit = 200) { + const albumList = await createHttpFetch(`http://mobiles.kugou.com/api/v3/album/song?version=9108&albumid=${id}&plat=0&pagesize=${limit}&area_code=0&page=${page}&with_res_tag=0`) + if (!albumList.info) return Promise.reject(new Error('Get album list failed.')) + + let result = await getMusicInfosByList(albumList.info) + + const info = await this.getAlbumInfo(id) + + return { + list: result || [], + page, + limit, + total: albumList.total, + source: 'kg', + info: { + name: info.name, + img: info.image, + desc: info.desc, + author: info.authorName, + // play_count: this.formatPlayCount(info.count), + }, + } + }, +} diff --git a/src/renderer/utils/musicSdk/kg/index.js b/src/renderer/utils/musicSdk/kg/index.js index 4d4ac3da..5d41846c 100644 --- a/src/renderer/utils/musicSdk/kg/index.js +++ b/src/renderer/utils/musicSdk/kg/index.js @@ -6,8 +6,10 @@ import pic from './pic' import lyric from './lyric' import hotSearch from './hotSearch' import comment from './comment' +// import tipSearch from './tipSearch' const kg = { + // tipSearch, leaderboard, songList, musicSearch, diff --git a/src/renderer/utils/musicSdk/kg/leaderboard.js b/src/renderer/utils/musicSdk/kg/leaderboard.js index 1e6ad109..d9821f29 100644 --- a/src/renderer/utils/musicSdk/kg/leaderboard.js +++ b/src/renderer/utils/musicSdk/kg/leaderboard.js @@ -1,5 +1,6 @@ import { httpFetch } from '../../request' import { decodeName, formatPlayTime, sizeFormate } from '../../index' +import { formatSingerName } from '../utils' let boardList = [{ id: 'kg__8888', name: 'TOP500', bangid: '8888' }, { id: 'kg__6666', name: '飙升榜', bangid: '6666' }, { id: 'kg__59703', name: '蜂鸟流行音乐榜', bangid: '59703' }, { id: 'kg__52144', name: '抖音热歌榜', bangid: '52144' }, { id: 'kg__52767', name: '快手热歌榜', bangid: '52767' }, { id: 'kg__24971', name: 'DJ热歌榜', bangid: '24971' }, { id: 'kg__23784', name: '网络红歌榜', bangid: '23784' }, { id: 'kg__44412', name: '说唱先锋榜', bangid: '44412' }, { id: 'kg__31308', name: '内地榜', bangid: '31308' }, { id: 'kg__33160', name: '电音榜', bangid: '33160' }, { id: 'kg__31313', name: '香港地区榜', bangid: '31313' }, { id: 'kg__51341', name: '民谣榜', bangid: '51341' }, { id: 'kg__54848', name: '台湾地区榜', bangid: '54848' }, { id: 'kg__31310', name: '欧美榜', bangid: '31310' }, { id: 'kg__33162', name: 'ACG新歌榜', bangid: '33162' }, { id: 'kg__31311', name: '韩国榜', bangid: '31311' }, { id: 'kg__31312', name: '日本榜', bangid: '31312' }, { id: 'kg__49225', name: '80后热歌榜', bangid: '49225' }, { id: 'kg__49223', name: '90后热歌榜', bangid: '49223' }, { id: 'kg__49224', name: '00后热歌榜', bangid: '49224' }, { id: 'kg__33165', name: '粤语金曲榜', bangid: '33165' }, { id: 'kg__33166', name: '欧美金曲榜', bangid: '33166' }, { id: 'kg__33163', name: '影视金曲榜', bangid: '33163' }, { id: 'kg__51340', name: '伤感榜', bangid: '51340' }, { id: 'kg__35811', name: '会员专享榜', bangid: '35811' }, { id: 'kg__37361', name: '雷达榜', bangid: '37361' }, { id: 'kg__21101', name: '分享榜', bangid: '21101' }, { id: 'kg__46910', name: '综艺新歌榜', bangid: '46910' }, { id: 'kg__30972', name: '酷狗音乐人原创榜', bangid: '30972' }, { id: 'kg__60170', name: '闽南语榜', bangid: '60170' }, { id: 'kg__65234', name: '儿歌榜', bangid: '65234' }, { id: 'kg__4681', name: '美国BillBoard榜', bangid: '4681' }, { id: 'kg__25028', name: 'Beatport电子舞曲榜', bangid: '25028' }, { id: 'kg__4680', name: '英国单曲榜', bangid: '4680' }, { id: 'kg__38623', name: '韩国Melon音乐榜', bangid: '38623' }, { id: 'kg__42807', name: 'joox本地热歌榜', bangid: '42807' }, { id: 'kg__36107', name: '小语种热歌榜', bangid: '36107' }, { id: 'kg__4673', name: '日本公信榜', bangid: '4673' }, { id: 'kg__46868', name: '日本SPACE SHOWER榜', bangid: '46868' }, { id: 'kg__42808', name: 'KKBOX风云榜', bangid: '42808' }, { id: 'kg__60171', name: '越南语榜', bangid: '60171' }, { id: 'kg__60172', name: '泰语榜', bangid: '60172' }, { id: 'kg__59895', name: 'R&B榜', bangid: '59895' }, { id: 'kg__59896', name: '摇滚榜', bangid: '59896' }, { id: 'kg__59897', name: '爵士榜', bangid: '59897' }, { id: 'kg__59898', name: '乡村音乐榜', bangid: '59898' }, { id: 'kg__59900', name: '纯音乐榜', bangid: '59900' }, { id: 'kg__59899', name: '古典榜', bangid: '59899' }, { id: 'kg__22603', name: '5sing音乐榜', bangid: '22603' }, { id: 'kg__21335', name: '繁星音乐榜', bangid: '21335' }, { id: 'kg__33161', name: '古风新歌榜', bangid: '33161' }] @@ -8,7 +9,7 @@ export default { list: [ { id: 'kgtop500', - name: '酷狗TOP500', + name: 'TOP500', bangid: '8888', }, { @@ -126,7 +127,7 @@ export default { } } return { - singer: decodeName(this.getSinger(item.authors)), + singer: formatSingerName(item.authors, 'author_name'), name: decodeName(item.songname), albumName: decodeName(item.remark), albumId: item.album_id, diff --git a/src/renderer/utils/musicSdk/kg/musicInfo.js b/src/renderer/utils/musicSdk/kg/musicInfo.js new file mode 100644 index 00000000..546ad8b4 --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/musicInfo.js @@ -0,0 +1,104 @@ +import { decodeName, formatPlayTime, sizeFormate } from '../../index' +import { createHttpFetch } from './util' + +const createGetMusicInfosTask = (hashs) => { + let data = { + appid: 1001, + clienttime: 639437935, + clientver: 9020, + fields: 'album_info,author_name,audio_info,ori_audio_name', + is_publish: '1', + key: '0475af1457cd3363c7b45b871e94428a', + mid: '21511157a05844bd085308bc76ef3342', + show_privilege: 1, + } + let list = hashs + let tasks = [] + while (list.length) { + tasks.push(Object.assign({ data: list.slice(0, 100) }, data)) + if (list.length < 100) break + list = list.slice(100) + } + let url = 'http://kmr.service.kugou.com/v2/album_audio/audio' + return tasks.map(task => createHttpFetch(url, { + method: 'POST', + body: task, + 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', + }, + }).then(data => data.map(s => s[0]))) +} + +export const filterMusicInfoList = (rawList) => { + // console.log(rawList) + let ids = new Set() + let list = [] + rawList.forEach(item => { + if (!item) return + if (ids.has(item.audio_info.audio_id)) return + ids.add(item.audio_info.audio_id) + const types = [] + const _types = {} + if (item.audio_info.filesize !== '0') { + let size = sizeFormate(parseInt(item.audio_info.filesize)) + types.push({ type: '128k', size, hash: item.audio_info.hash }) + _types['128k'] = { + size, + hash: item.audio_info.hash, + } + } + if (item.audio_info.filesize_320 !== '0') { + let size = sizeFormate(parseInt(item.audio_info.filesize_320)) + types.push({ type: '320k', size, hash: item.audio_info.hash_320 }) + _types['320k'] = { + size, + hash: item.audio_info.hash_320, + } + } + if (item.audio_info.filesize_flac !== '0') { + let size = sizeFormate(parseInt(item.audio_info.filesize_flac)) + types.push({ type: 'flac', size, hash: item.audio_info.hash_flac }) + _types.flac = { + size, + hash: item.audio_info.hash_flac, + } + } + if (item.audio_info.filesize_high !== '0') { + let size = sizeFormate(parseInt(item.audio_info.filesize_high)) + types.push({ type: 'flac24bit', size, hash: item.audio_info.hash_high }) + _types.flac24bit = { + size, + hash: item.audio_info.hash_high, + } + } + list.push({ + singer: decodeName(item.author_name), + name: decodeName(item.ori_audio_name), + albumName: decodeName(item.album_info.album_name), + albumId: item.album_info.album_id, + songmid: item.audio_info.audio_id, + source: 'kg', + interval: formatPlayTime(parseInt(item.audio_info.timelength) / 1000), + img: null, + lrc: null, + hash: item.audio_info.hash, + otherSource: null, + types, + _types, + typeUrl: {}, + }) + }) + return list +} + +export const getMusicInfos = async(hashs) => { + return filterMusicInfoList(await Promise.all(createGetMusicInfosTask(hashs)).then(data => data.flat())) +} + +export const getMusicInfo = async(hash) => { + return getMusicInfos([hash]).then(data => data[0]) +} + +export const getMusicInfosByList = (list) => { + return getMusicInfos(list.map(item => ({ hash: item.hash }))) +} diff --git a/src/renderer/utils/musicSdk/kg/musicSearch.js b/src/renderer/utils/musicSdk/kg/musicSearch.js index 66b15edd..ee60a694 100644 --- a/src/renderer/utils/musicSdk/kg/musicSearch.js +++ b/src/renderer/utils/musicSdk/kg/musicSearch.js @@ -1,9 +1,6 @@ -// import '../../polyfill/array.find' - import { httpFetch } from '../../request' import { decodeName, formatPlayTime, sizeFormate } from '../../index' -// import { debug } from '../../utils/env' -// import { formatSinger } from './util' +import { formatSingerName } from '@renderer/utils/musicSdk/utils' export default { limit: 30, @@ -50,7 +47,7 @@ export default { } } return { - singer: decodeName(rawData.SingerName), + singer: decodeName(formatSingerName(rawData.Singers, 'name')), name: decodeName(rawData.SongName), albumName: decodeName(rawData.AlbumName), albumId: rawData.AlbumID, diff --git a/src/renderer/utils/musicSdk/kg/songList.js b/src/renderer/utils/musicSdk/kg/songList.js index 836be49e..f06163dd 100644 --- a/src/renderer/utils/musicSdk/kg/songList.js +++ b/src/renderer/utils/musicSdk/kg/songList.js @@ -1,5 +1,5 @@ import { httpFetch } from '../../request' -import { decodeName, formatPlayTime, sizeFormate, dateFormat } from '../../index' +import { decodeName, formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../../index' import infSign from './vendors/infSign.min' import { signatureParams } from './util' @@ -52,17 +52,17 @@ export default { // https://www.kugou.com/yy/special/single/1067062.html listDetailLink: /^.+\/(\d+)\.html(?:\?.*|&.*$|#.*$|$)/, }, - async getGlobalSpecialId(specialId) { - return httpFetch(`http://mobilecdnbj.kugou.com/api/v5/special/info?specialid=${specialId}`, { - headers: { - 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; HLK-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Mobile Safari/537.36 EdgA/104.0.1293.70', - }, - }).promise.then(({ body }) => { - // console.log(body) - if (!body.data.global_specialid) Promise.reject(new Error('Failed to get global collection id.')) - return body.data.global_specialid - }) - }, + // async getGlobalSpecialId(specialId) { + // return httpFetch(`http://mobilecdnbj.kugou.com/api/v5/special/info?specialid=${specialId}`, { + // headers: { + // 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; HLK-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Mobile Safari/537.36 EdgA/104.0.1293.70', + // }, + // }).promise.then(({ body }) => { + // // console.log(body) + // if (!body.data.global_specialid) Promise.reject(new Error('Failed to get global collection id.')) + // return body.data.global_specialid + // }) + // }, // async getListInfoBySpecialId(special_id, retry = 0) { // if (++retry > 2) throw new Error('failed') // return httpFetch(`https://m.kugou.com/plist/list/${special_id}/?json=true`, { @@ -135,7 +135,7 @@ export default { img: pic, desc, // author: body.result.info.userinfo.username, - // play_count: this.formatPlayCount(body.result.listen_num), + // play_count: formatPlayCount(body.result.listen_num), }, } @@ -155,7 +155,7 @@ export default { // img: listInfo.image, // desc: listInfo.intro, // author: listInfo.author, - // play_count: this.formatPlayCount(listInfo.playcount), + // play_count: formatPlayCount(listInfo.playcount), // }, // } }, @@ -172,15 +172,6 @@ export default { return `http://www2.kugou.kugou.com/yueku/v9/special/single/${id}-5-9999.html` }, - /** - * 格式化播放数量 - * @param {*} num - */ - formatPlayCount(num) { - if (num > 100000000) return parseInt(num / 10000000) / 10 + '亿' - if (num > 10000) return parseInt(num / 1000) / 10 + '万' - return num - }, filterInfoHotTag(rawData) { const result = [] if (rawData.status !== 1) return result @@ -252,7 +243,7 @@ export default { }, filterList(rawData) { return rawData.map(item => ({ - play_count: item.total_play_count || this.formatPlayCount(item.play_count), + play_count: item.total_play_count || formatPlayCount(item.play_count), id: 'id_' + item.specialid, author: item.nickname, name: item.specialname, @@ -374,7 +365,7 @@ export default { img: (info.img_size && info.img_size.replace('{size}', 240)) || info.img, // desc: body.result.info.list_desc, author: info.username, - // play_count: this.formatPlayCount(info.count), + // play_count: formatPlayCount(info.count), }, } }, @@ -402,7 +393,7 @@ export default { img: songInfo.info.img, // desc: body.result.info.list_desc, author: songInfo.info.username, - // play_count: this.formatPlayCount(info.count), + // play_count: formatPlayCount(info.count), }, } }, @@ -446,7 +437,7 @@ export default { img: listInfo.pic && listInfo.pic.replace('{size}', 240), // desc: body.result.info.list_desc, author: listInfo.list_create_username, - // play_count: this.formatPlayCount(listInfo.count), + // play_count: formatPlayCount(listInfo.count), }, } }, @@ -497,7 +488,7 @@ export default { img: info.imgurl && info.imgurl.replace('{size}', 240), desc: info.intro, author: info.nickname, - play_count: this.formatPlayCount(info.playcount), + play_count: formatPlayCount(info.playcount), }, } }, @@ -548,7 +539,7 @@ export default { img: listInfo.imgurl && listInfo.imgurl.replace('{size}', 240), // desc: body.result.info.list_desc, author: listInfo.nickname, - // play_count: this.formatPlayCount(info.count), + // play_count: formatPlayCount(info.count), }, } }, @@ -569,7 +560,7 @@ export default { img: listInfo.imgurl && listInfo.imgurl.replace('{size}', 240), // desc: body.result.info.list_desc, author: listInfo.nickname, - // play_count: this.formatPlayCount(info.count), + // play_count: formatPlayCount(info.count), }, } }, @@ -905,7 +896,7 @@ export default { return { list: body.data.info.map(item => { return { - play_count: this.formatPlayCount(item.playcount), + play_count: formatPlayCount(item.playcount), id: 'id_' + item.specialid, author: item.nickname, name: item.specialname, diff --git a/src/renderer/utils/musicSdk/kg/temp/musicSearch-new.js b/src/renderer/utils/musicSdk/kg/temp/musicSearch-new.js new file mode 100644 index 00000000..535fc790 --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/temp/musicSearch-new.js @@ -0,0 +1,112 @@ +import { formatSingerName } from '@renderer/utils/musicSdk/utils' +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(formatSingerName(item.Singers)), + name: decodeName(item.SongName), + 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', + }) + }) + }, +} diff --git a/src/renderer/utils/musicSdk/kg/temp/songList-new.js b/src/renderer/utils/musicSdk/kg/temp/songList-new.js new file mode 100644 index 00000000..389e93c1 --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/temp/songList-new.js @@ -0,0 +1,794 @@ +import { httpFetch } from '../../../request' +import { formatSingerName } from '../../utils' +import { decodeName, formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../../../index' +import { signatureParams, createHttpFetch } from './../util' +import { getMusicInfosByList } from '../musicInfo' +import album from '../album' + +export default { + _requestObj_tags: null, + _requestObj_listInfo: null, + _requestObj_list: null, + _requestObj_listRecommend: null, + listDetailLimit: 10000, + currentTagInfo: { + id: undefined, + info: undefined, + }, + sortList: [ + { + name: '推荐', + id: '5', + }, + { + name: '最热', + id: '6', + }, + { + name: '最新', + id: '7', + }, + { + name: '热藏', + id: '3', + }, + { + name: '飙升', + id: '8', + }, + ], + cache: new Map(), + collectionIdListInfoCache: new Map(), + regExps: { + listData: /global\.data = (\[.+\]);/, + listInfo: /global = {[\s\S]+?name: "(.+)"[\s\S]+?pic: "(.+)"[\s\S]+?};/, + // https://www.kugou.com/yy/special/single/1067062.html + listDetailLink: /^.+\/(\d+)\.html(?:\?.*|&.*$|#.*$|$)/, + }, + + /** + * 获取歌曲列表内的音乐 + * @param {*} id + * @param {*} page + */ + async getListDetail(id, page) { + id = id.toString() + + if (id.includes('special/single/')) id = id.replace(this.regExps.listDetailLink, '$1') + // fix https://www.kugou.com/songlist/xxx/?uid=xxx&chl=qq_client&cover=http%3A%2F%2Fimge.kugou.com%xxx.jpg&iszlist=1 + if (/https?:/.test(id)) { + if (id.includes('#')) id = id.replace(/#.*$/, '') + if (id.includes('global_collection_id')) return this.getUserListDetailByCollectionId(id.replace(/^.*?global_collection_id=(\w+)(?:&.*$|#.*$|$)/, '$1'), page) + if (id.includes('chain=')) return this.getUserListDetail3(id.replace(/^.*?chain=(\w+)(?:&.*$|#.*$|$)/, '$1'), page) + if (id.includes('.html')) { + if (id.includes('zlist.html')) { + id = id.replace(/^(.*)zlist\.html/, 'https://m3ws.kugou.com/zlist/list') + if (id.includes('pagesize')) { + id = id.replace('pagesize=30', 'pagesize=' + this.listDetailLimit).replace('page=1', 'page=' + page) + } else { + id += `&pagesize=${this.listDetailLimit}&page=${page}` + } + } else if (!id.includes('song.html')) return this.getUserListDetail3(id.replace(/.+\/(\w+).html(?:\?.*|&.*$|#.*$|$)/, '$1'), page) + } + return this.getUserListDetail(id.replace(/^.*?http/, 'http'), page) + } + if (/^\d+$/.test(id)) return this.getUserListDetailByCode(id, page) + if (id.startsWith('gid_')) return this.getUserListDetailByCollectionId(id.replace('gid_', ''), page) + if (id.startsWith('id_')) return this.getUserListDetailBySpecialId(id.replace('id_', ''), page) + + return new Error('Failed.') + }, + + /** + * 获取SpecialId歌单 + * @param {*} id + */ + async getUserListDetailBySpecialId(id, page, tryNum = 0) { + if (tryNum > 2) throw new Error('try max num') + + const { body } = await httpFetch(this.getSongListDetailUrl(id)).promise + let listData = body.match(this.regExps.listData) + let listInfo = body.match(this.regExps.listInfo) + if (!listData) return this.getListDetailBySpecialId(id, page, ++tryNum) + let list = await getMusicInfosByList(JSON.parse(listData[1])) + let name + let pic + if (listInfo) { + name = listInfo[1] + pic = listInfo[2] + } + let desc = this.parseHtmlDesc(body) + + + return { + list, + page: 1, + limit: 10000, + total: list.length, + source: 'kg', + info: { + name, + img: pic, + desc, + // author: body.result.info.userinfo.username, + // play_count: formatPlayCount(body.result.listen_num), + }, + } + }, + parseHtmlDesc(html) { + const prefix = '
' + let index = html.indexOf(prefix) + if (index < 0) return null + const afterStr = html.substring(index + prefix.length) + index = afterStr.indexOf('
') + if (index < 0) return null + return decodeName(afterStr.substring(0, index)) + }, + + /** + * 使用SpecialId获取CollectionId + * @param {*} specialId + */ + async getCollectionIdBySpecialId(specialId) { + return httpFetch(`http://mobilecdnbj.kugou.com/api/v5/special/info?specialid=${specialId}`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; HLK-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Mobile Safari/537.36 EdgA/104.0.1293.70', + }, + }).promise.then(({ body }) => { + // console.log('getCollectionIdBySpecialId', body) + if (!body.data.global_specialid) return Promise.reject(new Error('Failed to get global collection id.')) + return body.data.global_specialid + }) + }, + + /** + * 获取歌单URL + * @param {*} sortId + * @param {*} tagId + * @param {*} page + */ + getSongListUrl(sortId, tagId, page) { + if (tagId == null) tagId = '' + return `http://www2.kugou.kugou.com/yueku/v9/special/getSpecial?is_ajax=1&cdn=cdn&t=${sortId}&c=${tagId}&p=${page}` + }, + getInfoUrl(tagId) { + return tagId + ? `http://www2.kugou.kugou.com/yueku/v9/special/getSpecial?is_smarty=1&cdn=cdn&t=5&c=${tagId}` + : 'http://www2.kugou.kugou.com/yueku/v9/special/getSpecial?is_smarty=1&' + }, + getSongListDetailUrl(id) { + return `http://www2.kugou.kugou.com/yueku/v9/special/single/${id}-5-9999.html` + }, + + filterInfoHotTag(rawData) { + const result = [] + if (rawData.status !== 1) return result + for (const key of Object.keys(rawData.data)) { + let tag = rawData.data[key] + result.push({ + id: tag.special_id, + name: tag.special_name, + source: 'kg', + }) + } + return result + }, + + filterTagInfo(rawData) { + const result = [] + for (const name of Object.keys(rawData)) { + result.push({ + name, + list: rawData[name].data.map(tag => ({ + parent_id: tag.parent_id, + parent_name: tag.pname, + id: tag.id, + name: tag.name, + source: 'kg', + })), + }) + } + return result + }, + filterSongList(rawData) { + return rawData.map(item => ({ + play_count: item.total_play_count || formatPlayCount(item.play_count), + id: 'id_' + item.specialid, + author: item.nickname, + name: item.specialname, + time: dateFormat(item.publish_time || item.publishtime, 'Y-M-D'), + img: item.img || item.imgurl, + total: item.songcount, + grade: item.grade, + desc: item.intro, + source: 'kg', + })) + }, + + getSongList(sortId, tagId, page, tryNum = 0) { + if (this._requestObj_list) this._requestObj_list.cancelHttp() + if (tryNum > 2) return Promise.reject(new Error('try max num')) + this._requestObj_list = httpFetch( + this.getSongListUrl(sortId, tagId, page), + ) + return this._requestObj_list.promise.then(({ body }) => { + if (!body || body.status !== 1) return this.getSongList(sortId, tagId, page, ++tryNum) + return this.filterSongList(body.special_db) + }) + }, + getSongListRecommend(tryNum = 0) { + if (this._requestObj_listRecommend) this._requestObj_listRecommend.cancelHttp() + if (tryNum > 2) return Promise.reject(new Error('try max num')) + this._requestObj_listRecommend = httpFetch( + 'http://everydayrec.service.kugou.com/guess_special_recommend', + { + method: 'post', + headers: { + 'User-Agent': 'KuGou2012-8275-web_browser_event_handler', + }, + body: { + appid: 1001, + clienttime: 1566798337219, + clientver: 8275, + key: 'f1f93580115bb106680d2375f8032d96', + mid: '21511157a05844bd085308bc76ef3343', + platform: 'pc', + userid: '262643156', + return_min: 6, + return_max: 15, + }, + }, + ) + return this._requestObj_listRecommend.promise.then(({ body }) => { + if (body.status !== 1) return this.getSongListRecommend(++tryNum) + return this.filterSongList(body.data.special_list) + }) + }, + + /** + * 通过CollectionId获取歌单详情 + * @param {*} id + */ + async getUserListInfoByCollectionId(id) { + if (!id || id.length > 1000) return Promise.reject(new Error('get list error')) + if (this.collectionIdListInfoCache.has(id)) return this.collectionIdListInfoCache.get(id) + + const params = `appid=1058&specialid=0&global_specialid=${id}&format=jsonp&srcappid=2919&clientver=20000&clienttime=1586163242519&mid=1586163242519&uuid=1586163242519&dfid=-` + return createHttpFetch(`https://mobiles.kugou.com/api/v5/special/info_v2?${params}&signature=${signatureParams(params, 5)}`, { + headers: { + mid: '1586163242519', + Referer: 'https://m3ws.kugou.com/share/index.php', + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + dfid: '-', + clienttime: '1586163242519', + }, + }).then(body => { + let info = { + type: body.type, + userName: body.nickname, + userAvatar: body.user_avatar, + imageUrl: body.imgurl, + desc: body.intro, + name: body.specialname, + globalSpecialid: body.global_specialid, + total: body.songcount, + playCount: body.playcount, + } + + this.collectionIdListInfoCache.set(id, info) + return info + }) + }, + /** + * 通过SpecialId获取歌单 + * @param {*} id + */ + // async getUserListDetailBySpecialId(id, page = 1, limit = 300) { + // if (!id || id.length > 1000) return Promise.reject(new Error('get list error.')) + // const listInfo = await this.getListInfoBySpecialId(id) + + // const params = `specialid=${id}&need_sort=1&module=CloudMusic&clientver=11589&pagesize=${limit}&userid=0&page=${page}&type=0&area_code=1&appid=1005` + // return createHttpFetch(`http://pubsongs.kugou.com/v2/get_other_list_file?${params}&signature=${signatureParams(params, 2)}`, { + // headers: { + // 'User-Agent': 'Android10-AndroidPhone-11589-201-0-playlist-wifi', + // }, + // }).then(body => { + // if (!body.info) return Promise.reject(new Error('Get list failed.')) + // const songList = this.filterListByCollectionId(body.info) + + // return { + // list: songList || [], + // page, + // limit, + // total: body.count, + // source: 'kg', + // info: { + // name: listInfo.name, + // img: listInfo.image, + // desc: listInfo.desc, + // // author: listInfo.userName, + // // play_count: formatPlayCount(listInfo.playCount), + // }, + // } + // }) + // }, + /** + * 通过CollectionId获取歌单 + * @param {*} id + */ + async getUserListDetailByCollectionId(id, page = 1, limit = 300) { + if (!id || id.length > 1000) return Promise.reject(new Error('ID error.')) + const listInfo = await this.getUserListInfoByCollectionId(id) + + const params = `need_sort=1&module=CloudMusic&clientver=11589&pagesize=${limit}&global_collection_id=${id}&userid=0&page=${page}&type=0&area_code=1&appid=1005` + return createHttpFetch(`http://pubsongs.kugou.com/v2/get_other_list_file?${params}&signature=${signatureParams(params, 2)}`, { + headers: { + 'User-Agent': 'Android10-AndroidPhone-11589-201-0-playlist-wifi', + }, + }).then(body => { + if (!body.info) return Promise.reject(new Error('Get list failed.')) + const songList = this.filterListByCollectionId(body.info) + + return { + list: songList || [], + page, + limit, + total: listInfo.total, + source: 'kg', + info: { + name: listInfo.name, + img: listInfo.imageUrl && listInfo.imageUrl.replace('{size}', 240), + desc: listInfo.desc, + author: listInfo.userName, + play_count: formatPlayCount(listInfo.playCount), + }, + } + }) + }, + /** + * 过滤GlobalSpecialId歌单数据 + * @param {*} rawData + */ + filterListByCollectionId(rawData) { + let ids = new Set() + let list = [] + rawData.forEach(item => { + if (!item) return + if (ids.has(item.hash)) return + ids.add(item.hash) + const types = [] + const _types = {} + + item.relate_goods.forEach(data => { + let size = sizeFormate(data.size) + switch (data.level) { + case 2: + types.push({ type: '128k', size, hash: data.hash }) + _types['128k'] = { + size, + hash: data.hash, + } + break + case 4: + types.push({ type: '320k', size, hash: data.hash }) + _types['320k'] = { + size, + hash: data.hash, + } + break + case 5: + types.push({ type: 'flac', size, hash: data.hash }) + _types.flac = { + size, + hash: data.hash, + } + break + case 6: + types.push({ type: 'flac24bit', size, hash: data.hash }) + _types.flac24bit = { + size, + hash: data.hash, + } + break + } + }) + + list.push({ + singer: formatSingerName(item.singerinfo, 'name') || decodeName(item.name).split(' - ')[0].replace(/&/g, '、'), + name: decodeName(item.name).split(' - ')[1], + albumName: decodeName(item.albuminfo.name), + albumId: item.albuminfo.id, + songmid: item.audio_id, + source: 'kg', + interval: formatPlayTime(parseInt(item.timelen) / 1000), + img: null, + lrc: null, + hash: item.hash, + otherSource: null, + types, + _types, + typeUrl: {}, + }) + }) + return list + }, + /** + * 通过酷狗码获取歌单 + * @param {*} id + * @param {*} page + */ + async getUserListDetailByCode(id, page = 1) { + // type 1单曲,2歌单,3电台,4酷狗码,5别人的播放队列 + const codeData = await createHttpFetch('http://t.kugou.com/command/', { + method: 'POST', + headers: { + 'KG-RC': 1, + 'KG-THash': 'network_super_call.cpp:3676261689:379', + 'User-Agent': '', + }, + body: { appid: 1001, clientver: 9020, mid: '21511157a05844bd085308bc76ef3343', clienttime: 640612895, key: '36164c4015e704673c588ee202b9ecb8', data: id }, + }) + if (!codeData) return Promise.reject(new Error('Get list failed.')) + const codeInfo = codeData.info + + switch (codeInfo.type) { + case 2: + if (!codeInfo.global_collection_id) return this.getUserListDetailBySpecialId(codeInfo.id, page) + break + case 3: + return album.getAlbumDetail(codeInfo.id, page) + } + if (codeInfo.global_collection_id) return this.getUserListDetailByCollectionId(codeInfo.global_collection_id, page) + + if (codeInfo.userid != null) { + const songList = await createHttpFetch('http://www2.kugou.kugou.com/apps/kucodeAndShare/app/', { + method: 'POST', + headers: { + 'KG-RC': 1, + 'KG-THash': 'network_super_call.cpp:3676261689:379', + 'User-Agent': '', + }, + body: { appid: 1001, clientver: 9020, mid: '21511157a05844bd085308bc76ef3343', clienttime: 640612895, key: '36164c4015e704673c588ee202b9ecb8', data: { id: codeInfo.id, type: 3, userid: codeInfo.userid, collect_type: 0, page: 1, pagesize: codeInfo.count } }, + }) + // console.log(songList) + let list = await getMusicInfosByList(songList || codeInfo.list) + return { + list, + page: 1, + limit: codeInfo.count, + total: list.length, + source: 'kg', + info: { + name: codeInfo.name, + img: (codeInfo.img_size && codeInfo.img_size.replace('{size}', 240)) || codeInfo.img, + // desc: body.result.info.list_desc, + author: codeInfo.username, + // play_count: formatPlayCount(info.count), + }, + } + } + }, + + async getUserListDetail3(chain, page) { + const songInfo = await createHttpFetch(`http://m.kugou.com/schain/transfer?pagesize=${this.listDetailLimit}&chain=${chain}&su=1&page=${page}&n=0.7928855356604456`, { + 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', + }, + }) + if (!songInfo.list) { + if (songInfo.global_collection_id) return this.getUserListDetailByCollectionId(songInfo.global_collection_id, page) + else return this.getUserListDetail4(songInfo, chain, page).catch(() => this.getUserListDetail5(chain)) + } + let list = await getMusicInfosByList(songInfo.list) + // console.log(info, songInfo) + return { + list, + page: 1, + limit: this.listDetailLimit, + total: list.length, + source: 'kg', + info: { + name: songInfo.info.name, + img: songInfo.info.img, + // desc: body.result.info.list_desc, + author: songInfo.info.username, + // play_count: formatPlayCount(info.count), + }, + } + }, + + async getUserListDetailByLink({ info }, link) { + let listInfo = info['0'] + let total = listInfo.count + let tasks = [] + let page = 0 + while (total) { + const limit = total > 90 ? 90 : total + total -= limit + page += 1 + tasks.push(createHttpFetch(link.replace(/pagesize=\d+/, 'pagesize=' + limit).replace(/page=\d+/, 'page=' + page), { + 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: link, + }, + }).then(data => data.list.info)) + } + let result = await Promise.all(tasks).then(([...datas]) => datas.flat()) + result = await getMusicInfosByList(result) + // console.log(result) + return { + list: result, + page, + limit: this.listDetailLimit, + total: result.length, + source: 'kg', + info: { + name: listInfo.name, + img: listInfo.pic && listInfo.pic.replace('{size}', 240), + // desc: body.result.info.list_desc, + author: listInfo.list_create_username, + // play_count: formatPlayCount(listInfo.count), + }, + } + }, + createGetListDetail2Task(id, total) { + let tasks = [] + let page = 0 + while (total) { + const limit = total > 300 ? 300 : total + total -= limit + page += 1 + const params = 'appid=1058&global_specialid=' + id + '&specialid=0&plat=0&version=8000&page=' + page + '&pagesize=' + limit + '&srcappid=2919&clientver=20000&clienttime=1586163263991&mid=1586163263991&uuid=1586163263991&dfid=-' + tasks.push(createHttpFetch(`https://mobiles.kugou.com/api/v5/special/song_v2?${params}&signature=${signatureParams(params, 5)}`, { + headers: { + mid: '1586163263991', + Referer: 'https://m3ws.kugou.com/share/index.php', + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + dfid: '-', + clienttime: '1586163263991', + }, + }).then(data => data.info)) + } + return Promise.all(tasks).then(([...datas]) => datas.flat()) + }, + async getUserListDetail2(global_collection_id) { + let id = global_collection_id + if (id.length > 1000) throw new Error('get list error') + const params = 'appid=1058&specialid=0&global_specialid=' + id + '&format=jsonp&srcappid=2919&clientver=20000&clienttime=1586163242519&mid=1586163242519&uuid=1586163242519&dfid=-' + let info = await createHttpFetch(`https://mobiles.kugou.com/api/v5/special/info_v2?${params}&signature=${signatureParams(params, 5)}`, { + headers: { + mid: '1586163242519', + Referer: 'https://m3ws.kugou.com/share/index.php', + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + dfid: '-', + clienttime: '1586163242519', + }, + }) + const songInfo = await this.createGetListDetail2Task(id, info.songcount) + let list = await getMusicInfosByList(songInfo) + // console.log(info, songInfo, list) + return { + list, + page: 1, + limit: this.listDetailLimit, + total: list.length, + source: 'kg', + info: { + name: info.specialname, + img: info.imgurl && info.imgurl.replace('{size}', 240), + desc: info.intro, + author: info.nickname, + play_count: formatPlayCount(info.playcount), + }, + } + }, + + async getListInfoByChain(chain) { + if (this.cache.has(chain)) return this.cache.get(chain) + const { body } = await httpFetch(`https://m.kugou.com/share/?chain=${chain}&id=${chain}`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1', + }, + }).promise + // console.log(body) + let result = body.match(/var\sphpParam\s=\s({.+?});/) + if (result) result = JSON.parse(result[1]) + this.cache.set(chain, result) + return result + }, + + async getUserListDetailByPcChain(chain) { + let key = `${chain}_pc_list` + if (this.cache.has(key)) return this.cache.get(key) + const { body } = await httpFetch(`http://www.kugou.com/share/${chain}.html`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', + }, + }).promise + let result = body.match(/var\sdataFromSmarty\s=\s(\[.+?\])/) + if (result) result = JSON.parse(result[1]) + this.cache.set(chain, result) + result = await getMusicInfosByList(result) + // console.log(info, songInfo) + return result + }, + + async getUserListDetail4(songInfo, chain, page) { + const limit = 100 + const [listInfo, list] = await Promise.all([ + this.getListInfoByChain(chain), + this.getUserListDetailBySpecialId(songInfo.id, page, limit), + ]) + return { + list: list || [], + page, + limit, + total: list.length ?? 0, + source: 'kg', + info: { + name: listInfo.specialname, + img: listInfo.imgurl && listInfo.imgurl.replace('{size}', 240), + // desc: body.result.info.list_desc, + author: listInfo.nickname, + // play_count: formatPlayCount(info.count), + }, + } + }, + + async getUserListDetail5(chain) { + const [listInfo, list] = await Promise.all([ + this.getListInfoByChain(chain), + this.getUserListDetailByPcChain(chain), + ]) + return { + list: list || [], + page: 1, + limit: this.listDetailLimit, + total: list.length ?? 0, + source: 'kg', + info: { + name: listInfo.specialname, + img: listInfo.imgurl && listInfo.imgurl.replace('{size}', 240), + // desc: body.result.info.list_desc, + author: listInfo.nickname, + // play_count: formatPlayCount(info.count), + }, + } + }, + + async getUserListDetail(link, page, retryNum = 0) { + if (retryNum > 3) return Promise.reject(new Error('link try max num')) + + const requestLink = httpFetch(link, { + 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: link, + }, + follow_max: 2, + }) + const { headers: { location }, statusCode, body } = await requestLink.promise + // console.log(body, location, statusCode) + if (statusCode > 400) return this.getUserListDetail(link, page, ++retryNum) + if (typeof body == 'string') { + if (body.includes('"global_collection_id":')) return this.getUserListDetailByCollectionId(body.replace(/^[\s\S]+?"global_collection_id":"(\w+)"[\s\S]+?$/, '$1'), page) + if (body.includes('"albumid":')) return album.getAlbumDetail(body.replace(/^[\s\S]+?"albumid":(\w+)[\s\S]+?$/, '$1'), page) + if (body.includes('"album_id":') && link.includes('album/info')) return album.getAlbumDetail(body.replace(/^[\s\S]+?"album_id":(\w+)[\s\S]+?$/, '$1'), page) + if (body.includes('list_id = "') && link.includes('album/info')) return album.getAlbumDetail(body.replace(/^[\s\S]+?list_id = "(\w+)"[\s\S]+?$/, '$1'), page) + } + if (location) { + // 概念版分享链接 https://t1.kugou.com/xxx + if (location.includes('global_specialid')) return this.getUserListDetailByCollectionId(location.replace(/^.*?global_specialid=(\w+)(?:&.*$|#.*$|$)/, '$1'), page) + if (location.includes('global_collection_id')) return this.getUserListDetailByCollectionId(location.replace(/^.*?global_collection_id=(\w+)(?:&.*$|#.*$|$)/, '$1'), page) + if (location.includes('chain=')) return this.getUserListDetail3(location.replace(/^.*?chain=(\w+)(?:&.*$|#.*$|$)/, '$1'), page) + if (location.includes('.html')) { + if (location.includes('zlist.html')) { + let link = location.replace(/^(.*)zlist\.html/, 'https://m3ws.kugou.com/zlist/list') + if (link.includes('pagesize')) { + link = link.replace('pagesize=30', 'pagesize=' + this.listDetailLimit).replace('page=1', 'page=' + page) + } else { + link += `&pagesize=${this.listDetailLimit}&page=${page}` + } + return this.getUserListDetail(link, page, ++retryNum) + } else return this.getUserListDetail3(location.replace(/.+\/(\w+).html(?:\?.*|&.*$|#.*$|$)/, '$1'), page) + } + return this.getUserListDetail(location, page, ++retryNum) + } + if (body.errcode !== 0) return this.getUserListDetail(link, page, ++retryNum) + return this.getUserListDetailByLink(body, link) + }, + + // 获取列表信息 + getListInfo(tagId, tryNum = 0) { + if (this._requestObj_listInfo) this._requestObj_listInfo.cancelHttp() + if (tryNum > 2) return Promise.reject(new Error('try max num')) + this._requestObj_listInfo = httpFetch(this.getInfoUrl(tagId)) + return this._requestObj_listInfo.promise.then(({ body }) => { + if (body.status !== 1) return this.getListInfo(tagId, ++tryNum) + return { + limit: body.data.params.pagesize, + page: body.data.params.p, + total: body.data.params.total, + source: 'kg', + } + }) + }, + + // 获取列表数据 + getList(sortId, tagId, page) { + let tasks = [this.getSongList(sortId, tagId, page)] + tasks.push( + this.currentTagInfo.id === tagId + ? Promise.resolve(this.currentTagInfo.info) + : this.getListInfo(tagId).then(info => { + this.currentTagInfo.id = tagId + this.currentTagInfo.info = Object.assign({}, info) + return info + }), + ) + if (!tagId && page === 1 && sortId === this.sortList[0].id) tasks.push(this.getSongListRecommend()) // 如果是所有类别,则顺便获取推荐列表 + return Promise.all(tasks).then(([list, info, recommendList]) => { + if (recommendList) list.unshift(...recommendList) + return { + list, + ...info, + } + }) + }, + + // 获取标签 + getTags(tryNum = 0) { + if (this._requestObj_tags) this._requestObj_tags.cancelHttp() + if (tryNum > 2) return Promise.reject(new Error('try max num')) + this._requestObj_tags = httpFetch(this.getInfoUrl()) + return this._requestObj_tags.promise.then(({ body }) => { + if (body.status !== 1) return this.getTags(++tryNum) + return { + hotTag: this.filterInfoHotTag(body.data.hotTag), + tags: this.filterTagInfo(body.data.tagids), + source: 'kg', + } + }) + }, + + getDetailPageUrl(id) { + if (typeof id == 'string') { + if (/^https?:\/\//.test(id)) return id + id = id.replace('id_', '') + } + return `https://www.kugou.com/yy/special/single/${id}.html` + }, + + search(text, page, limit = 20) { + const params = `userid=1384394652&req_custom=1&appid=1005&req_multi=1&version=11589&page=${page}&filter=0&pagesize=${limit}&order=0&clienttime=1681779443&iscorrection=1&searchsong=0&keyword=${text}&mid=288799920684148686226285199951543865551&dfid=3eSBsO1u97EY1zeIZd40hH4p&clientver=11589&platform=AndroidFilter` + const url = encodeURI(`http://complexsearchretry.kugou.com/v1/search/special?${params}&signature=${signatureParams(params, 1)}`) + return createHttpFetch(url).then(body => { + // console.log(body) + return { + list: body.lists.map(item => { + return { + play_count: formatPlayCount(item.total_play_count), + id: item.gid ? `gid_${item.gid}` : `id_${item.specialid}`, + author: item.nickname, + name: item.specialname, + time: dateFormat(item.publish_time, 'Y-M-D'), + img: item.img, + grade: item.grade, + desc: item.intro, + total: item.song_count, + source: 'kg', + } + }), + limit, + total: body.total, + source: 'kg', + } + }) + // http://msearchretry.kugou.com/api/v3/search/special?version=9209&keyword=%E5%91%A8%E6%9D%B0%E4%BC%A6&pagesize=20&filter=0&page=1&sver=2&with_res_tag=0 + // http://ioscdn.kugou.com/api/v3/search/special?keyword=${encodeURIComponent(text)}&page=${page}&pagesize=${limit}&showtype=10&plat=2&version=7910&correct=1&sver=5 + // http://msearchretry.kugou.com/api/v3/search/special?keyword=${encodeURIComponent(text)}&page=${page}&pagesize=${limit}&showtype=10&filter=0&version=7910&sver=2 + }, +} + +// getList +// getTags +// getListDetail diff --git a/src/renderer/utils/musicSdk/kg/tipSearch.js b/src/renderer/utils/musicSdk/kg/tipSearch.js new file mode 100644 index 00000000..0268676d --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/tipSearch.js @@ -0,0 +1,25 @@ +import { createHttpFetch } from './util' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = createHttpFetch(`https://searchtip.kugou.com/getSearchTip?MusicTipCount=10&keyword=${encodeURIComponent(str)}`, { + headers: { + referer: 'https://www.kugou.com/', + }, + }) + return this.requestObj.then(body => { + return body[0].RecordDatas + }) + }, + handleResult(rawData) { + return rawData.map(info => info.HintInfo) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +} diff --git a/src/renderer/utils/musicSdk/kg/util.js b/src/renderer/utils/musicSdk/kg/util.js index 6475f175..955bac98 100644 --- a/src/renderer/utils/musicSdk/kg/util.js +++ b/src/renderer/utils/musicSdk/kg/util.js @@ -1,5 +1,6 @@ import { inflate } from 'zlib' import { toMD5 } from '../utils' +import { httpFetch } from '../../request' // https://github.com/lyswhut/lx-music-desktop/issues/296#issuecomment-683285784 const enc_key = Buffer.from([0x40, 0x47, 0x61, 0x77, 0x5e, 0x32, 0x74, 0x47, 0x51, 0x36, 0x31, 0x2d, 0xce, 0xd2, 0x6e, 0x69], 'binary') @@ -19,6 +20,11 @@ export const decodeLyric = str => new Promise((resolve, reject) => { // console.log(str) // }) +/** + * 签名 + * @param {*} params + * @param {*} apiver + */ export const signatureParams = (params, apiver = 9) => { let keyparam = 'OIlwieks28dk2k092lksi2UIkp' if (apiver === 5) keyparam = 'NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt' @@ -27,3 +33,30 @@ export const signatureParams = (params, apiver = 9) => { let sign_params = `${keyparam}${param_list.join('')}${keyparam}` return toMD5(sign_params) } + +/** + * 创建一个适用于KG的Http请求 + * @param {*} url + * @param {*} options + * @param {*} retryNum + */ +export const createHttpFetch = async(url, options, retryNum = 0) => { + if (retryNum > 2) throw new Error('try max num') + let result + try { + result = await httpFetch(url, options).promise + } catch (err) { + console.log(err) + return createHttpFetch(url, options, ++retryNum) + } + // console.log(result.statusCode, result.body) + if (result.statusCode !== 200 || + ( + result.body.error_code ?? + result.body.errcode ?? + result.body.err_code) != 0 + ) return createHttpFetch(url, options, ++retryNum) + if (result.body.data) return result.body.data + if (Array.isArray(result.body.info)) return result.body + return result.body.info +} diff --git a/src/renderer/utils/musicSdk/kw/index.js b/src/renderer/utils/musicSdk/kw/index.js index b943f18a..5c89fba7 100644 --- a/src/renderer/utils/musicSdk/kw/index.js +++ b/src/renderer/utils/musicSdk/kw/index.js @@ -1,5 +1,5 @@ import { httpFetch } from '../../request' -import tempSearch from './tempSearch' +import tipSearch from './tipSearch' import musicSearch from './musicSearch' import { formatSinger, getToken } from './util' import leaderboard from './leaderboard' @@ -32,7 +32,7 @@ const kw = { // // }) // }, - tempSearch, + tipSearch, musicSearch, leaderboard, songList, diff --git a/src/renderer/utils/musicSdk/kw/leaderboard.js b/src/renderer/utils/musicSdk/kw/leaderboard.js index 0c639bfb..5756ea72 100644 --- a/src/renderer/utils/musicSdk/kw/leaderboard.js +++ b/src/renderer/utils/musicSdk/kw/leaderboard.js @@ -2,7 +2,7 @@ import { httpFetch } from '../../request' import { formatPlayTime, decodeName } from '../../index' import { formatSinger } from './util' -const boardList = [{ id: 'kw__93', name: '酷我飙升榜', bangid: '93' }, { id: 'kw__17', name: '酷我新歌榜', bangid: '17' }, { id: 'kw__16', name: '酷我热歌榜', bangid: '16' }, { id: 'kw__158', name: '抖音热歌榜', bangid: '158' }, { id: 'kw__292', name: '酷我铃声榜', bangid: '292' }, { id: 'kw__284', name: '酷我热评榜', bangid: '284' }, { id: 'kw__290', name: 'ACG新歌榜', bangid: '290' }, { id: 'kw__286', name: '台湾KKBOX榜', bangid: '286' }, { id: 'kw__279', name: '冬日暖心榜', bangid: '279' }, { id: 'kw__281', name: '巴士随身听榜', bangid: '281' }, { id: 'kw__255', name: 'KTV点唱榜', bangid: '255' }, { id: 'kw__280', name: '家务进行曲榜', bangid: '280' }, { id: 'kw__282', name: '熬夜修仙榜', bangid: '282' }, { id: 'kw__283', name: '枕边轻音乐榜', bangid: '283' }, { id: 'kw__278', name: '古风音乐榜', bangid: '278' }, { id: 'kw__264', name: 'Vlog音乐榜', bangid: '264' }, { id: 'kw__242', name: '酷我电音榜', bangid: '242' }, { id: 'kw__187', name: '流行趋势榜', bangid: '187' }, { id: 'kw__204', name: '现场音乐榜', bangid: '204' }, { id: 'kw__186', name: 'ACG神曲榜', bangid: '186' }, { id: 'kw__185', name: '最强翻唱榜', bangid: '185' }, { id: 'kw__26', name: '经典怀旧榜', bangid: '26' }, { id: 'kw__104', name: '酷我华语榜', bangid: '104' }, { id: 'kw__182', name: '酷我粤语榜', bangid: '182' }, { id: 'kw__22', name: '酷我欧美榜', bangid: '22' }, { id: 'kw__184', name: '酷我韩语榜', bangid: '184' }, { id: 'kw__183', name: '酷我日语榜', bangid: '183' }, { id: 'kw__145', name: '会员畅听榜', bangid: '145' }, { id: 'kw__153', name: '网红新歌榜', bangid: '153' }, { id: 'kw__64', name: '影视金曲榜', bangid: '64' }, { id: 'kw__176', name: 'DJ嗨歌榜', bangid: '176' }, { id: 'kw__106', name: '酷我真声音', bangid: '106' }, { id: 'kw__12', name: 'Billboard榜', bangid: '12' }, { id: 'kw__49', name: 'iTunes音乐榜', bangid: '49' }, { id: 'kw__180', name: 'beatport电音榜', bangid: '180' }, { id: 'kw__13', name: '英国UK榜', bangid: '13' }, { id: 'kw__164', name: '百大DJ榜', bangid: '164' }, { id: 'kw__246', name: 'YouTube音乐排行榜', bangid: '246' }, { id: 'kw__265', name: '韩国Genie榜', bangid: '265' }, { id: 'kw__14', name: '韩国M-net榜', bangid: '14' }, { id: 'kw__8', name: '香港电台榜', bangid: '8' }, { id: 'kw__15', name: '日本公信榜', bangid: '15' }, { id: 'kw__151', name: '腾讯音乐人原创榜', bangid: '151' }] +const boardList = [{ id: 'kw__93', name: '飙升榜', bangid: '93' }, { id: 'kw__17', name: '新歌榜', bangid: '17' }, { id: 'kw__16', name: '热歌榜', bangid: '16' }, { id: 'kw__158', name: '抖音热歌榜', bangid: '158' }, { id: 'kw__292', name: '铃声榜', bangid: '292' }, { id: 'kw__284', name: '热评榜', bangid: '284' }, { id: 'kw__290', name: 'ACG新歌榜', bangid: '290' }, { id: 'kw__286', name: '台湾KKBOX榜', bangid: '286' }, { id: 'kw__279', name: '冬日暖心榜', bangid: '279' }, { id: 'kw__281', name: '巴士随身听榜', bangid: '281' }, { id: 'kw__255', name: 'KTV点唱榜', bangid: '255' }, { id: 'kw__280', name: '家务进行曲榜', bangid: '280' }, { id: 'kw__282', name: '熬夜修仙榜', bangid: '282' }, { id: 'kw__283', name: '枕边轻音乐榜', bangid: '283' }, { id: 'kw__278', name: '古风音乐榜', bangid: '278' }, { id: 'kw__264', name: 'Vlog音乐榜', bangid: '264' }, { id: 'kw__242', name: '电音榜', bangid: '242' }, { id: 'kw__187', name: '流行趋势榜', bangid: '187' }, { id: 'kw__204', name: '现场音乐榜', bangid: '204' }, { id: 'kw__186', name: 'ACG神曲榜', bangid: '186' }, { id: 'kw__185', name: '最强翻唱榜', bangid: '185' }, { id: 'kw__26', name: '经典怀旧榜', bangid: '26' }, { id: 'kw__104', name: '华语榜', bangid: '104' }, { id: 'kw__182', name: '粤语榜', bangid: '182' }, { id: 'kw__22', name: '欧美榜', bangid: '22' }, { id: 'kw__184', name: '韩语榜', bangid: '184' }, { id: 'kw__183', name: '日语榜', bangid: '183' }, { id: 'kw__145', name: '会员畅听榜', bangid: '145' }, { id: 'kw__153', name: '网红新歌榜', bangid: '153' }, { id: 'kw__64', name: '影视金曲榜', bangid: '64' }, { id: 'kw__176', name: 'DJ嗨歌榜', bangid: '176' }, { id: 'kw__106', name: '真声音', bangid: '106' }, { id: 'kw__12', name: 'Billboard榜', bangid: '12' }, { id: 'kw__49', name: 'iTunes音乐榜', bangid: '49' }, { id: 'kw__180', name: 'beatport电音榜', bangid: '180' }, { id: 'kw__13', name: '英国UK榜', bangid: '13' }, { id: 'kw__164', name: '百大DJ榜', bangid: '164' }, { id: 'kw__246', name: 'YouTube音乐排行榜', bangid: '246' }, { id: 'kw__265', name: '韩国Genie榜', bangid: '265' }, { id: 'kw__14', name: '韩国M-net榜', bangid: '14' }, { id: 'kw__8', name: '香港电台榜', bangid: '8' }, { id: 'kw__15', name: '日本公信榜', bangid: '15' }, { id: 'kw__151', name: '腾讯音乐人原创榜', bangid: '151' }] export default { list: [ diff --git a/src/renderer/utils/musicSdk/kw/tempSearch.js b/src/renderer/utils/musicSdk/kw/tipSearch.js similarity index 79% rename from src/renderer/utils/musicSdk/kw/tempSearch.js rename to src/renderer/utils/musicSdk/kw/tipSearch.js index f1f958f3..007478c6 100644 --- a/src/renderer/utils/musicSdk/kw/tempSearch.js +++ b/src/renderer/utils/musicSdk/kw/tipSearch.js @@ -1,15 +1,13 @@ -// import { httpFetch } from '../../request' import { decodeName } from '../../index' import { tokenRequest } from './util' - export default { regExps: { relWord: /RELWORD=(.+)/, }, requestObj: null, - async tempSearch(str) { - this.cancelTempSearch() + async tipSearchBySong(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) @@ -23,10 +21,10 @@ export default { return matchResult ? decodeName(matchResult[1]) : '' }) }, - cancelTempSearch() { + cancelTipSearch() { if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() }, async search(str) { - return this.tempSearch(str).then(result => this.handleResult(result.data)) + return this.tipSearchBySong(str).then(result => this.handleResult(result.data)) }, } diff --git a/src/renderer/utils/musicSdk/mg/album.js b/src/renderer/utils/musicSdk/mg/album.js index a41c95cb..b9b4435a 100644 --- a/src/renderer/utils/musicSdk/mg/album.js +++ b/src/renderer/utils/musicSdk/mg/album.js @@ -1,18 +1,51 @@ -import { httpFetch } from '../../request' +import { createHttpFetch } from './utils' +import { filterMusicInfoList } from './musicInfo' +import { formatPlayCount } from '../../index' export default { - getAlbum(songInfo, tryNum = 0) { - let requestObj = httpFetch(`http://app.c.nf.migu.cn/MIGUM2.0/v1.0/content/queryAlbumSong?albumId=${songInfo.albumId}&pageNo=1`) - requestObj.promise = requestObj.promise.then(({ body }) => { - if (body.code !== '000000') { - if (tryNum > 5) return Promise.reject('获取专辑失败') - let tryRequestObj = this.getAlbum(songInfo, ++tryNum) - requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj) - return tryRequestObj.promise - } - // console.log(body) - return body.songList - }) - return requestObj + /** + * 通过AlbumId获取专辑 + * @param {*} id + * @param {*} page + */ + async getAlbumDetail(id, page = 1) { + const list = await createHttpFetch(`http://app.c.nf.migu.cn/MIGUM2.0/v1.0/content/queryAlbumSong?albumId=${id}&pageNo=${page}`) + if (!list.songList) return Promise.reject(new Error('Get album list error.')) + + const songList = filterMusicInfoList(list.songList) + const listInfo = await this.getAlbumInfo(id) + + return { + list: songList || [], + page, + limit: listInfo.total, + total: listInfo.total, + source: 'mg', + info: { + name: listInfo.name, + img: listInfo.image, + desc: listInfo.desc, + author: listInfo.author, + play_count: listInfo.play_count, + }, + } + }, + /** + * 通过AlbumId获取专辑信息 + * @param {*} id + * @param {*} page + */ + async getAlbumInfo(id) { + const info = await createHttpFetch(`https://app.c.nf.migu.cn/MIGUM3.0/resource/album/v2.0?albumId=${id}`) + if (!info) return Promise.reject(new Error('Get album info error.')) + + return { + name: info.title, + image: info.imgItems.length ? info.imgItems[0].img : null, + desc: info.summary, + author: info.singer, + play_count: formatPlayCount(info.opNumItem.playNum), + total: info.totalCount, + } }, } diff --git a/src/renderer/utils/musicSdk/mg/index.js b/src/renderer/utils/musicSdk/mg/index.js index 72ccde6a..6fec7987 100644 --- a/src/renderer/utils/musicSdk/mg/index.js +++ b/src/renderer/utils/musicSdk/mg/index.js @@ -6,8 +6,10 @@ import pic from './pic' import lyric from './lyric' import hotSearch from './hotSearch' import comment from './comment' +// import tipSearch from './tipSearch' const mg = { + // tipSearch, songList, musicSearch, leaderboard, diff --git a/src/renderer/utils/musicSdk/mg/leaderboard.js b/src/renderer/utils/musicSdk/mg/leaderboard.js index 112435f3..c41fe22f 100644 --- a/src/renderer/utils/musicSdk/mg/leaderboard.js +++ b/src/renderer/utils/musicSdk/mg/leaderboard.js @@ -1,6 +1,5 @@ import { httpFetch } from '../../request' -import { filterMusicInfoData } from './musicInfo' - +import { filterMusicInfoList } from './musicInfo' // const boardList = [{ id: 'mg__27553319', name: '咪咕尖叫新歌榜', bangid: '27553319' }, { id: 'mg__27186466', name: '咪咕尖叫热歌榜', bangid: '27186466' }, { id: 'mg__27553408', name: '咪咕尖叫原创榜', bangid: '27553408' }, { id: 'mg__23189800', name: '咪咕港台榜', bangid: '23189800' }, { id: 'mg__23189399', name: '咪咕内地榜', bangid: '23189399' }, { id: 'mg__19190036', name: '咪咕欧美榜', bangid: '19190036' }, { id: 'mg__23189813', name: '咪咕日韩榜', bangid: '23189813' }, { id: 'mg__23190126', name: '咪咕彩铃榜', bangid: '23190126' }, { id: 'mg__15140045', name: '咪咕KTV榜', bangid: '15140045' }, { id: 'mg__15140034', name: '咪咕网络榜', bangid: '15140034' }, { id: 'mg__23217754', name: 'MV榜', bangid: '23217754' }, { id: 'mg__23218151', name: '新专辑榜', bangid: '23218151' }, { id: 'mg__21958042', name: 'iTunes榜', bangid: '21958042' }, { id: 'mg__21975570', name: 'billboard榜', bangid: '21975570' }, { id: 'mg__22272815', name: '台湾Hito中文榜', bangid: '22272815' }, { id: 'mg__22272904', name: '中国TOP排行榜', bangid: '22272904' }, { id: 'mg__22272943', name: '韩国Melon榜', bangid: '22272943' }, { id: 'mg__22273437', name: '英国UK榜', bangid: '22273437' }] @@ -144,7 +143,7 @@ export default { return this.getData(this.getUrl(bangid, page)).then(({ statusCode, body }) => { // console.log(body) if (statusCode !== 200 || body.code !== this.successCode) return this.getList(bangid, page, retryNum) - const list = filterMusicInfoData(body.columnInfo.contents.map(m => m.objectInfo)) + const list = filterMusicInfoList(body.columnInfo.contents.map(m => m.objectInfo)) return { total: list.length, list, diff --git a/src/renderer/utils/musicSdk/mg/lyric.js b/src/renderer/utils/musicSdk/mg/lyric.js index 540a2e9e..8511cdb0 100644 --- a/src/renderer/utils/musicSdk/mg/lyric.js +++ b/src/renderer/utils/musicSdk/mg/lyric.js @@ -1,6 +1,6 @@ import { httpFetch } from '../../request' import { getMusicInfo } from './musicInfo' -import { decrypt } from './mrc' +import { decrypt } from './utils/mrc' const mrcTools = { rxps: { diff --git a/src/renderer/utils/musicSdk/mg/musicInfo.js b/src/renderer/utils/musicSdk/mg/musicInfo.js index caad8286..99aeeebc 100644 --- a/src/renderer/utils/musicSdk/mg/musicInfo.js +++ b/src/renderer/utils/musicSdk/mg/musicInfo.js @@ -1,15 +1,25 @@ -import { httpFetch } from '../../request' import { sizeFormate } from '../../index' +import { createHttpFetch } from './utils' +import { formatSingerName } from '../utils' -const getSinger = (singers) => { - let arr = [] - singers?.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') +const createGetMusicInfosTask = (ids) => { + let list = ids + let tasks = [] + while (list.length) { + tasks.push(list.slice(0, 100)) + if (list.length < 100) break + list = list.slice(100) + } + let url = 'https://c.musicapp.migu.cn/MIGUM2.0/v1.0/content/resourceinfo.do?resourceType=2' + return Promise.all(tasks.map(task => createHttpFetch(url, { + method: 'POST', + form: { + resourceId: task.join('|'), + }, + }).then(data => data.resource))) } -export const filterMusicInfoData = (rawList) => { +export const filterMusicInfoList = (rawList) => { // console.log(rawList) let ids = new Set() const list = [] @@ -55,7 +65,7 @@ export const filterMusicInfoData = (rawList) => { const intervalTest = /(\d\d:\d\d)$/.test(item.length) list.push({ - singer: getSinger(item.artists), + singer: formatSingerName(item.artists, 'name'), name: item.songName, albumName: item.album, albumId: item.albumId, @@ -74,27 +84,13 @@ export const filterMusicInfoData = (rawList) => { typeUrl: {}, }) }) - // console.log(list) return list } -export const getMusicInfos = (copyrightIds, retry = 0) => { - if (++retry > 2) return Promise.reject(new Error('Failed to get music info try max')) - return httpFetch('https://c.musicapp.migu.cn/MIGUM2.0/v1.0/content/resourceinfo.do?resourceType=2', { - method: 'POST', - form: { - resourceId: copyrightIds.join('|'), - }, - }).promise.then(({ body }) => { - if (!body) return getMusicInfos(copyrightIds, retry) - if (body.code !== '000000') return Promise.reject(new Error('Failed to get music info')) - return filterMusicInfoData(body.resource) - }) +export const getMusicInfo = async(copyrightId) => { + return getMusicInfos([copyrightId]).then(data => data[0]) } -export const getMusicInfo = (copyrightId) => { - return getMusicInfos([copyrightId]).then(([musicInfo]) => { - if (musicInfo) return musicInfo - throw new Error('failed') - }) +export const getMusicInfos = async(copyrightIds) => { + return filterMusicInfoList(await Promise.all(createGetMusicInfosTask(copyrightIds)).then(data => data.flat())) } diff --git a/src/renderer/utils/musicSdk/mg/musicSearch.js b/src/renderer/utils/musicSdk/mg/musicSearch.js index e5cc0c24..3cb1b7df 100644 --- a/src/renderer/utils/musicSdk/mg/musicSearch.js +++ b/src/renderer/utils/musicSdk/mg/musicSearch.js @@ -1,6 +1,7 @@ import { httpFetch } from '../../request' import { sizeFormate, formatPlayTime } from '../../index' import { toMD5 } from '../utils' +import { formatSingerName } from '@renderer/utils/musicSdk/utils' const sign = (time, str) => { const deviceId = '963B7AA0D21511ED807EE5846EC87D20' @@ -124,13 +125,6 @@ export default { }) return searchRequest.promise.then(({ body }) => body) }, - getSinger(singers) { - let arr = [] - singers.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') - }, filterData(rawData) { // console.log(rawData) const list = [] @@ -181,7 +175,7 @@ export default { if (img && !/https?:/.test(data.img3)) img = 'http://d.musicapp.migu.cn' + img list.push({ - singer: this.getSinger(data.singerList), + singer: formatSingerName(data.singerList), name: data.name, albumName: data.album, albumId: data.albumId, diff --git a/src/renderer/utils/musicSdk/mg/songList.js b/src/renderer/utils/musicSdk/mg/songList.js index 5b0984ee..090d9da8 100644 --- a/src/renderer/utils/musicSdk/mg/songList.js +++ b/src/renderer/utils/musicSdk/mg/songList.js @@ -1,6 +1,6 @@ import { httpFetch } from '../../request' -import { dateFormat } from '../../index' -import { filterMusicInfoData } from './musicInfo' +import { dateFormat, formatPlayCount } from '../../index' +import { filterMusicInfoList } from './musicInfo' // const tagData = { code: '000000', info: 'SUCCESS', columnInfo: { columnTitle: '分类', columnId: '15244430', columnPid: '15031270', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 6, columnStatus: 1, columnCreateTime: '2016-11-10 10:53:05.077', columntype: 2011, contents: [{ contentId: '18464615', relationType: 2011, objectInfo: { columnTitle: '热门', columnId: '18464615', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 8, columnStatus: 1, columnCreateTime: '2017-02-20 16:09:13.400', columntype: 2011, contents: [{ contentId: '1000001672', relationType: 4034, objectInfo: { tagId: '1000001672', tagName: '流行', resourceType: '2034' }, relationSort: 9 }, { contentId: '1003449727', relationType: 4034, objectInfo: { tagId: '1003449727', tagName: '厂牌', resourceType: '2034' }, relationSort: 8 }, { contentId: '1000001795', relationType: 4034, objectInfo: { tagId: '1000001795', tagName: '伤感', resourceType: '2034' }, relationSort: 7 }, { contentId: '1001076080', relationType: 4034, objectInfo: { tagId: '1001076080', tagName: '电影', resourceType: '2034' }, relationSort: 6 }, { contentId: '1000001675', relationType: 4034, objectInfo: { tagId: '1000001675', tagName: '中国风', resourceType: '2034' }, relationSort: 5 }, { contentId: '1000001635', relationType: 4034, objectInfo: { tagId: '1000001635', tagName: '经典老歌', resourceType: '2034' }, relationSort: 4 }, { contentId: '1000001831', relationType: 4034, objectInfo: { tagId: '1000001831', tagName: '翻唱', resourceType: '2034' }, relationSort: 3 }, { contentId: '1000001762', relationType: 4034, objectInfo: { tagId: '1000001762', tagName: '国语', resourceType: '2034' }, relationSort: 1 }], dataVersion: '1620410266029', customizedPicUrls: [] }, relationSort: 6 }, { contentId: '15244503', relationType: 2011, objectInfo: { columnTitle: '主题', columnId: '15244503', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 23, columnStatus: 1, columnCreateTime: '2016-11-10 10:54:10.261', columntype: 2011, contents: [{ contentId: '1003449727', relationType: 4034, objectInfo: { tagId: '1003449727', tagName: '厂牌', resourceType: '2034' }, relationSort: 29 }, { contentId: '1001076080', relationType: 4034, objectInfo: { tagId: '1001076080', tagName: '电影', resourceType: '2034' }, relationSort: 28 }, { contentId: '1001076078', relationType: 4034, objectInfo: { tagId: '1001076078', tagName: '电视剧', resourceType: '2034' }, relationSort: 27 }, { contentId: '1001076083', relationType: 4034, objectInfo: { tagId: '1001076083', tagName: '综艺', resourceType: '2034' }, relationSort: 26 }, { contentId: '1000001827', relationType: 4034, objectInfo: { tagId: '1000001827', tagName: 'KTV', resourceType: '2034' }, relationSort: 23 }, { contentId: '1000001698', relationType: 4034, objectInfo: { tagId: '1000001698', tagName: '爱情', resourceType: '2034' }, relationSort: 22 }, { contentId: '1000001635', relationType: 4034, objectInfo: { tagId: '1000001635', tagName: '经典老歌', resourceType: '2034' }, relationSort: 21 }, { contentId: '1001076096', relationType: 4034, objectInfo: { tagId: '1001076096', tagName: '网络热歌', resourceType: '2034' }, relationSort: 20 }, { contentId: '1000001780', relationType: 4034, objectInfo: { tagId: '1000001780', tagName: '儿童歌曲', resourceType: '2034' }, relationSort: 19 }, { contentId: '1000587702', relationType: 4034, objectInfo: { tagId: '1000587702', tagName: '广场舞', resourceType: '2034' }, relationSort: 18 }, { contentId: '1000587717', relationType: 4034, objectInfo: { tagId: '1000587717', tagName: '70后', resourceType: '2034' }, relationSort: 17 }, { contentId: '1000587718', relationType: 4034, objectInfo: { tagId: '1000587718', tagName: '80后', resourceType: '2034' }, relationSort: 16 }, { contentId: '1000587726', relationType: 4034, objectInfo: { tagId: '1000587726', tagName: '90后', resourceType: '2034' }, relationSort: 15 }, { contentId: '1000001670', relationType: 4034, objectInfo: { tagId: '1000001670', tagName: '红歌', resourceType: '2034' }, relationSort: 14 }, { contentId: '1000587698', relationType: 4034, objectInfo: { tagId: '1000587698', tagName: '游戏', resourceType: '2034' }, relationSort: 13 }, { contentId: '1000587706', relationType: 4034, objectInfo: { tagId: '1000587706', tagName: '动漫', resourceType: '2034' }, relationSort: 12 }, { contentId: '1000001675', relationType: 4034, objectInfo: { tagId: '1000001675', tagName: '中国风', resourceType: '2034' }, relationSort: 11 }, { contentId: '1000587712', relationType: 4034, objectInfo: { tagId: '1000587712', tagName: '青春校园', resourceType: '2034' }, relationSort: 10 }, { contentId: '1000587673', relationType: 4034, objectInfo: { tagId: '1000587673', tagName: '小清新', resourceType: '2034' }, relationSort: 9 }, { contentId: '1000093902', relationType: 4034, objectInfo: { tagId: '1000093902', tagName: 'DJ舞曲', resourceType: '2034' }, relationSort: 7 }, { contentId: '1000093963', relationType: 4034, objectInfo: { tagId: '1000093963', tagName: '广告', resourceType: '2034' }, relationSort: 6 }, { contentId: '1000001831', relationType: 4034, objectInfo: { tagId: '1000001831', tagName: '翻唱', resourceType: '2034' }, relationSort: 2 }, { contentId: '1003449726', relationType: 4034, objectInfo: { tagId: '1003449726', tagName: '读书', resourceType: '2034' }, relationSort: 1 }], dataVersion: '1620410266055', customizedPicUrls: [] }, relationSort: 5 }, { contentId: '15244509', relationType: 2011, objectInfo: { columnTitle: '风格', columnId: '15244509', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 12, columnStatus: 1, columnCreateTime: '2016-11-10 10:54:57.257', columntype: 2011, contents: [{ contentId: '1000001672', relationType: 4034, objectInfo: { tagId: '1000001672', tagName: '流行', resourceType: '2034' }, relationSort: 14 }, { contentId: '1000001808', relationType: 4034, objectInfo: { tagId: '1000001808', tagName: 'R&B', resourceType: '2034' }, relationSort: 13 }, { contentId: '1000001809', relationType: 4034, objectInfo: { tagId: '1000001809', tagName: '嘻哈', resourceType: '2034' }, relationSort: 12 }, { contentId: '1000001674', relationType: 4034, objectInfo: { tagId: '1000001674', tagName: '摇滚', resourceType: '2034' }, relationSort: 11 }, { contentId: '1000001682', relationType: 4034, objectInfo: { tagId: '1000001682', tagName: '电子', resourceType: '2034' }, relationSort: 10 }, { contentId: '1000001852', relationType: 4034, objectInfo: { tagId: '1000001852', tagName: '电子舞曲', resourceType: '2034' }, relationSort: 9 }, { contentId: '1000001681', relationType: 4034, objectInfo: { tagId: '1000001681', tagName: '爵士', resourceType: '2034' }, relationSort: 6 }, { contentId: '1000001683', relationType: 4034, objectInfo: { tagId: '1000001683', tagName: '乡村', resourceType: '2034' }, relationSort: 5 }, { contentId: '1000001851', relationType: 4034, objectInfo: { tagId: '1000001851', tagName: '蓝调', resourceType: '2034' }, relationSort: 4 }, { contentId: '1000001775', relationType: 4034, objectInfo: { tagId: '1000001775', tagName: '民谣', resourceType: '2034' }, relationSort: 3 }, { contentId: '1000001807', relationType: 4034, objectInfo: { tagId: '1000001807', tagName: '纯音乐', resourceType: '2034' }, relationSort: 2 }, { contentId: '1000001783', relationType: 4034, objectInfo: { tagId: '1000001783', tagName: '古典', resourceType: '2034' }, relationSort: 1 }], dataVersion: '1620410266033', customizedPicUrls: [] }, relationSort: 4 }, { contentId: '18464665', relationType: 2011, objectInfo: { columnTitle: '语种', columnId: '18464665', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 6, columnStatus: 1, columnCreateTime: '2017-02-20 16:07:16.566', columntype: 2011, contents: [{ contentId: '1000001762', relationType: 4034, objectInfo: { tagId: '1000001762', tagName: '国语', resourceType: '2034' }, relationSort: 6 }, { contentId: '1000001763', relationType: 4034, objectInfo: { tagId: '1000001763', tagName: '粤语', resourceType: '2034' }, relationSort: 5 }, { contentId: '1000001766', relationType: 4034, objectInfo: { tagId: '1000001766', tagName: '英语', resourceType: '2034' }, relationSort: 4 }, { contentId: '1000001599', relationType: 4034, objectInfo: { tagId: '1000001599', tagName: '韩语', resourceType: '2034' }, relationSort: 3 }, { contentId: '1000001767', relationType: 4034, objectInfo: { tagId: '1000001767', tagName: '日语', resourceType: '2034' }, relationSort: 2 }, { contentId: '1003449724', relationType: 4034, objectInfo: { tagId: '1003449724', tagName: '小语种', resourceType: '2034' }, relationSort: 1 }], dataVersion: '1620410266036', customizedPicUrls: [] }, relationSort: 3 }, { contentId: '18464583', relationType: 2011, objectInfo: { columnTitle: '心情', columnId: '18464583', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 13, columnStatus: 1, columnCreateTime: '2017-02-20 15:59:03.412', columntype: 2011, contents: [{ contentId: '1000587677', relationType: 4034, objectInfo: { tagId: '1000587677', tagName: '幸福', resourceType: '2034' }, relationSort: 15 }, { contentId: '1000587710', relationType: 4034, objectInfo: { tagId: '1000587710', tagName: '治愈', resourceType: '2034' }, relationSort: 14 }, { contentId: '1000001703', relationType: 4034, objectInfo: { tagId: '1000001703', tagName: '思念', resourceType: '2034' }, relationSort: 13 }, { contentId: '1000587667', relationType: 4034, objectInfo: { tagId: '1000587667', tagName: '期待', resourceType: '2034' }, relationSort: 12 }, { contentId: '1000001700', relationType: 4034, objectInfo: { tagId: '1000001700', tagName: '励志', resourceType: '2034' }, relationSort: 11 }, { contentId: '1000001694', relationType: 4034, objectInfo: { tagId: '1000001694', tagName: '欢快', resourceType: '2034' }, relationSort: 10 }, { contentId: '1002600588', relationType: 4034, objectInfo: { tagId: '1002600588', tagName: '叛逆', resourceType: '2034' }, relationSort: 9 }, { contentId: '1002600585', relationType: 4034, objectInfo: { tagId: '1002600585', tagName: '宣泄', resourceType: '2034' }, relationSort: 8 }, { contentId: '1000001696', relationType: 4034, objectInfo: { tagId: '1000001696', tagName: '怀旧', resourceType: '2034' }, relationSort: 7 }, { contentId: '1000587679', relationType: 4034, objectInfo: { tagId: '1000587679', tagName: '减压', resourceType: '2034' }, relationSort: 6 }, { contentId: '1000001699', relationType: 4034, objectInfo: { tagId: '1000001699', tagName: '寂寞', resourceType: '2034' }, relationSort: 5 }, { contentId: '1002600579', relationType: 4034, objectInfo: { tagId: '1002600579', tagName: '忧郁', resourceType: '2034' }, relationSort: 4 }, { contentId: '1000001795', relationType: 4034, objectInfo: { tagId: '1000001795', tagName: '伤感', resourceType: '2034' }, relationSort: 3 }], dataVersion: '1620410266187', customizedPicUrls: [] }, relationSort: 2 }, { contentId: '18464638', relationType: 2011, objectInfo: { columnTitle: '场景', columnId: '18464638', columnPid: '15244430', opNumItem: { playNum: 0, playNumDesc: '0', keepNum: 0, keepNumDesc: '0', commentNum: 0, commentNumDesc: '0', shareNum: 0, shareNumDesc: '0', orderNumByWeek: 0, orderNumByWeekDesc: '0', orderNumByTotal: 0, orderNumByTotalDesc: '0', thumbNum: 0, thumbNumDesc: '0', followNum: 0, followNumDesc: '0', subscribeNum: 0, subscribeNumDesc: '0', livePlayNum: 0, livePlayNumDesc: '0', popularNum: 0, popularNumDesc: '0', bookingNum: 0, bookingNumDesc: '0' }, contentsCount: 13, columnStatus: 1, columnCreateTime: '2017-02-20 16:02:59.711', columntype: 2011, contents: [{ contentId: '1000587689', relationType: 4034, objectInfo: { tagId: '1000587689', tagName: '清晨', resourceType: '2034' }, relationSort: 21 }, { contentId: '1000587690', relationType: 4034, objectInfo: { tagId: '1000587690', tagName: '夜晚', resourceType: '2034' }, relationSort: 20 }, { contentId: '1000587688', relationType: 4034, objectInfo: { tagId: '1000587688', tagName: '睡前安眠', resourceType: '2034' }, relationSort: 19 }, { contentId: '1003449726', relationType: 4034, objectInfo: { tagId: '1003449726', tagName: '读书', resourceType: '2034' }, relationSort: 18 }, { contentId: '1003449723', relationType: 4034, objectInfo: { tagId: '1003449723', tagName: '下午·茶', resourceType: '2034' }, relationSort: 16 }, { contentId: '1000093923', relationType: 4034, objectInfo: { tagId: '1000093923', tagName: '驾车', resourceType: '2034' }, relationSort: 15 }, { contentId: '1003449615', relationType: 4034, objectInfo: { tagId: '1003449615', tagName: '运动', resourceType: '2034' }, relationSort: 13 }, { contentId: '1000587694', relationType: 4034, objectInfo: { tagId: '1000587694', tagName: '散步', resourceType: '2034' }, relationSort: 12 }, { contentId: '1000001749', relationType: 4034, objectInfo: { tagId: '1000001749', tagName: '旅行', resourceType: '2034' }, relationSort: 11 }, { contentId: '1000587686', relationType: 4034, objectInfo: { tagId: '1000587686', tagName: '夜店', resourceType: '2034' }, relationSort: 10 }, { contentId: '1002600606', relationType: 4034, objectInfo: { tagId: '1002600606', tagName: '派对', resourceType: '2034' }, relationSort: 9 }, { contentId: '1000001634', relationType: 4034, objectInfo: { tagId: '1000001634', tagName: '咖啡馆', resourceType: '2034' }, relationSort: 3 }, { contentId: '1000587692', relationType: 4034, objectInfo: { tagId: '1000587692', tagName: '瑜伽', resourceType: '2034' }, relationSort: 1 }], dataVersion: '1620846028994', customizedPicUrls: [] }, relationSort: 1 }], dataVersion: '1620846028941', customizedPicUrls: [] } } @@ -61,16 +61,6 @@ export default { // version: '6.8.5', }, - /** - * 格式化播放数量 - * @param {*} num - */ - formatPlayCount(num) { - if (num > 100000000) return parseInt(num / 10000000) / 10 + '亿' - if (num > 10000) return parseInt(num / 1000) / 10 + '万' - return num - }, - getListDetailList(id, page, tryNum = 0) { if (tryNum > 2) return Promise.reject(new Error('try max num')) // https://h5.nf.migu.cn/app/v4/p/share/playlist/index.html?id=184187437&channel=0146921 @@ -85,7 +75,7 @@ export default { // console.log(JSON.stringify(body)) // console.log(body) return { - list: filterMusicInfoData(body.list), + list: filterMusicInfoList(body.list), page, limit: this.limit_song, total: body.totalCount, @@ -110,7 +100,7 @@ export default { img: body.data.imgItem.img, desc: body.data.summary, author: body.data.ownerName, - play_count: this.formatPlayCount(body.data.opNumItem.playNum), + play_count: formatPlayCount(body.data.opNumItem.playNum), } return cachedDetailInfo }) @@ -219,7 +209,7 @@ export default { filterList(rawData) { // console.log(rawData) return rawData.map(item => ({ - play_count: this.formatPlayCount(item.playCount), + play_count: formatPlayCount(item.playCount), id: item.playListId, author: item.createName, name: item.playListName, @@ -310,7 +300,7 @@ export default { return { list: body.songListResultData.result.map(item => { return { - play_count: this.formatPlayCount(item.playcount), + play_count: formatPlayCount(item.playcount), id: item.id, // author: item.createName, name: item.name, diff --git a/src/renderer/utils/musicSdk/mg/leaderboard2.js b/src/renderer/utils/musicSdk/mg/temp/leaderboard-old.js similarity index 98% rename from src/renderer/utils/musicSdk/mg/leaderboard2.js rename to src/renderer/utils/musicSdk/mg/temp/leaderboard-old.js index 14fc6ae5..1c5cb186 100644 --- a/src/renderer/utils/musicSdk/mg/leaderboard2.js +++ b/src/renderer/utils/musicSdk/mg/temp/leaderboard-old.js @@ -1,5 +1,5 @@ -import { httpFetch } from '../../request' -import { formatPlayTime } from '../../index' +import { httpFetch } from '../../../request' +import { formatPlayTime } from '../../../index' // import { sizeFormate } from '../../index' diff --git a/src/renderer/utils/musicSdk/mg/tipSearch.js b/src/renderer/utils/musicSdk/mg/tipSearch.js new file mode 100644 index 00000000..fcd4951b --- /dev/null +++ b/src/renderer/utils/musicSdk/mg/tipSearch.js @@ -0,0 +1,25 @@ +import { createHttpFetch } from './utils' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = createHttpFetch(`https://music.migu.cn/v3/api/search/suggest?keyword=${encodeURIComponent(str)}`, { + headers: { + referer: 'https://music.migu.cn/v3', + }, + }) + return this.requestObj.then(body => { + return body.songs + }) + }, + handleResult(rawData) { + return rawData.map(info => `${info.name} - ${info.singerName}`) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +} diff --git a/src/renderer/utils/musicSdk/mg/utils/index.js b/src/renderer/utils/musicSdk/mg/utils/index.js new file mode 100644 index 00000000..503a85f6 --- /dev/null +++ b/src/renderer/utils/musicSdk/mg/utils/index.js @@ -0,0 +1,29 @@ +import { httpFetch } from '../../../request' + +/** + * 创建一个适用于MG的Http请求 + * @param {*} url + * @param {*} options + * @param {*} retryNum + */ +export const createHttpFetch = async(url, options, retryNum = 0) => { + if (retryNum > 2) throw new Error('try max num') + let result + try { + result = await httpFetch(url, options).promise + } catch (err) { + console.log(err) + return createHttpFetch(url, options, ++retryNum) + } + if (result.statusCode !== 200 || + ( + (result.body.code !== undefined + ? result.body.code + : result.body.returnCode !== undefined + ? result.body.returnCode + : result.body.code + ) !== '000000') + ) return createHttpFetch(url, options, ++retryNum) + if (result.body.data) return result.body.data + return result.body +} diff --git a/src/renderer/utils/musicSdk/mg/mrc.js b/src/renderer/utils/musicSdk/mg/utils/mrc.js similarity index 100% rename from src/renderer/utils/musicSdk/mg/mrc.js rename to src/renderer/utils/musicSdk/mg/utils/mrc.js diff --git a/src/renderer/utils/musicSdk/tx/index.js b/src/renderer/utils/musicSdk/tx/index.js index 0c755acf..beb52af0 100644 --- a/src/renderer/utils/musicSdk/tx/index.js +++ b/src/renderer/utils/musicSdk/tx/index.js @@ -5,8 +5,10 @@ import musicSearch from './musicSearch' import { apis } from '../api-source' import hotSearch from './hotSearch' import comment from './comment' +// import tipSearch from './tipSearch' const tx = { + // tipSearch, leaderboard, songList, musicSearch, diff --git a/src/renderer/utils/musicSdk/tx/leaderboard.js b/src/renderer/utils/musicSdk/tx/leaderboard.js index 2e58f93f..09f51746 100644 --- a/src/renderer/utils/musicSdk/tx/leaderboard.js +++ b/src/renderer/utils/musicSdk/tx/leaderboard.js @@ -1,5 +1,6 @@ import { httpFetch } from '../../request' import { formatPlayTime, sizeFormate } from '../../index' +import { formatSingerName } from '../utils' let boardList = [{ id: 'tx__4', name: '流行指数榜', bangid: '4' }, { id: 'tx__26', name: '热歌榜', bangid: '26' }, { id: 'tx__27', name: '新歌榜', bangid: '27' }, { id: 'tx__62', name: '飙升榜', bangid: '62' }, { id: 'tx__58', name: '说唱榜', bangid: '58' }, { id: 'tx__57', name: '喜力电音榜', bangid: '57' }, { id: 'tx__28', name: '网络歌曲榜', bangid: '28' }, { id: 'tx__5', name: '内地榜', bangid: '5' }, { id: 'tx__3', name: '欧美榜', bangid: '3' }, { id: 'tx__59', name: '香港地区榜', bangid: '59' }, { id: 'tx__16', name: '韩国榜', bangid: '16' }, { id: 'tx__60', name: '抖快榜', bangid: '60' }, { id: 'tx__29', name: '影视金曲榜', bangid: '29' }, { id: 'tx__17', name: '日本榜', bangid: '17' }, { id: 'tx__52', name: '腾讯音乐人原创榜', bangid: '52' }, { id: 'tx__36', name: 'K歌金曲榜', bangid: '36' }, { id: 'tx__61', name: '台湾地区榜', bangid: '61' }, { id: 'tx__63', name: 'DJ舞曲榜', bangid: '63' }, { id: 'tx__64', name: '综艺新歌榜', bangid: '64' }, { id: 'tx__65', name: '国风热歌榜', bangid: '65' }, { id: 'tx__67', name: '听歌识曲榜', bangid: '67' }, { id: 'tx__72', name: '动漫音乐榜', bangid: '72' }, { id: 'tx__73', name: '游戏音乐榜', bangid: '73' }, { id: 'tx__75', name: '有声榜', bangid: '75' }, { id: 'tx__131', name: '校园音乐人排行榜', bangid: '131' }] @@ -104,13 +105,6 @@ export default { const requestDataObj = httpFetch(url) return requestDataObj.promise }, - getSinger(singers) { - let arr = [] - singers.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') - }, filterData(rawList) { // console.log(rawList) return rawList.map(item => { @@ -146,7 +140,7 @@ export default { } // types.reverse() return { - singer: this.getSinger(item.singer), + singer: formatSingerName(item.singer, 'name'), name: item.name, albumName: item.album.name, albumId: item.album.mid, diff --git a/src/renderer/utils/musicSdk/tx/musicSearch.js b/src/renderer/utils/musicSdk/tx/musicSearch.js index 7dd268d7..2e011eca 100644 --- a/src/renderer/utils/musicSdk/tx/musicSearch.js +++ b/src/renderer/utils/musicSdk/tx/musicSearch.js @@ -1,9 +1,6 @@ -// import '../../polyfill/array.find' - import { httpFetch } from '../../request' import { formatPlayTime, sizeFormate } from '../../index' -// import { debug } from '../../utils/env' -// import { formatSinger } from './util' +import { formatSingerName } from '../utils' export default { limit: 50, @@ -52,13 +49,6 @@ export default { return body.req.data }) }, - getSinger(singers) { - let arr = [] - singers.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') - }, handleResult(rawList) { // console.log(rawList) const list = [] @@ -104,7 +94,7 @@ export default { albumId = item.album.mid } list.push({ - singer: this.getSinger(item.singer), + singer: formatSingerName(item.singer, 'name'), name: item.name, albumName, albumId, diff --git a/src/renderer/utils/musicSdk/tx/songList.js b/src/renderer/utils/musicSdk/tx/songList.js index f7f72dd5..345bbb0f 100644 --- a/src/renderer/utils/musicSdk/tx/songList.js +++ b/src/renderer/utils/musicSdk/tx/songList.js @@ -1,5 +1,6 @@ import { httpFetch } from '../../request' -import { decodeName, formatPlayTime, sizeFormate, dateFormat } from '../../index' +import { decodeName, formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../../index' +import { formatSingerName } from '../utils' export default { _requestObj_tags: null, @@ -125,20 +126,10 @@ export default { }) }, - - /** - * 格式化播放数量 - * @param {*} num - */ - formatPlayCount(num) { - if (num > 100000000) return parseInt(num / 10000000) / 10 + '亿' - if (num > 10000) return parseInt(num / 1000) / 10 + '万' - return num - }, filterList(data, page) { return { list: data.v_playlist.map(item => ({ - play_count: this.formatPlayCount(item.access_num), + play_count: formatPlayCount(item.access_num), id: item.tid, author: item.creator_info.nick, name: item.title, @@ -159,7 +150,7 @@ export default { // console.log(content.v_item) return { list: content.v_item.map(({ basic }) => ({ - play_count: this.formatPlayCount(basic.play_cnt), + play_count: formatPlayCount(basic.play_cnt), id: basic.tid, author: basic.creator.nick, name: basic.title, @@ -228,17 +219,10 @@ export default { img: cdlist.logo, desc: decodeName(cdlist.desc).replace(/
/g, '\n'), author: cdlist.nickname, - play_count: this.formatPlayCount(cdlist.visitnum), + play_count: formatPlayCount(cdlist.visitnum), }, } }, - getSinger(singers) { - let arr = [] - singers.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') - }, filterListDetail(rawList) { // console.log(rawList) return rawList.map(item => { @@ -274,7 +258,7 @@ export default { } // types.reverse() return { - singer: this.getSinger(item.singer), + singer: formatSingerName(item.singer, 'name'), name: item.name, albumName: item.album.name, albumId: item.album.mid, @@ -319,7 +303,7 @@ export default { return { list: body.data.list.map(item => { return { - play_count: this.formatPlayCount(item.listennum), + play_count: formatPlayCount(item.listennum), id: item.dissid, author: item.creator.name, name: item.dissname, diff --git a/src/renderer/utils/musicSdk/tx/tipSearch.js b/src/renderer/utils/musicSdk/tx/tipSearch.js index 23f397e6..a844da9e 100644 --- a/src/renderer/utils/musicSdk/tx/tipSearch.js +++ b/src/renderer/utils/musicSdk/tx/tipSearch.js @@ -1,13 +1,12 @@ import { httpFetch } from '../../request' - export default { - regExps: { - relWord: /RELWORD=(.+)/, - }, + // regExps: { + // relWord: /RELWORD=(.+)/, + // }, requestObj: null, - tempSearch(str) { - this.cancelTempSearch() + tipSearch(str) { + this.cancelTipSearch() this.requestObj = httpFetch(`https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg?is_xml=0&format=json&key=${encodeURIComponent(str)}&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0`, { headers: { Referer: 'https://y.qq.com/portal/player.html', @@ -21,10 +20,10 @@ export default { handleResult(rawData) { return rawData.map(info => `${info.name} - ${info.singer}`) }, - cancelTempSearch() { + cancelTipSearch() { if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() }, async search(str) { - return this.tempSearch(str).then(result => this.handleResult(result.song.itemlist)) + return this.tipSearch(str).then(result => this.handleResult(result.song.itemlist)) }, } diff --git a/src/renderer/utils/musicSdk/utils.js b/src/renderer/utils/musicSdk/utils.js index 42e82232..756cf880 100644 --- a/src/renderer/utils/musicSdk/utils.js +++ b/src/renderer/utils/musicSdk/utils.js @@ -1,5 +1,6 @@ import crypto from 'crypto' import dns from 'dns' +import { decodeName } from '@common/utils/common' export const toMD5 = str => crypto.createHash('md5').update(str).digest('hex') @@ -27,3 +28,29 @@ export const dnsLookup = (hostname, options, callback) => { dns.lookup(hostname, options, callback) } + + +/** + * 格式化歌手 + * @param singers 歌手数组 + * @param nameKey 歌手名键值 + * @param join 歌手分割字符 + */ +export const formatSingerName = (singers, nameKey = 'name', join = '、') => { + if (typeof singers == 'string') { + try { + singers = JSON.parse(singers) + } catch (err) { + return decodeName(singers) + } + } else if (Array.isArray(singers)) { + const singer = [] + singers.forEach(item => { + let name = item[nameKey] + if (!name) return + singer.push(name) + }) + return decodeName(singer.join(join)) + } + return decodeName(String(singers ?? '')) +} diff --git a/src/renderer/utils/musicSdk/wy/index.js b/src/renderer/utils/musicSdk/wy/index.js index 5fdc0251..a28295f5 100644 --- a/src/renderer/utils/musicSdk/wy/index.js +++ b/src/renderer/utils/musicSdk/wy/index.js @@ -6,8 +6,10 @@ import musicSearch from './musicSearch' import songList from './songList' import hotSearch from './hotSearch' import comment from './comment' +// import tipSearch from './tipSearch' const wy = { + // tipSearch, leaderboard, musicSearch, songList, diff --git a/src/renderer/utils/musicSdk/wy/leaderboard.js b/src/renderer/utils/musicSdk/wy/leaderboard.js index 474adfa7..e55023fb 100644 --- a/src/renderer/utils/musicSdk/wy/leaderboard.js +++ b/src/renderer/utils/musicSdk/wy/leaderboard.js @@ -6,30 +6,30 @@ const topList = [{ id: 'wy__19723756', name: '飙升榜', bangid: '19723756' }, { id: 'wy__3779629', name: '新歌榜', bangid: '3779629' }, { id: 'wy__2884035', name: '原创榜', bangid: '2884035' }, { id: 'wy__3778678', name: '热歌榜', bangid: '3778678' }, - { id: 'wy__991319590', name: '云音乐说唱榜', bangid: '991319590' }, - { id: 'wy__71384707', name: '云音乐古典榜', bangid: '71384707' }, - { id: 'wy__1978921795', name: '云音乐电音榜', bangid: '1978921795' }, + { id: 'wy__991319590', name: '说唱榜', bangid: '991319590' }, + { id: 'wy__71384707', name: '古典榜', bangid: '71384707' }, + { id: 'wy__1978921795', name: '电音榜', bangid: '1978921795' }, { id: 'wy__5453912201', name: '黑胶VIP爱听榜', bangid: '5453912201' }, - { id: 'wy__71385702', name: '云音乐ACG榜', bangid: '71385702' }, - { id: 'wy__745956260', name: '云音乐韩语榜', bangid: '745956260' }, - { id: 'wy__10520166', name: '云音乐国电榜', bangid: '10520166' }, + { id: 'wy__71385702', name: 'ACG榜', bangid: '71385702' }, + { id: 'wy__745956260', name: '韩语榜', bangid: '745956260' }, + { id: 'wy__10520166', name: '国电榜', bangid: '10520166' }, { id: 'wy__180106', name: 'UK排行榜周榜', bangid: '180106' }, { id: 'wy__60198', name: '美国Billboard榜', bangid: '60198' }, { id: 'wy__3812895', name: 'Beatport全球电子舞曲榜', bangid: '3812895' }, { id: 'wy__21845217', name: 'KTV唛榜', bangid: '21845217' }, { id: 'wy__60131', name: '日本Oricon榜', bangid: '60131' }, - { id: 'wy__2809513713', name: '云音乐欧美热歌榜', bangid: '2809513713' }, - { id: 'wy__2809577409', name: '云音乐欧美新歌榜', bangid: '2809577409' }, + { id: 'wy__2809513713', name: '欧美热歌榜', bangid: '2809513713' }, + { id: 'wy__2809577409', name: '欧美新歌榜', bangid: '2809577409' }, { id: 'wy__27135204', name: '法国 NRJ Vos Hits 周榜', bangid: '27135204' }, - { id: 'wy__3001835560', name: '云音乐ACG动画榜', bangid: '3001835560' }, - { id: 'wy__3001795926', name: '云音乐ACG游戏榜', bangid: '3001795926' }, - { id: 'wy__3001890046', name: '云音乐ACG VOCALOID榜', bangid: '3001890046' }, + { id: 'wy__3001835560', name: 'ACG动画榜', bangid: '3001835560' }, + { id: 'wy__3001795926', name: 'ACG游戏榜', bangid: '3001795926' }, + { id: 'wy__3001890046', name: 'ACG VOCALOID榜', bangid: '3001890046' }, { id: 'wy__3112516681', name: '中国新乡村音乐排行榜', bangid: '3112516681' }, - { id: 'wy__5059644681', name: '云音乐日语榜', bangid: '5059644681' }, - { id: 'wy__5059633707', name: '云音乐摇滚榜', bangid: '5059633707' }, - { id: 'wy__5059642708', name: '云音乐国风榜', bangid: '5059642708' }, + { id: 'wy__5059644681', name: '日语榜', bangid: '5059644681' }, + { id: 'wy__5059633707', name: '摇滚榜', bangid: '5059633707' }, + { id: 'wy__5059642708', name: '国风榜', bangid: '5059642708' }, { id: 'wy__5338990334', name: '潜力爆款榜', bangid: '5338990334' }, - { id: 'wy__5059661515', name: '云音乐民谣榜', bangid: '5059661515' }, + { id: 'wy__5059661515', name: '民谣榜', bangid: '5059661515' }, { id: 'wy__6688069460', name: '听歌识曲榜', bangid: '6688069460' }, { id: 'wy__6723173524', name: '网络热歌榜', bangid: '6723173524' }, { id: 'wy__6732051320', name: '俄语榜', bangid: '6732051320' }, diff --git a/src/renderer/utils/musicSdk/wy/songList.js b/src/renderer/utils/musicSdk/wy/songList.js index cd719cac..9e485387 100644 --- a/src/renderer/utils/musicSdk/wy/songList.js +++ b/src/renderer/utils/musicSdk/wy/songList.js @@ -5,9 +5,10 @@ import { weapi, linuxapi } from './utils/crypto' import { httpFetch } from '../../request' -import { formatPlayTime, sizeFormate, dateFormat } from '../../index' +import { formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../../index' import musicDetailApi from './musicDetail' import { eapiRequest } from './utils/index' +import { formatSingerName } from '../utils' export default { _requestObj_tags: null, @@ -31,22 +32,6 @@ export default { listDetailLink: /^.+(?:\?|&)id=(\d+)(?:&.*$|#.*$|$)/, listDetailLink2: /^.+\/playlist\/(\d+)\/\d+\/.+$/, }, - /** - * 格式化播放数量 - * @param {*} num - */ - formatPlayCount(num) { - if (num > 100000000) return parseInt(num / 10000000) / 10 + '亿' - if (num > 10000) return parseInt(num / 1000) / 10 + '万' - return num - }, - getSinger(singers) { - let arr = [] - singers?.forEach(singer => { - arr.push(singer.name) - }) - return arr.join('、') - }, async handleParseId(link, retryNum = 0) { if (retryNum > 2) throw new Error('link try max num') @@ -130,7 +115,7 @@ export default { total: body.playlist.trackIds.length, source: 'wy', info: { - play_count: this.formatPlayCount(body.playlist.playCount), + play_count: formatPlayCount(body.playlist.playCount), name: body.playlist.name, img: body.playlist.coverImgUrl, desc: body.playlist.description, @@ -198,7 +183,7 @@ export default { }) } else { list.push({ - singer: this.getSinger(item.ar), + singer: formatSingerName(item.ar, 'name'), name: item.name ?? '', albumName: item.al?.name, albumId: item.al?.id, @@ -232,7 +217,7 @@ export default { }), }) return this._requestObj_list.promise.then(({ body }) => { - console.log(body) + // console.log(body) if (body.code !== this.successCode) return this.getList(sortId, tagId, page, ++tryNum) return { list: this.filterList(body.playlists), @@ -246,7 +231,7 @@ export default { filterList(rawData) { // console.log(rawData) return rawData.map(item => ({ - play_count: this.formatPlayCount(item.playCount), + play_count: formatPlayCount(item.playCount), id: item.id, author: item.creator.nickname, name: item.name, diff --git a/src/renderer/utils/musicSdk/wy/tipSearch.js b/src/renderer/utils/musicSdk/wy/tipSearch.js new file mode 100644 index 00000000..34afa1d7 --- /dev/null +++ b/src/renderer/utils/musicSdk/wy/tipSearch.js @@ -0,0 +1,33 @@ +import { httpFetch } from '../../request' +import { weapi } from './utils/crypto' +import { formatSingerName } from '../utils' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = httpFetch('https://music.163.com/weapi/search/suggest/web', { + method: 'POST', + headers: { + referer: 'https://music.163.com/', + origin: 'https://music.163.com/', + }, + form: weapi({ + s: str, + }), + }) + return this.requestObj.promise.then(({ statusCode, body }) => { + if (statusCode != 200 || body.code != 200) return Promise.reject(new Error('请求失败')) + return body.result.songs + }) + }, + handleResult(rawData) { + return rawData.map(info => `${info.name} - ${formatSingerName(info.artists, 'name')}`) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +}