Skip to content

Commit ed9cf9c

Browse files
committed
✨ feat: 优化音源解析功能,添加音源配置
1 parent 35b9cbf commit ed9cf9c

File tree

12 files changed

+163
-16
lines changed

12 files changed

+163
-16
lines changed

src/i18n/lang/en-US/settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ export default {
5656
dolby: 'Dolby Atmos',
5757
jymaster: 'Master'
5858
},
59+
musicSources: 'Music Sources',
60+
musicSourcesDesc: 'Select music sources for song resolution',
61+
musicSourcesWarning: 'At least one music source must be selected',
62+
musicUnblockEnable: 'Enable Music Unblocking',
63+
musicUnblockEnableDesc: 'When enabled, attempts to resolve unplayable songs',
64+
configureMusicSources: 'Configure Sources',
65+
selectedMusicSources: 'Selected sources:',
66+
noMusicSources: 'No sources selected',
5967
autoPlay: 'Auto Play',
6068
autoPlayDesc: 'Auto resume playback when reopening the app'
6169
},

src/i18n/lang/zh-CN/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"playback": {
2+
"musicSources": "音源设置",
3+
"musicSourcesDesc": "选择音乐解析使用的音源平台",
4+
"musicSourcesWarning": "至少需要选择一个音源平台"
5+
}

src/i18n/lang/zh-CN/settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ export default {
5656
dolby: '杜比全景声',
5757
jymaster: '超清母带'
5858
},
59+
musicSources: '音源设置',
60+
musicSourcesDesc: '选择音乐解析使用的音源平台',
61+
musicSourcesWarning: '至少需要选择一个音源平台',
62+
musicUnblockEnable: '启用音乐解析',
63+
musicUnblockEnableDesc: '开启后将尝试解析无法播放的音乐',
64+
configureMusicSources: '配置音源',
65+
selectedMusicSources: '已选音源:',
66+
noMusicSources: '未选择音源',
5967
autoPlay: '自动播放',
6068
autoPlayDesc: '重新打开应用时是否自动继续播放'
6169
},

src/main/server.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ import server from 'netease-cloud-music-api-alger/server';
55
import os from 'os';
66
import path from 'path';
77

8-
import { unblockMusic } from './unblockMusic';
8+
import { unblockMusic, type Platform } from './unblockMusic';
99

1010
const store = new Store();
1111
if (!fs.existsSync(path.resolve(os.tmpdir(), 'anonymous_token'))) {
1212
fs.writeFileSync(path.resolve(os.tmpdir(), 'anonymous_token'), '', 'utf-8');
1313
}
1414

15-
// 处理解锁音乐请求
16-
ipcMain.handle('unblock-music', async (_, id, data) => {
17-
return unblockMusic(id, data);
15+
// 设置音乐解析的处理程序
16+
ipcMain.handle('unblock-music', async (_event, id, songData, enabledSources) => {
17+
try {
18+
const result = await unblockMusic(id, songData, 1, enabledSources as Platform[]);
19+
return result;
20+
} catch (error) {
21+
console.error('音乐解析失败:', error);
22+
return { error: (error as Error).message || '未知错误' };
23+
}
1824
});
1925

2026
async function startMusicApi(): Promise<void> {

src/main/set.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@
2121
"downloadPath": "",
2222
"language": "zh-CN",
2323
"alwaysShowDownloadButton": false,
24-
"unlimitedDownload": false
24+
"unlimitedDownload": false,
25+
"enableMusicUnblock": true,
26+
"enabledMusicSources": ["migu", "kugou", "pyncmd","bilibili", "youtube"]
2527
}

src/main/unblockMusic.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ interface SongData {
66
name: string;
77
artists: Array<{ name: string }>;
88
album?: { name: string };
9+
ar?: Array<{ name: string }>;
10+
al?: { name: string };
911
}
1012

1113
interface ResponseData {
@@ -27,24 +29,29 @@ interface UnblockResult {
2729
};
2830
}
2931

32+
// 所有可用平台
33+
export const ALL_PLATFORMS: Platform[] = ['migu', 'kugou', 'pyncmd', 'kuwo', 'bilibili', 'youtube'];
34+
3035
/**
3136
* 音乐解析函数
3237
* @param id 歌曲ID
3338
* @param songData 歌曲信息
3439
* @param retryCount 重试次数
40+
* @param enabledPlatforms 启用的平台列表,默认为所有平台
3541
* @returns Promise<UnblockResult>
3642
*/
3743
const unblockMusic = async (
3844
id: number | string,
3945
songData: SongData,
40-
retryCount = 3
46+
retryCount = 1,
47+
enabledPlatforms?: Platform[]
4148
): Promise<UnblockResult> => {
42-
// 所有可用平台
43-
const platforms: Platform[] = ['migu', 'kugou', 'pyncmd', 'joox', 'kuwo', 'bilibili', 'youtube'];
44-
49+
const platforms = enabledPlatforms || ALL_PLATFORMS;
50+
songData.album = songData.album || songData.al;
51+
songData.artists = songData.artists || songData.ar;
4552
const retry = async (attempt: number): Promise<UnblockResult> => {
4653
try {
47-
const data = await match(parseInt(String(id), 10), platforms, songData);
54+
const data = await match(parseInt(String(id), 10), platforms,songData);
4855
const result: UnblockResult = {
4956
data: {
5057
data,
@@ -58,7 +65,7 @@ const unblockMusic = async (
5865
} catch (err) {
5966
if (attempt < retryCount) {
6067
// 延迟重试,每次重试增加延迟时间
61-
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt));
68+
await new Promise((resolve) => setTimeout(resolve, 100 * attempt));
6269
return retry(attempt + 1);
6370
}
6471

src/preload/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface API {
1414
openLyric: () => void;
1515
sendLyric: (data: any) => void;
1616
sendSong: (data: any) => void;
17-
unblockMusic: (id: number, data: any) => Promise<any>;
17+
unblockMusic: (id: number, data: any, enabledSources?: string[]) => Promise<any>;
1818
onLyricWindowClosed: (callback: () => void) => void;
1919
startDownload: (url: string) => void;
2020
onDownloadProgress: (callback: (progress: number, status: string) => void) => void;

src/preload/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const api = {
1616
openLyric: () => ipcRenderer.send('open-lyric'),
1717
sendLyric: (data) => ipcRenderer.send('send-lyric', data),
1818
sendSong: (data) => ipcRenderer.send('update-current-song', data),
19-
unblockMusic: (id) => ipcRenderer.invoke('unblock-music', id),
19+
unblockMusic: (id, data, enabledSources) => ipcRenderer.invoke('unblock-music', id, data, enabledSources),
2020
// 歌词窗口关闭事件
2121
onLyricWindowClosed: (callback: () => void) => {
2222
ipcRenderer.on('lyric-window-closed', () => callback());

src/renderer/api/music.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ILyric } from '@/type/lyric';
44
import { isElectron } from '@/utils';
55
import request from '@/utils/request';
66
import requestMusic from '@/utils/request_music';
7+
import { cloneDeep } from 'lodash';
78

89
const { addData, getData, deleteData } = musicDB;
910

@@ -79,8 +80,15 @@ export const getMusicLrc = async (id: number) => {
7980
};
8081

8182
export const getParsingMusicUrl = (id: number, data: any) => {
83+
8284
if (isElectron) {
83-
return window.api.unblockMusic(id, data);
85+
const settingStore = useSettingsStore();
86+
87+
// 如果禁用了音乐解析功能,则直接返回空结果
88+
if (!settingStore.setData.enableMusicUnblock) {
89+
return Promise.resolve({ data: { code: 404, message: '音乐解析功能已禁用' } });
90+
}
91+
return window.api.unblockMusic(id, cloneDeep(data), cloneDeep(settingStore.setData.enabledMusicSources));
8492
}
8593
return requestMusic.get<any>('/music', { params: { id } });
8694
};

src/renderer/components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ declare module 'vue' {
2828
NEmpty: typeof import('naive-ui')['NEmpty']
2929
NForm: typeof import('naive-ui')['NForm']
3030
NFormItem: typeof import('naive-ui')['NFormItem']
31+
NGrid: typeof import('naive-ui')['NGrid']
32+
NGridItem: typeof import('naive-ui')['NGridItem']
3133
NIcon: typeof import('naive-ui')['NIcon']
3234
NImage: typeof import('naive-ui')['NImage']
3335
NInput: typeof import('naive-ui')['NInput']

0 commit comments

Comments
 (0)