molodcd commited on
Commit
eef43a2
·
verified ·
1 Parent(s): 6deefd3

Upload aggregated-music-sources.js

Browse files
Files changed (1) hide show
  1. img/u4aiEepo.js +317 -0
img/u4aiEepo.js ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*!
2
+ * @name 聚合音源 (负载均衡版)
3
+ * @description 合并多个付费音源,实现负载均衡与故障转移
4
+ * @version v1.0.0
5
+ * @author Roo
6
+ */
7
+
8
+ const DEV_ENABLE = false;
9
+ const UPDATE_ENABLE = true;
10
+
11
+ // 音源配置列表
12
+ const SOURCE_CONFIGS = [
13
+ // ikun 音源 1
14
+ {
15
+ id: 'ikun1',
16
+ name: 'ikun音源(1)',
17
+ apiUrl: 'https://api.ikunshare.com',
18
+ apiKey: 'KAWANG_902abcd8-OKFLX66ITQEROT6U',
19
+ scriptMd5: '573c5ba8835ae6146e9c19f449e3b045',
20
+ headerKey: 'X-Request-Key',
21
+ enabled: true,
22
+ weight: 1,
23
+ },
24
+ // ikun 音源 2
25
+ {
26
+ id: 'ikun2',
27
+ name: 'ikun音源(2)',
28
+ apiUrl: 'https://api.ikunshare.com',
29
+ apiKey: 'KAWANG_af0d6f80-L3SSWURLQPKQBNTX',
30
+ scriptMd5: '34c5a35cd9e204d0ac5eb5e11f8749cc',
31
+ headerKey: 'X-Request-Key',
32
+ enabled: true,
33
+ weight: 1,
34
+ },
35
+ // 新澜音源 1
36
+ {
37
+ id: 'xinlan1',
38
+ name: '新澜音源(1)',
39
+ apiUrl: 'https://source.shiqianjiang.cn/api/music',
40
+ apiKey: 'CERU_KEY-11490507CC102A1BBD8FB5F1299A3F8E',
41
+ scriptMd5: '6cd9d36abc88c641032e483b58c9ec90',
42
+ headerKey: 'X-API-Key',
43
+ enabled: true,
44
+ weight: 1,
45
+ },
46
+ // 新澜音源 2
47
+ {
48
+ id: 'xinlan2',
49
+ name: '新澜音源(2)',
50
+ apiUrl: 'https://source.shiqianjiang.cn/api/music',
51
+ apiKey: 'CERU_KEY-FE367E7E8FFC1942B70ACE0A564AF398',
52
+ scriptMd5: '6cd9d36abc88c641032e483b58c9ec90',
53
+ headerKey: 'X-API-Key',
54
+ enabled: true,
55
+ weight: 1,
56
+ },
57
+ // 新澜音源 3
58
+ {
59
+ id: 'xinlan3',
60
+ name: '新澜音源(3)',
61
+ apiUrl: 'https://source.shiqianjiang.cn/api/music',
62
+ apiKey: 'CERU_KEY-41C278A488FA6573E18434A5081B6CE9',
63
+ scriptMd5: '6cd9d36abc88c641032e483b58c9ec90',
64
+ headerKey: 'X-API-Key',
65
+ enabled: true,
66
+ weight: 1,
67
+ },
68
+ // 新澜音源 4
69
+ {
70
+ id: 'xinlan4',
71
+ name: '新澜音源(4)',
72
+ apiUrl: 'https://source.shiqianjiang.cn/api/music',
73
+ apiKey: 'CERU_KEY-2BE8D135B48A0C06ED5238218E262B13',
74
+ scriptMd5: '6cd9d36abc88c641032e483b58c9ec90',
75
+ headerKey: 'X-API-Key',
76
+ enabled: true,
77
+ weight: 1,
78
+ },
79
+ // 聆川音源 1
80
+ {
81
+ id: 'lingchuan1',
82
+ name: '聆川音源(1)',
83
+ apiUrl: 'https://lc.guoyue2010.top/api/music',
84
+ apiKey: 'CERU_KEY-DFB4C5892BC852727D71CEB79A607003',
85
+ scriptMd5: 'c25672a07554fd763489e58bab59be7c',
86
+ headerKey: 'X-API-Key',
87
+ enabled: true,
88
+ weight: 1,
89
+ },
90
+ // 聆川音源 2
91
+ {
92
+ id: 'lingchuan2',
93
+ name: '聆川音源(2)',
94
+ apiUrl: 'https://lc.guoyue2010.top/api/music',
95
+ apiKey: 'CERU_KEY-A7F51CB80CA5C583A35D0B6789DB6097',
96
+ scriptMd5: 'c25672a07554fd763489e58bab59be7c',
97
+ headerKey: 'X-API-Key',
98
+ enabled: true,
99
+ weight: 1,
100
+ },
101
+ // 聆川音源 3
102
+ {
103
+ id: 'lingchuan3',
104
+ name: '聆川音源(3)',
105
+ apiUrl: 'https://lc.guoyue2010.top/api/music',
106
+ apiKey: 'CERU_KEY-2D944F1C0D4493DD6DEBEBE2EDD1BF1F',
107
+ scriptMd5: 'c25672a07554fd763489e58bab59be7c',
108
+ headerKey: 'X-API-Key',
109
+ enabled: true,
110
+ weight: 1,
111
+ },
112
+ // 聆川音源 4
113
+ {
114
+ id: 'lingchuan4',
115
+ name: '聆川音源(4)',
116
+ apiUrl: 'https://lc.guoyue2010.top/api/music',
117
+ apiKey: 'CERU_KEY-jrhHNFPEGfsPyCgtsKygxf9oop6858D5ubXkGjfsskYGUF6t',
118
+ scriptMd5: 'c25672a07554fd763489e58bab59be7c',
119
+ headerKey: 'X-API-Key',
120
+ enabled: true,
121
+ weight: 1,
122
+ },
123
+ ];
124
+
125
+ // 音乐质量定义 (合并了所有音源的支持情况)
126
+ const MUSIC_QUALITY = {
127
+ kw: ['128k', '320k', 'flac', 'flac24bit', 'hires'],
128
+ mg: ['128k', '320k', 'flac', 'flac24bit', 'hires'],
129
+ kg: ['128k', '320k', 'flac', 'flac24bit', 'hires', 'atmos', 'master'],
130
+ tx: ['128k', '320k', 'flac', 'flac24bit', 'hires', 'atmos', 'atmos_plus', 'master'],
131
+ wy: ['128k', '320k', 'flac', 'flac24bit', 'hires', 'atmos', 'master'],
132
+ git: ['128k', '320k', 'flac'],
133
+ };
134
+
135
+ const MUSIC_SOURCE = Object.keys(MUSIC_QUALITY);
136
+ const { EVENT_NAMES, request, on, send, utils, env, version } = globalThis.lx;
137
+
138
+ // HTTP 请求封装
139
+ const httpFetch = (url, options = { method: 'GET' }) => {
140
+ return new Promise((resolve, reject) => {
141
+ // console.log('--- start --- ' + url);
142
+ request(url, options, (err, resp) => {
143
+ if (err) return reject(err);
144
+ // console.log('API Response: ', resp);
145
+ resolve(resp);
146
+ });
147
+ });
148
+ };
149
+
150
+ // 辅助函数:加权随机选择音源 (支持排除已尝试的音源)
151
+ const selectSource = (excludeIds = []) => {
152
+ const enabledSources = SOURCE_CONFIGS.filter(s => s.enabled && !excludeIds.includes(s.id));
153
+ if (enabledSources.length === 0) return null;
154
+
155
+ const totalWeight = enabledSources.reduce((sum, s) => sum + (s.weight || 1), 0);
156
+ let random = Math.random() * totalWeight;
157
+
158
+ for (const source of enabledSources) {
159
+ random -= (source.weight || 1);
160
+ if (random < 0) return source;
161
+ }
162
+ return enabledSources[enabledSources.length - 1];
163
+ };
164
+
165
+ // 处理音乐 URL 请求 (带故障转移)
166
+ const handleGetMusicUrl = async (source, musicInfo, quality) => {
167
+ const songId = musicInfo.hash ?? musicInfo.songmid;
168
+ const maxAttempts = SOURCE_CONFIGS.length; // 最多尝试次数为可用音源总数
169
+ const triedIds = [];
170
+ let lastError = null;
171
+
172
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
173
+ const config = selectSource(triedIds);
174
+ if (!config) {
175
+ if (attempt === 0) throw new Error('没有可用的音源');
176
+ break; // 没有更多音源可试
177
+ }
178
+
179
+ triedIds.push(config.id);
180
+ console.log(`[第${attempt + 1}次尝试] 使用音源: ${config.name}`);
181
+
182
+ const requestUrl = `${config.apiUrl}/url?source=${source}&songId=${songId}&quality=${quality}`;
183
+ const headers = {
184
+ 'Content-Type': 'application/json',
185
+ 'User-Agent': env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`,
186
+ };
187
+ headers[config.headerKey] = config.apiKey;
188
+
189
+ try {
190
+ const resp = await httpFetch(requestUrl, {
191
+ method: 'GET',
192
+ headers,
193
+ follow_max: 5,
194
+ });
195
+
196
+ const { body } = resp;
197
+ if (!body || isNaN(Number(body.code))) throw new Error('响应格式错误');
198
+ if (env != 'mobile') console.groupEnd();
199
+
200
+ switch (body.code) {
201
+ case 200:
202
+ console.log(`成功获取 URL: ${body.url}`);
203
+ return body.url;
204
+ case 403:
205
+ console.warn(`音源 ${config.name} 鉴权失败 (403)`);
206
+ // 鉴权失败通常是永久性的,临时禁用该音源
207
+ config.enabled = false;
208
+ throw new Error('Key失效/鉴权失败');
209
+ case 429:
210
+ console.warn(`音源 ${config.name} 请求过速 (429)`);
211
+ throw new Error('请求过速');
212
+ case 500:
213
+ console.warn(`音源 ${config.name} 服务器错误 (500): ${body.message}`);
214
+ throw new Error(`服务端错误: ${body.message}`);
215
+ default:
216
+ console.warn(`音源 ${config.name} 返回异常 (${body.code}): ${body.message}`);
217
+ throw new Error(body.message ?? '未知错误');
218
+ }
219
+ } catch (err) {
220
+ lastError = err;
221
+ console.warn(`音源 ${config.name} 请求失败: ${err.message}`);
222
+ // 继续尝试下一个音源
223
+ }
224
+ }
225
+
226
+ // 所有尝试都失败
227
+ console.error(`所有音源均失败,最后错误: ${lastError?.message}`);
228
+ throw lastError || new Error('无法获取音乐 URL');
229
+ };
230
+
231
+ // 检查更新 (使用第一个可用音源)
232
+ const checkUpdate = async () => {
233
+ const config = SOURCE_CONFIGS.find(s => s.enabled);
234
+ if (!config) {
235
+ console.log('没有可用的音源用于检查更新');
236
+ return;
237
+ }
238
+
239
+ let updateUrl = '';
240
+ if (config.apiUrl.includes('ikunshare.com')) {
241
+ updateUrl = `${config.apiUrl}/script?key=${config.apiKey}&checkUpdate=${config.scriptMd5}`;
242
+ } else {
243
+ // 新澜/聆川风格的更新 URL
244
+ updateUrl = `${config.apiUrl.replace('/music', '')}/script?checkUpdate=${config.scriptMd5}&key=${config.apiKey}`;
245
+ }
246
+
247
+ try {
248
+ const resp = await httpFetch(updateUrl, {
249
+ method: 'GET',
250
+ headers: {
251
+ 'Content-Type': 'application/json',
252
+ 'User-Agent': env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`,
253
+ },
254
+ });
255
+ const { body } = resp;
256
+
257
+ if (!body || body.code !== 200) console.log('检查更新失败');
258
+ else {
259
+ console.log('检查更新成功');
260
+ if (body.data != null) {
261
+ globalThis.lx.send(lx.EVENT_NAMES.updateAlert, {
262
+ log: body.data.updateMsg,
263
+ updateUrl: body.data.updateUrl,
264
+ });
265
+ }
266
+ }
267
+ } catch (error) {
268
+ console.log('检查更新错误:', error);
269
+ }
270
+ };
271
+
272
+ // 构建 musicSources 映射
273
+ const musicSources = {};
274
+ MUSIC_SOURCE.forEach((item) => {
275
+ musicSources[item] = {
276
+ name: item,
277
+ type: 'music',
278
+ actions: ['musicUrl'],
279
+ qualitys: MUSIC_QUALITY[item],
280
+ };
281
+ });
282
+
283
+ // 监听请求事件
284
+ on(EVENT_NAMES.request, ({ action, source, info }) => {
285
+ switch (action) {
286
+ case 'musicUrl':
287
+ if (env != 'mobile') {
288
+ console.group(`Handle Action(musicUrl)`);
289
+ console.log('source', source);
290
+ console.log('quality', info.type);
291
+ console.log('musicInfo', info.musicInfo);
292
+ } else {
293
+ console.log(`Handle Action(musicUrl)`);
294
+ console.log('source', source);
295
+ console.log('quality', info.type);
296
+ console.log('musicInfo', info.musicInfo);
297
+ }
298
+ return handleGetMusicUrl(source, info.musicInfo, info.type)
299
+ .then((data) => Promise.resolve(data))
300
+ .catch((err) => Promise.reject(err));
301
+ default:
302
+ console.error(`action(${action}) not support`);
303
+ return Promise.reject('action not support');
304
+ }
305
+ });
306
+
307
+ // 启动检查更新
308
+ if (UPDATE_ENABLE) checkUpdate();
309
+
310
+ // 发送初始化完成事件
311
+ send(EVENT_NAMES.inited, {
312
+ status: true,
313
+ openDevTools: DEV_ENABLE,
314
+ sources: musicSources,
315
+ });
316
+
317
+ console.log('聚合音源已加载,共配置', SOURCE_CONFIGS.length, '个音源');