refactor(sdk): musicSdk 全量迁移至 TypeScript
This commit is contained in:
1 parent
f289df4cd7
commit
1dfb7a59ca
83 files changed
+924
-732
No files matched your search
@@ -0,0 +1,163 @@
|
||||
import { sizeFormate, formatPlayTime } from '../index.ts'
|
||||
import { createHttpFetch } from './utils/index.ts'
|
||||
import { formatSingerName } from '../utils.ts'
|
||||
import type { SongInfo, SongType, QualityInfo, Quality } from '../types.ts'
|
||||
|
||||
const createGetMusicInfosTask = (ids: Array<string | undefined>) => {
|
||||
let list = ids
|
||||
const tasks: Array<Array<string | undefined>> = []
|
||||
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 filterMusicInfoList = (rawList: any[]): SongInfo[] => {
|
||||
// console.log(rawList)
|
||||
let ids = new Set()
|
||||
const list: any[] = []
|
||||
rawList.forEach(item => {
|
||||
if (!item.songId || ids.has(item.songId)) return
|
||||
ids.add(item.songId)
|
||||
const types: SongType[] = []
|
||||
const _types: Partial<Record<Quality, QualityInfo>> = {}
|
||||
item.newRateFormats?.forEach(type => {
|
||||
let size
|
||||
switch (type.formatType) {
|
||||
case 'PQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: '128k', size })
|
||||
_types['128k'] = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'HQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: '320k', size })
|
||||
_types['320k'] = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'SQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: 'flac', size })
|
||||
_types.flac = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'ZQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: 'flac24bit', size })
|
||||
_types.flac24bit = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
const intervalTest = /(\d\d:\d\d)$/.test(item.length)
|
||||
|
||||
list.push({
|
||||
singer: formatSingerName(item.artists, 'name'),
|
||||
name: item.songName,
|
||||
albumName: item.album,
|
||||
albumId: item.albumId,
|
||||
songmid: item.songId,
|
||||
copyrightId: item.copyrightId,
|
||||
source: 'mg',
|
||||
interval: intervalTest ? RegExp.$1 : null,
|
||||
img: item.albumImgs?.length ? item.albumImgs[0].img : null,
|
||||
lrc: null,
|
||||
lrcUrl: item.lrcUrl,
|
||||
mrcUrl: item.mrcUrl,
|
||||
trcUrl: item.trcUrl,
|
||||
otherSource: null,
|
||||
types,
|
||||
_types,
|
||||
typeUrl: {},
|
||||
})
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
export const filterMusicInfoListV5 = (rawList) => {
|
||||
// console.log(rawList)
|
||||
let ids = new Set()
|
||||
const list: any[] = []
|
||||
rawList.forEach(item => {
|
||||
if (!item.songId || ids.has(item.songId)) return
|
||||
ids.add(item.songId)
|
||||
const types: SongType[] = []
|
||||
const _types: Partial<Record<Quality, QualityInfo>> = {}
|
||||
item.audioFormats?.forEach(type => {
|
||||
let size
|
||||
switch (type.formatType) {
|
||||
case 'PQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: '128k', size })
|
||||
_types['128k'] = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'HQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: '320k', size })
|
||||
_types['320k'] = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'SQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: 'flac', size })
|
||||
_types.flac = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
case 'ZQ':
|
||||
size = sizeFormate(type.size ?? type.androidSize)
|
||||
types.push({ type: 'flac24bit', size })
|
||||
_types.flac24bit = {
|
||||
size,
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
list.push({
|
||||
singer: formatSingerName(item.singerList, 'name'),
|
||||
name: item.songName,
|
||||
albumName: item.album,
|
||||
albumId: item.albumId,
|
||||
songmid: item.songId,
|
||||
copyrightId: item.copyrightId,
|
||||
source: 'mg',
|
||||
interval: formatPlayTime(item.duration),
|
||||
img: item.img3 || item.img2 || item.img1 || null,
|
||||
lrc: null,
|
||||
lrcUrl: item.lrcUrl,
|
||||
mrcUrl: item.mrcUrl,
|
||||
trcUrl: item.trcUrl,
|
||||
otherSource: null,
|
||||
types,
|
||||
_types,
|
||||
typeUrl: {},
|
||||
})
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
export const getMusicInfo = async(copyrightId?: string): Promise<SongInfo | undefined> => {
|
||||
return getMusicInfos([copyrightId]).then(data => data[0])
|
||||
}
|
||||
|
||||
export const getMusicInfos = async(copyrightIds: Array<string | undefined>): Promise<SongInfo[]> => {
|
||||
return filterMusicInfoList((await createGetMusicInfosTask(copyrightIds)).flat())
|
||||
}
|
||||
Reference in new issue
Block a user