分离歌曲URL的缓存

pull/495/head
lyswhut 2021-04-05 19:30:06 +08:00
parent c6853b197c
commit 1c585dc479
9 changed files with 61 additions and 28 deletions

View File

@ -63,6 +63,9 @@ const names = {
get_lyric: 'get_lyric',
save_lyric: 'save_lyric',
clear_lyric: 'clear_lyric',
get_music_url: 'get_music_url',
save_music_url: 'save_music_url',
clear_music_url: 'clear_music_url',
},
winLyric: {
close: 'close',

View File

@ -18,6 +18,7 @@ require('./showDialog')
require('./playList')
require('./data')
require('./lyric')
require('./musicUrl')
require('./kw_decodeLyric')

View File

@ -2,9 +2,9 @@ const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = requir
const getStore = require('@common/store')
mainHandle(ipcMainWindowNames.get_lyric, async(event, id) => getStore('lyrics').get(id) || {})
mainHandle(ipcMainWindowNames.get_lyric, async(event, id) => getStore('lyrics', true, false).get(id) || {})
mainOn(ipcMainWindowNames.save_lyric, (event, { id, lyrics }) => getStore('lyrics').set(id, lyrics))
mainOn(ipcMainWindowNames.save_lyric, (event, { id, lyrics }) => getStore('lyrics', true, false).set(id, lyrics))
mainOn(ipcMainWindowNames.clear_lyric, () => getStore('lyrics').clear())
mainOn(ipcMainWindowNames.clear_lyric, () => getStore('lyrics', true, false).clear())

View File

@ -0,0 +1,10 @@
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = require('../../common/ipc')
const getStore = require('@common/store')
mainHandle(ipcMainWindowNames.get_music_url, async(event, id) => getStore('musicUrls', true, false).get(id) || '')
mainOn(ipcMainWindowNames.save_music_url, (event, { id, url }) => getStore('musicUrls', true, false).set(id, url))
mainOn(ipcMainWindowNames.clear_music_url, () => getStore('musicUrls', true, false).clear())

View File

@ -607,7 +607,7 @@ export default {
if (!retryedSource.includes(targetSong.source)) retryedSource.push(targetSong.source)
let type = this.getPlayType(this.setting.player.highQuality, targetSong)
this.musicInfo.url = targetSong.typeUrl[type]
// this.musicInfo.url = await getMusicUrl(targetSong, type)
this.status = this.statusText = this.$t('core.player.geting_url')
return this.getUrl({ musicInfo: targetSong, originMusic, type, isRefresh }).then(url => {

View File

@ -3,7 +3,7 @@ import fs from 'fs'
import path from 'path'
import music from '../../utils/music'
import { getMusicType } from '../../utils/music/utils'
import { setMeta, saveLrc, getLyric, setLyric } from '../../utils'
import { setMeta, saveLrc, getLyric, setLyric, getMusicUrl, setMusicUrl } from '../../utils'
// state
const state = {
@ -147,15 +147,20 @@ const pauseTasks = async(store, list, runs = []) => {
await pauseTasks(store, list, runs)
}
const getUrl = (downloadInfo, isRefresh) => {
const url = downloadInfo.musicInfo.typeUrl[downloadInfo.type]
const getUrl = async(downloadInfo, isRefresh) => {
const cachedUrl = await getMusicUrl(downloadInfo.musicInfo, downloadInfo.type)
if (!downloadInfo.musicInfo._types[downloadInfo.type]) {
// 兼容旧版酷我源搜索列表过滤128k音质的bug
if (!(downloadInfo.musicInfo.source == 'kw' && downloadInfo.type == '128k')) return Promise.reject(new Error('该歌曲没有可下载的音频'))
if (!(downloadInfo.musicInfo.source == 'kw' && downloadInfo.type == '128k')) throw new Error('该歌曲没有可下载的音频')
// return Promise.reject(new Error('该歌曲没有可下载的音频'))
}
return url && !isRefresh ? Promise.resolve({ url }) : music[downloadInfo.musicInfo.source].getMusicUrl(downloadInfo.musicInfo, downloadInfo.type).promise
return cachedUrl && !isRefresh
? cachedUrl
: music[downloadInfo.musicInfo.source].getMusicUrl(downloadInfo.musicInfo, downloadInfo.type).promise.then(({ url }) => {
setMusicUrl(downloadInfo.musicInfo, downloadInfo.type, url)
return url
})
}
// 修复 1.1.x版本 酷狗源歌词格式
@ -228,12 +233,12 @@ const downloadLyric = (downloadInfo, filePath) => {
const refreshUrl = function(commit, downloadInfo) {
commit('setStatusText', { downloadInfo, text: '链接失效,正在刷新链接' })
getUrl(downloadInfo, true).then(result => {
commit('updateUrl', { downloadInfo, url: result.url })
getUrl(downloadInfo, true).then(url => {
commit('updateUrl', { downloadInfo, url })
commit('setStatusText', { downloadInfo, text: '链接刷新成功' })
const dl = dls[downloadInfo.key]
if (!dl) return
dl.refreshUrl(result.url)
dl.refreshUrl(url)
dl.start().catch(err => {
commit('onError', { downloadInfo, errorMsg: err.message })
commit('setStatusText', { downloadInfo, text: err.message })
@ -389,10 +394,10 @@ const actions = {
commit('setStatusText', { downloadInfo, text: '获取URL中...' })
let p = options.url
? Promise.resolve()
: getUrl(downloadInfo).then(result => {
commit('updateUrl', { downloadInfo, url: result.url })
if (!result.url) return Promise.reject(new Error('获取URL失败'))
options.url = result.url
: getUrl(downloadInfo).then(url => {
commit('updateUrl', { downloadInfo, url })
if (!url) return Promise.reject(new Error('获取URL失败'))
options.url = url
})
p.then(() => {
tryNum[downloadInfo.key] = 0

View File

@ -1,5 +1,5 @@
import musicSdk from '../../utils/music'
import { clearLyric } from '../../utils'
import { clearLyric, clearMusicUrl } from '../../utils'
let allList = {}
window.allList = allList
@ -215,6 +215,7 @@ const mutations = {
if (item.tlrc) delete item.tlrc
}
}
clearMusicUrl()
clearLyric()
},
setOtherSource(state, { musicInfo, otherSource }) {

View File

@ -1,6 +1,13 @@
import path from 'path'
import music from '../../utils/music'
import { getRandom, checkPath, getLyric as getStoreLyric, setLyric } from '../../utils'
import {
getRandom,
checkPath,
getLyric as getStoreLyric,
setLyric,
setMusicUrl,
getMusicUrl,
} from '../../utils'
// state
const state = {
@ -147,20 +154,19 @@ const getters = {
// actions
const actions = {
getUrl({ commit, state }, { musicInfo, originMusic, type, isRefresh }) {
async getUrl({ commit, state }, { musicInfo, originMusic, type, isRefresh }) {
if (!musicInfo._types[type]) {
// 兼容旧版酷我源搜索列表过滤128k音质的bug
if (!(musicInfo.source == 'kw' && type == '128k')) return Promise.reject(new Error('该歌曲没有可播放的音频'))
if (!(musicInfo.source == 'kw' && type == '128k')) throw new Error('该歌曲没有可播放的音频')
// return Promise.reject(new Error('该歌曲没有可播放的音频'))
}
if (urlRequest && urlRequest.cancelHttp) urlRequest.cancelHttp()
if (musicInfo.typeUrl[type] && !isRefresh) return Promise.resolve(musicInfo.typeUrl[type])
try {
urlRequest = music[musicInfo.source].getMusicUrl(musicInfo, type)
} catch (err) {
return Promise.reject(err)
}
const cachedUrl = await getMusicUrl(musicInfo, type)
if (cachedUrl && !isRefresh) return cachedUrl
urlRequest = music[musicInfo.source].getMusicUrl(musicInfo, type)
return urlRequest.promise.then(({ url }) => {
if (originMusic) commit('setUrl', { musicInfo: originMusic, url, type })
commit('setUrl', { musicInfo, url, type })
@ -328,8 +334,8 @@ const actions = {
// mitations
const mutations = {
setUrl(state, datas) {
datas.musicInfo.typeUrl = Object.assign({}, datas.musicInfo.typeUrl, { [datas.type]: datas.url })
setUrl(state, { musicInfo, type, url }) {
setMusicUrl(musicInfo, type, url)
},
getPic(state, datas) {
datas.musicInfo.img = datas.url

View File

@ -414,3 +414,10 @@ export const setLyric = (musicInfo, { lyric, tlyric, lxlyric }) => rendererSend(
lyrics: { lyric, tlyric, lxlyric },
})
export const clearLyric = () => rendererSend(NAMES.mainWindow.clear_lyric)
export const getMusicUrl = (musicInfo, type) => rendererInvoke(NAMES.mainWindow.get_music_url, `${musicInfo.source}_${musicInfo.songmid}_${type}`)
export const setMusicUrl = (musicInfo, type, url) => rendererSend(NAMES.mainWindow.save_music_url, {
id: `${musicInfo.source}_${musicInfo.songmid}_${type}`,
url,
})
export const clearMusicUrl = () => rendererSend(NAMES.mainWindow.clear_music_url)