🦄 refactor: 重构网易云音乐解析代码

This commit is contained in:
zhiyu1998 2023-03-12 14:07:36 +08:00
parent df8c816ebc
commit a94dc34e40

View File

@ -645,25 +645,30 @@ export class tools extends plugin {
const message = e.msg === undefined ? e.message.shift().data.replaceAll("\\", "") : e.msg.trim(); 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 musicUrlReg = /(http:|https:)\/\/music.163.com\/song\/media\/outer\/url\?id=(\d+)/;
const id = musicUrlReg.exec(message)[2] || /id=(\d+)/.exec(message)[1]; const id = musicUrlReg.exec(message)[2] || /id=(\d+)/.exec(message)[1];
fetch(`https://api.vvhan.com/api/music?id=${id}&type=song&media=netease`, { const musicJson = JSON.parse(message)
headers: { const {musicUrl, preview, title, desc} = musicJson.meta.music
"User-Agent": // 如果没有下载地址跳出if
"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", if (_.isNull(musicUrl) || _.isUndefined(musicUrl)) {
"Content-Type": "application/json", e.reply(`识别:网易云音乐,解析失败!`);
}, return
}) } else {
.then(resp => resp.json()) fetch(`https://www.oranges1.top/neteaseapi.do/song/url?id=${id}`, {
.then(resp => { headers: {
const { name, author, mp3url, cover } = resp; "User-Agent":
e.reply([`识别:网易云音乐,${name}--${author}`, segment.image(cover)]); "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",
// 下载音乐并上传到群 },
this.downloadMp3(mp3url).then(path => { }).then(async resp => {
Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), '/', `${name}.mp3`) 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 => { .catch(err => {
console.error(`下载音乐失败,错误信息为: ${err.message}`); console.error(`下载音乐失败,错误信息为: ${err.message}`);
}); });
}); })
}
return true; return true;
} }