forked from lx-music-project/lx-music-api-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlx-music-source-example.js
107 lines (98 loc) · 3.02 KB
/
lx-music-source-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*!
* @name 替换为你的音乐源名称
* @description 替换为你的音乐源介绍
* @version v1.0.1
* @author Folltoshe & helloplhm-qwq
* @repository https://github.com/lxmusics/lx-music-api-server
*/
// 是否开启开发模式
const DEV_ENABLE = true
// 服务端地址
const API_URL = 'http://127.0.0.1:9763'
// 服务端配置的请求key
const API_KEY = ''
// 音质配置(key为音源名称,不要乱填.如果你账号为VIP可以填写到hires)
// 全部的支持值: ['128k', '320k', 'flac', 'flac24bit']
const MUSIC_QUALITY = {
kw: ['128k', '320k', 'flac'],
kg: ['128k'],
tx: ['128k'],
wy: ['128k'],
mg: ['128k'],
}
// 音源配置(默认为自动生成,可以修改为手动)
const MUSIC_SOURCE = Object.keys(MUSIC_QUALITY)
/**
* 下面的东西就不要修改了
*/
const { EVENT_NAMES, request, on, send, utils, env, version } = globalThis.lx
const httpFetch = (url, options = { method: 'GET' }) => {
return new Promise((resolve, reject) => {
request(url, options, (err, resp) => {
if (err) return reject(err)
resolve(resp)
})
})
}
const handleGetMusicUrl = async (source, musicInfo, quality) => {
const songId = musicInfo.hash ?? musicInfo.songmid
const request = await httpFetch(`${API_URL}/url/${source}/${songId}/${quality}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-usic-request/${version}`}`,
'X-Request-Key': API_KEY,
},
})
const { body } = request
if (!body || isNaN(Number(body.code))) throw new Error('unknow error')
switch (body.code) {
case 0:
return body.data
case 1:
throw new Error('block ip')
case 2:
throw new Error('get music url failed')
case 4:
throw new Error('internal server error')
case 5:
throw new Error('too many requests')
case 5:
throw new Error('param error')
default:
throw new Error(body.msg ?? 'unknow error')
}
}
const musicSources = {}
MUSIC_SOURCE.forEach(item => {
musicSources[item] = {
name: item,
type: 'music',
actions: ['musicUrl'],
qualitys: MUSIC_QUALITY[item],
}
})
on(EVENT_NAMES.request, ({ action, source, info }) => {
switch (action) {
case 'musicUrl':
if (env != "mobile") {
console.group(`Handle Action(musicUrl)`)
console.log('source', source)
console.log('quality', info.type)
console.log('musicInfo', info.musicInfo)
console.groupEnd()
} else {
console.log(`Handle Action(musicUrl)`)
console.log('source', source)
console.log('quality', info.type)
console.log('musicInfo', info.musicInfo)
}
return handleGetMusicUrl(source, info.musicInfo, info.type)
.then(data => Promise.resolve(data))
.catch(err => Promise.reject(err))
default:
console.error(`action(${action}) not support`)
return Promise.reject('action not support')
}
})
send(EVENT_NAMES.inited, { status: true, openDevTools: DEV_ENABLE, sources: musicSources })