From a94dc34e40214ef7fb9384b60d5767bcf66c1950 Mon Sep 17 00:00:00 2001 From: zhiyu1998 Date: Sun, 12 Mar 2023 14:07:36 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=BD=91=E6=98=93=E4=BA=91=E9=9F=B3=E4=B9=90=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index 36b27e7..0cf825c 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -645,25 +645,30 @@ export class tools extends plugin { const message = e.msg === undefined ? e.message.shift().data.replaceAll("\\", "") : e.msg.trim(); const musicUrlReg = /(http:|https:)\/\/music.163.com\/song\/media\/outer\/url\?id=(\d+)/; const id = musicUrlReg.exec(message)[2] || /id=(\d+)/.exec(message)[1]; - fetch(`https://api.vvhan.com/api/music?id=${id}&type=song&media=netease`, { - headers: { - "User-Agent": - "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36", - "Content-Type": "application/json", - }, - }) - .then(resp => resp.json()) - .then(resp => { - const { name, author, mp3url, cover } = resp; - e.reply([`识别:网易云音乐,${name}--${author}`, segment.image(cover)]); - // 下载音乐并上传到群 - this.downloadMp3(mp3url).then(path => { - Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), '/', `${name}.mp3`) - }) + const musicJson = JSON.parse(message) + const {musicUrl, preview, title, desc} = musicJson.meta.music + // 如果没有下载地址跳出if + if (_.isNull(musicUrl) || _.isUndefined(musicUrl)) { + e.reply(`识别:网易云音乐,解析失败!`); + return + } else { + fetch(`https://www.oranges1.top/neteaseapi.do/song/url?id=${id}`, { + headers: { + "User-Agent": + "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36", + }, + }).then(async resp => { + const url = await JSON.parse(await resp.text()).data[0].url + // 反之解析官方地址 + e.reply([`识别:网易云音乐,${title}--${desc}`, segment.image(preview)]); + this.downloadMp3(url, 'follow').then(path => { + Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), '/', `${title.replace(/[\/\?<>\\:\*\|".… ]/g, '')}.mp3`) + }) .catch(err => { console.error(`下载音乐失败,错误信息为: ${err.message}`); }); - }); + }) + } return true; }