自定义源新增支持local源的musicUrl、pic、lyric的获取操作

This commit is contained in:
lyswhut
2024-01-30 19:25:01 +08:00
parent 7893814da4
commit 1cd93344dd
7 changed files with 186 additions and 15 deletions
+25
View File
@@ -6,8 +6,11 @@ import { getLocalFilePath } from '@renderer/utils/music'
import {
buildLyricInfo,
getCachedLyricInfo,
getOnlineOtherSourceLyricByLocal,
getOnlineOtherSourceLyricInfo,
getOnlineOtherSourceMusicUrl,
getOnlineOtherSourceMusicUrlByLocal,
getOnlineOtherSourcePicByLocal,
getOnlineOtherSourcePicUrl,
getOtherSource,
} from './utils'
@@ -72,6 +75,14 @@ export const getMusicUrl = async({ musicInfo, isRefresh, onToggleSource = () =>
const path = await getLocalFilePath(musicInfo)
if (path) return encodePath(path)
}
try {
return await getOnlineOtherSourceMusicUrlByLocal(musicInfo, isRefresh).then(({ url, quality, isFromCache }) => {
if (!isFromCache) void saveMusicUrl(musicInfo, quality, url)
return url
})
} catch {}
onToggleSource()
const otherSource = await getOtherSourceByLocal(musicInfo)
if (!otherSource.length) throw new Error('source not found')
@@ -97,6 +108,12 @@ export const getPicUrl = async({ musicInfo, listId, isRefresh, onToggleSource =
if (musicInfo.meta.picUrl) return musicInfo.meta.picUrl
}
try {
return await getOnlineOtherSourcePicByLocal(musicInfo).then(({ url }) => {
return url
})
} catch {}
onToggleSource()
const otherSource = await getOtherSourceByLocal(musicInfo)
if (!otherSource.length) throw new Error('source not found')
@@ -126,6 +143,14 @@ export const getLyricInfo = async({ musicInfo, isRefresh, onToggleSource = () =>
if (lyricInfo?.lyric) return buildLyricInfo(lyricInfo)
}
try {
// eslint-disable-next-line @typescript-eslint/promise-function-async
return await getOnlineOtherSourceLyricByLocal(musicInfo, isRefresh).then(({ lyricInfo, isFromCache }) => {
if (!isFromCache) void saveLyric(musicInfo, lyricInfo)
return buildLyricInfo(lyricInfo)
})
} catch {}
onToggleSource()
const otherSource = await getOtherSourceByLocal(musicInfo)
if (!otherSource.length) throw new Error('source not found')
+63
View File
@@ -10,6 +10,7 @@ import {
import { appSetting } from '@renderer/store/setting'
import { langS2T, toNewMusicInfo, toOldMusicInfo } from '@renderer/utils'
import { requestMsg } from '@renderer/utils/message'
import { apis } from '@renderer/utils/musicSdk/api-source'
const getOtherSourcePromises = new Map()
@@ -147,6 +148,68 @@ export const getCachedLyricInfo = async(musicInfo: LX.Music.MusicInfo): Promise<
return null
}
export const getOnlineOtherSourceMusicUrlByLocal = async(musicInfo: LX.Music.MusicInfoLocal, isRefresh: boolean): Promise<{
url: string
quality: LX.Quality
isFromCache: boolean
}> => {
if (!await window.lx.apiInitPromise[0]) throw new Error('source init failed')
const quality = '128k'
const cachedUrl = await getStoreMusicUrl(musicInfo, quality)
if (cachedUrl && !isRefresh) return { url: cachedUrl, quality, isFromCache: true }
let reqPromise
try {
reqPromise = apis('local').getMusicUrl(toOldMusicInfo(musicInfo), null).promise
} catch (err: any) {
reqPromise = Promise.reject(err)
}
return reqPromise.then(({ url }: { url: string }) => {
return { url, quality, isFromCache: false }
})
}
export const getOnlineOtherSourceLyricByLocal = async(musicInfo: LX.Music.MusicInfoLocal, isRefresh: boolean): Promise<{
lyricInfo: LX.Music.LyricInfo
isFromCache: boolean
}> => {
if (!await window.lx.apiInitPromise[0]) throw new Error('source init failed')
const lyricInfo = await getCachedLyricInfo(musicInfo)
if (lyricInfo && !isRefresh) return { lyricInfo, isFromCache: true }
let reqPromise
try {
reqPromise = apis('local').getLyric(toOldMusicInfo(musicInfo)).promise
} catch (err: any) {
reqPromise = Promise.reject(err)
}
return reqPromise.then((lyricInfo: LX.Music.LyricInfo) => {
return { lyricInfo, isFromCache: false }
})
}
export const getOnlineOtherSourcePicByLocal = async(musicInfo: LX.Music.MusicInfoLocal): Promise<{
url: string
}> => {
if (!await window.lx.apiInitPromise[0]) throw new Error('source init failed')
let reqPromise
try {
reqPromise = apis('local').getPic(toOldMusicInfo(musicInfo)).promise
} catch (err: any) {
reqPromise = Promise.reject(err)
}
return reqPromise.then((url: string) => {
return { url }
})
}
export const getPlayQuality = (highQuality: boolean, musicInfo: LX.Music.MusicInfoOnline): LX.Quality => {
let type: LX.Quality = '128k'
let list = qualityList.value[musicInfo.source]
+56 -2
View File
@@ -57,7 +57,6 @@ export default () => {
// eslint-disable-next-line @typescript-eslint/promise-function-async
}).then(res => {
// console.log(res)
if (!/^https?:/.test(res.data.url)) return Promise.reject(new Error('Get url failed'))
return { type, url: res.data.url }
}).catch(async err => {
console.log(err.message)
@@ -66,7 +65,62 @@ export default () => {
}
}
break
case 'lyric':
apis[source].getLyric = (songInfo: LX.Music.MusicInfo) => {
const requestKey = `request__${Math.random().toString().substring(2)}`
return {
canceleFn() {
userApiRequestCancel(requestKey)
},
promise: sendUserApiRequest({
requestKey,
data: {
source,
action: 'lyric',
info: {
type,
musicInfo: songInfo,
},
},
// eslint-disable-next-line @typescript-eslint/promise-function-async
}).then(res => {
// console.log(res)
return res.data
}).catch(async err => {
console.log(err.message)
return Promise.reject(err)
}),
}
}
break
case 'pic':
apis[source].getPic = (songInfo: LX.Music.MusicInfo) => {
const requestKey = `request__${Math.random().toString().substring(2)}`
return {
canceleFn() {
userApiRequestCancel(requestKey)
},
promise: sendUserApiRequest({
requestKey,
data: {
source,
action: 'pic',
info: {
type,
musicInfo: songInfo,
},
},
// eslint-disable-next-line @typescript-eslint/promise-function-async
}).then(res => {
// console.log(res)
return res.data
}).catch(async err => {
console.log(err.message)
return Promise.reject(err)
}),
}
}
break
default:
break
}