From 9958568d05a6fe0375a8732218717d0e69dd4aff Mon Sep 17 00:00:00 2001 From: zhiyu1998 Date: Thu, 16 Mar 2023 01:00:24 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E7=BD=91=E6=98=93=E4=BA=91?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/neteasepro.js | 64 +++++++++++++++++++++++++++++----------------- utils/netease.js | 10 ++++++++ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/apps/neteasepro.js b/apps/neteasepro.js index 8c2189f..5d30fb1 100644 --- a/apps/neteasepro.js +++ b/apps/neteasepro.js @@ -11,6 +11,7 @@ import { getUserRecord, checkMusic, getSong, + getSongDetail, } from "../utils/netease.js"; import { ha12store, store2ha1 } from "../utils/encrypt.js"; import fetch from "node-fetch"; @@ -153,24 +154,27 @@ export class neteasepro extends plugin { musicUrlReg2.exec(message)[3] || musicUrlReg.exec(message)[2] || /id=(\d+)/.exec(message)[1]; - const musicJson = JSON.parse(message); - const { musicUrl, preview, title, desc } = musicJson.meta.music || musicJson.meta.news; - console.log(musicUrl, preview, title, desc); - // 如果没有登陆,就使用官方接口 - e.reply([`识别:网易云音乐,${title}--${desc}`, segment.image(preview)]); - if (!(await redis.exists(await this.getRedisKey(e.user_id)))) { - this.downloadMp3(`music.163.com/song/media/outer/url?id=${id}`, "follow") - .then(path => { - Bot.acquireGfs(e.group_id).upload( - fs.readFileSync(path), - "/", - `${title.replace(/[\/\?<>\\:\*\|".… ]/g, "")}.mp3`, - ); - }) - .catch(err => { - console.error(`下载音乐失败,错误信息为: ${err.message}`); - }); - return true; + const isMessageJson = await this.isJSON(message); + if (isMessageJson) { + const musicJson = JSON.parse(message); + const { preview, title, desc } = musicJson.meta.music || musicJson.meta.news; + // console.log(musicUrl, preview, title, desc); + // 如果没有登陆,就使用官方接口 + e.reply([`识别:网易云音乐,${title}--${desc}`, segment.image(preview)]); + if (!(await redis.exists(await this.getRedisKey(e.user_id)))) { + this.downloadMp3(`music.163.com/song/media/outer/url?id=${id}`, "follow") + .then(path => { + Bot.acquireGfs(e.group_id).upload( + fs.readFileSync(path), + "/", + `${title.replace(/[\/\?<>\\:\*\|".… ]/g, "")}.mp3`, + ); + }) + .catch(err => { + console.error(`下载音乐失败,错误信息为: ${err.message}`); + }); + return true; + } } // 检查当前歌曲是否可用 const checkOne = await checkMusic(id); @@ -181,13 +185,13 @@ export class neteasepro extends plugin { const userInfo = await this.aopBefore(e); // 可用,开始下载 const userDownloadUrl = (await getSong(id, await userInfo.cookie))[0].url; + const title = await getSongDetail(id).then(res => { + const song = res.songs[0]; + return `${song?.name}-${song?.ar?.[0].name}`.replace(/[\/\?<>\\:\*\|".… ]/g, ""); + }); await this.downloadMp3(userDownloadUrl) .then(path => { - Bot.acquireGfs(e.group_id).upload( - fs.readFileSync(path), - "/", - `${title.replace(/[\/\?<>\\:\*\|".… ]/g, "")}.mp3`, - ); + Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), "/", `${title}.mp3`); }) .catch(err => { console.error(`下载音乐失败,错误信息为: ${err.message}`); @@ -231,13 +235,25 @@ export class neteasepro extends plugin { }); } + async isJSON(str) { + if (typeof str !== "string") { + return false; + } + try { + JSON.parse(str); + return true; + } catch (e) { + return false; + } + } + // 定时轮询 async poll(key) { let timer; return new Promise((resolve, reject) => { timer = setInterval(async () => { const statusRes = await getCookies(key); - console.log(statusRes); + // console.log(statusRes); if (statusRes.code === 800) { clearInterval(timer); reject("二维码已过期,请重新获取"); diff --git a/utils/netease.js b/utils/netease.js index f1e91c5..7876d22 100644 --- a/utils/netease.js +++ b/utils/netease.js @@ -115,6 +115,15 @@ async function getSong(id, cookie) { }); } +async function getSongDetail(ids) { + return axios({ + url: `${BASE_URL}/song/detail?ids=${ids}×tamp=${Date.now()}`, + method: "get", + }).then(resp => { + return resp.data; + }); +} + export { getCookies, getLoginStatus, @@ -124,4 +133,5 @@ export { getUserRecord, checkMusic, getSong, + getSongDetail };