lxx.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. yjok/*!
  2. * @name 肥猫不肥
  3. * @description 肥猫不肥
  4. * @version 肥猫不肥
  5. * @author 肥猫不肥
  6. * @repository 肥猫不肥
  7. */
  8. // 是否开启开发模式
  9. const DEV_ENABLE = false
  10. // 服务端地址
  11. const API_URL = "http://music.xn--z7x900a.live"
  12. // 服务端配置的请求key
  13. const API_KEY = `114514`
  14. // 音质配置(key为音源名称,不要乱填.如果你账号为VIP可以填写到hires)
  15. // 全部的支持值: ['128k', '320k', 'flac', 'flac24bit']
  16. const MUSIC_QUALITY = JSON.parse('{"kw":["128k","320k","flac"],"kg":["128k"],"tx":["128k"],"wy":["128k"],"mg":["128k"]}')
  17. // 音源配置(默认为自动生成,可以修改为手动)
  18. const MUSIC_SOURCE = Object.keys(MUSIC_QUALITY)
  19. MUSIC_SOURCE.push('local')
  20. /**
  21. * 下面的东西就不要修改了
  22. */
  23. const { EVENT_NAMES, request, on, send, utils, env, version } = globalThis.lx
  24. /**
  25. * URL请求
  26. *
  27. * @param {string} url - 请求的地址
  28. * @param {object} options - 请求的配置文件
  29. * @return {Promise} 携带响应体的Promise对象
  30. */
  31. const httpFetch = (url, options = { method: 'GET' }) => {
  32. return new Promise((resolve, reject) => {
  33. console.log('--- start --- ' + url)
  34. request(url, options, (err, resp) => {
  35. if (err) return reject(err)
  36. console.log('API Response: ', resp)
  37. resolve(resp)
  38. })
  39. })
  40. }
  41. /**
  42. * Encodes the given data to base64.
  43. *
  44. * @param {type} data - the data to be encoded
  45. * @return {string} the base64 encoded string
  46. */
  47. const handleBase64Encode = (data) => {
  48. var data = utils.buffer.from(data, 'utf-8')
  49. return utils.buffer.bufToString(data, 'base64')
  50. }
  51. /**
  52. *
  53. * @param {string} source - 音源
  54. * @param {object} musicInfo - 歌曲信息
  55. * @param {string} quality - 音质
  56. * @returns {Promise<string>} 歌曲播放链接
  57. * @throws {Error} - 错误消息
  58. */
  59. const handleGetMusicUrl = async (source, musicInfo, quality) => {
  60. if (source == 'local') {
  61. if (!musicInfo.songmid.startsWith('server_')) throw new Error('upsupported local file')
  62. const songId = musicInfo.songmid
  63. const requestBody = {
  64. p: songId.replace('server_', ''),
  65. }
  66. var t = 'c'
  67. var b = handleBase64Encode(JSON.stringify(requestBody)) /* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  68. const targetUrl = `${API_URL}/local/${t}?q=${b}`
  69. const request = await httpFetch(targetUrl, {
  70. method: 'GET',
  71. headers: {
  72. 'Content-Type': 'application/json',
  73. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`,
  74. 'X-Request-Key': API_KEY,
  75. },
  76. follow_max: 5,
  77. })
  78. const { body } = request
  79. if (body.code == 0 && body.data && body.data.file) {
  80. var t = 'u'
  81. var b = handleBase64Encode(JSON.stringify(requestBody)) /* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  82. return `${API_URL}/local/${t}?q=${b}`
  83. }
  84. throw new Error('404 Not Found')
  85. }
  86. const songId = musicInfo.hash ?? musicInfo.songmid
  87. const request = await httpFetch(`${API_URL}/url/${source}/${songId}/${quality}`, {
  88. method: 'GET',
  89. headers: {
  90. 'Content-Type': 'application/json',
  91. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`,
  92. 'X-Request-Key': API_KEY,
  93. },
  94. follow_max: 5,
  95. })
  96. const { body } = request
  97. if (!body || isNaN(Number(body.code))) throw new Error('unknow error')
  98. if (env != 'mobile') console.groupEnd()
  99. switch (body.code) {
  100. case 0:
  101. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) success, URL: ${body.data}`)
  102. return body.data
  103. case 1:
  104. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed: ip被封禁`)
  105. throw new Error('block ip')
  106. case 2:
  107. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed, ${body.msg}`)
  108. throw new Error('get music url failed')
  109. case 4:
  110. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed, 远程服务器错误`)
  111. throw new Error('internal server error')
  112. case 5:
  113. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed, 请求过于频繁,请休息一下吧`)
  114. throw new Error('too many requests')
  115. case 6:
  116. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed, 请求参数错误`)
  117. throw new Error('param error')
  118. default:
  119. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) failed, ${body.msg ? body.msg : 'unknow error'}`)
  120. throw new Error(body.msg ?? 'unknow error')
  121. }
  122. }
  123. const handleGetMusicPic = async (source, musicInfo) => {
  124. switch (source) {
  125. case 'local':
  126. // 先从服务器检查是否有对应的类型,再响应链接
  127. if (!musicInfo.songmid.startsWith('server_')) throw new Error('upsupported local file')
  128. const songId = musicInfo.songmid
  129. const requestBody = {
  130. p: songId.replace('server_', ''),
  131. }
  132. var t = 'c'
  133. var b = handleBase64Encode(JSON.stringify(requestBody))/* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  134. const targetUrl = `${API_URL}/local/${t}?q=${b}`
  135. const request = await httpFetch(targetUrl, {
  136. method: 'GET',
  137. headers: {
  138. 'Content-Type': 'application/json',
  139. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`
  140. },
  141. follow_max: 5,
  142. })
  143. const { body } = request
  144. if (body.code === 0 && body.data.cover) {
  145. var t = 'p'
  146. var b = handleBase64Encode(JSON.stringify(requestBody))/* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  147. return `${API_URL}/local/${t}?q=${b}`
  148. }
  149. throw new Error('get music pic failed')
  150. default:
  151. throw new Error('action(pic) does not support source(' + source + ')')
  152. }
  153. }
  154. const handleGetMusicLyric = async (source, musicInfo) => {
  155. switch (source) {
  156. case 'local':
  157. if (!musicInfo.songmid.startsWith('server_')) throw new Error('upsupported local file')
  158. const songId = musicInfo.songmid
  159. const requestBody = {
  160. p: songId.replace('server_', ''),
  161. }
  162. var t = 'c'
  163. var b = handleBase64Encode(JSON.stringify(requestBody))/* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  164. const targetUrl = `${API_URL}/local/${t}?q=${b}`
  165. const request = await httpFetch(targetUrl, {
  166. method: 'GET',
  167. headers: {
  168. 'Content-Type': 'application/json',
  169. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`
  170. },
  171. follow_max: 5,
  172. })
  173. const { body } = request
  174. if (body.code === 0 && body.data.lyric) {
  175. var t = 'l'
  176. var b = handleBase64Encode(JSON.stringify(requestBody))/* url safe*/.replace(/\+/g, '-').replace(/\//g, '_')
  177. const request2 = await httpFetch(`${API_URL}/local/${t}?q=${b}`, {
  178. method: 'GET',
  179. headers: {
  180. 'Content-Type': 'application/json',
  181. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`
  182. },
  183. follow_max: 5,
  184. })
  185. if (request2.body.code === 0) {
  186. return {
  187. lyric: request2.body.data ?? "",
  188. tlyric: "",
  189. rlyric: "",
  190. lxlyric: ""
  191. }
  192. }
  193. throw new Error('get music lyric failed')
  194. }
  195. throw new Error('get music lyric failed')
  196. default:
  197. throw new Error('action(lyric) does not support source(' + source + ')')
  198. }
  199. }
  200. // 生成歌曲信息
  201. const musicSources = {}
  202. MUSIC_SOURCE.forEach(item => {
  203. musicSources[item] = {
  204. name: item,
  205. type: 'music',
  206. actions: (item == 'local') ? ['musicUrl', 'pic', 'lyric'] : ['musicUrl'],
  207. qualitys: (item == 'local') ? [] : MUSIC_QUALITY[item],
  208. }
  209. })
  210. // 监听 LX Music 请求事件
  211. on(EVENT_NAMES.request, ({ action, source, info }) => {
  212. switch (action) {
  213. case 'musicUrl':
  214. if (env != 'mobile') {
  215. console.group(`Handle Action(musicUrl)`)
  216. console.log('source', source)
  217. console.log('quality', info.type)
  218. console.log('musicInfo', info.musicInfo)
  219. } else {
  220. console.log(`Handle Action(musicUrl)`)
  221. console.log('source', source)
  222. console.log('quality', info.type)
  223. console.log('musicInfo', info.musicInfo)
  224. }
  225. return handleGetMusicUrl(source, info.musicInfo, info.type)
  226. .then(data => Promise.resolve(data))
  227. .catch(err => Promise.reject(err))
  228. case 'pic':
  229. return handleGetMusicPic(source, info.musicInfo)
  230. .then(data => Promise.resolve(data))
  231. .catch(err => Promise.reject(err))
  232. case 'lyric':
  233. return handleGetMusicLyric(source, info.musicInfo)
  234. .then(data => Promise.resolve(data))
  235. .catch(err => Promise.reject(err))
  236. default:
  237. console.error(`action(${action}) not support`)
  238. return Promise.reject('action not support')
  239. }
  240. })
  241. // 向 LX Music 发送初始化成功事件
  242. send(EVENT_NAMES.inited, { status: true, openDevTools: DEV_ENABLE, sources: musicSources })