forked from Huibq/keep-alive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_api.js
89 lines (88 loc) · 2.66 KB
/
render_api.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
/*!
* @name Huibq_lxmusic源
* @description Github搜索“洛雪音乐音源”,禁止批量下载!
* @version v1.2.0
* @author Huibq
*/
const DEV_ENABLE = false
const API_URL = 'https://lxmusicapi.onrender.com'
const API_KEY = 'share-v2'
const MUSIC_QUALITY = {
kw: ['128k', '320k'],
kg: ['128k', '320k'],
tx: ['128k', '320k'],
wy: ['128k', '320k'],
mg: ['128k', '320k'],
}
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.url
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 6:
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 })