m3.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*!
  2. * @name 梓澄公益音源二代
  3. * @description 摆烂了,随便你们吧,反正这个没有账号
  4. * @version 1.2.3.4.5
  5. * @author helloplhm-qwq & Folltoshe
  6. * @repository https://github.com/lxmusics/lx-music-api-server
  7. */
  8. // 是否开启开发模式
  9. const DEV_ENABLE = false
  10. // 服务端地址
  11. const API_URL = "http://103.239.247.51:9763"
  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. /**
  20. * 下面的东西就不要修改了
  21. */
  22. const {EVENT_NAMES, request, on, send, utils, env, version} = globalThis.lx
  23. const httpFetch = (url, options = {method: 'GET'}) => {
  24. return new Promise((resolve, reject) => {
  25. console.log('--- start --- ' + url)
  26. request(url, options, (err, resp) => {
  27. if (err) return reject(err)
  28. console.log('API Response: ', resp)
  29. resolve(resp)
  30. })
  31. })
  32. }
  33. const handleGetMusicUrl = async (source, musicInfo, quality) => {
  34. const songId = musicInfo.hash ?? musicInfo.songmid
  35. const request = await httpFetch(`${API_URL}/url/${source}/${songId}/${quality}`, {
  36. method: 'GET',
  37. headers: {
  38. 'Content-Type': 'application/json',
  39. 'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`,
  40. 'X-Request-Key': API_KEY,
  41. },
  42. })
  43. const {body} = request
  44. if (!body || isNaN(Number(body.code))) throw new Error('unknow error')
  45. switch (body.code) {
  46. case 0:
  47. console.log(`handleGetMusicUrl(${source}_${musicInfo.songmid}, ${quality}) success, URL: ${body.data}`)
  48. return body.data
  49. case 1:
  50. throw new Error('block ip')
  51. case 2:
  52. throw new Error('get music url failed')
  53. case 4:
  54. throw new Error('internal server error')
  55. case 5:
  56. throw new Error('too many requests')
  57. case 6:
  58. throw new Error('param error')
  59. default:
  60. throw new Error(body.msg ?? 'unknow error')
  61. }
  62. }
  63. const musicSources = {}
  64. MUSIC_SOURCE.forEach(item => {
  65. musicSources[item] = {
  66. name: item,
  67. type: 'music',
  68. actions: ['musicUrl'],
  69. qualitys: MUSIC_QUALITY[item],
  70. }
  71. })
  72. on(EVENT_NAMES.request, ({action, source, info}) => {
  73. switch (action) {
  74. case 'musicUrl':
  75. if (env != 'mobile') {
  76. console.group(`Handle Action(musicUrl)`)
  77. console.log('source', source)
  78. console.log('quality', info.type)
  79. console.log('musicInfo', info.musicInfo)
  80. console.groupEnd()
  81. } else {
  82. console.log(`Handle Action(musicUrl)`)
  83. console.log('source', source)
  84. console.log('quality', info.type)
  85. console.log('musicInfo', info.musicInfo)
  86. }
  87. return handleGetMusicUrl(source, info.musicInfo, info.type)
  88. .then(data => Promise.resolve(data))
  89. .catch(err => Promise.reject(err))
  90. default:
  91. console.error(`action(${action}) not support`)
  92. return Promise.reject('action not support')
  93. }
  94. })
  95. send(EVENT_NAMES.inited, {status: true, openDevTools: DEV_ENABLE, sources: musicSources})