🐞 fix: V1.6.7-lts 修复网易云音乐 & 修复皮皮虾问题

1. 修复函数`downloadMp3`导致的一系列下载问题
2. 修复netease music下载
3. 修复皮皮虾的问题
4. 修正部分注释
This commit is contained in:
zhiyu1998 2024-04-30 22:35:56 +08:00
parent c2c75fccc0
commit 7e820cbf26
4 changed files with 95 additions and 63 deletions

View File

@ -991,6 +991,15 @@ export class tools extends plugin {
async netease(e) { async netease(e) {
let message = let message =
e.msg === undefined ? e.message.shift().data.replaceAll("\\", "") : e.msg.trim(); e.msg === undefined ? e.message.shift().data.replaceAll("\\", "") : e.msg.trim();
// 处理短号此时会变成y.music.163.com
if (message.includes("163cn.tv")) {
message = /(http:|https:)\/\/163cn\.tv\/([a-zA-Z1-9]+)/.exec(message)?.[0]
logger.info(message)
message = await axios.head(message).then((resp) => {
return resp.request.res.responseUrl;
});
}
// 处理网页链接
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 musicUrlReg2 = /(http:|https:)\/\/y.music.163.com\/m\/song\?(.*)&id=(\d+)/; const musicUrlReg2 = /(http:|https:)\/\/y.music.163.com\/m\/song\?(.*)&id=(\d+)/;
const id = const id =
@ -1005,41 +1014,49 @@ export class tools extends plugin {
if (typeof message !== "string") { if (typeof message !== "string") {
return false; return false;
} }
try { axios.get(NETEASE_SONG_DOWNLOAD.replace("{}", id), {
// 小程序 headers: {
const musicJson = JSON.parse(message); "User-Agent":
const { preview, title, desc } = musicJson.meta.music || musicJson.meta.news; "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",
e.reply([`识别:网易云音乐,${ title }--${ desc }`, segment.image(preview)]); },
JSON.parse(message); }).then(async resp => {
return true; logger.info(resp.data)
} catch (err) { let url = await resp.data.data?.[0].url;
axios.get(NETEASE_SONG_DOWNLOAD.replace("{}", id), { // 获取歌曲信息
headers: { let title = await axios.get(NETEASE_SONG_DETAIL.replace("{}", id)).then(res => {
"User-Agent": const song = res.data.songs[0];
"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", return `${ song?.name }-${ song?.ar?.[0].name }`.replace(/[\/\?<>\\:\*\|".… ]/g, "");
}, });
}).then(async resp => { // 一般这个情况是VIP歌曲
const url = await resp.data.data?.[0].url; if (url == null) {
// 获取歌曲信息 // 临时接口
const title = await axios.get(NETEASE_SONG_DETAIL.replace("{}", id)).then(res => { const vipMusicData = await axios.get(`https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm=${ title }&n=1&type=json`, {
const song = res.data.songs[0]; headers: {
return `${ song?.name }-${ song?.ar?.[0].name }`.replace(/[\/\?<>\\:\*\|".… ]/g, ""); "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",
},
}); });
e.reply(`识别:网易云音乐,${ title }`); title += "\nR插件检测到当前为VIP音乐正在转换...";
// const mvUrlJson = await getKugouMv(title, 1, 1, 0); url = vipMusicData.data.music_url;
// const mvUrl = mvUrlJson.map(item => item.mv_url)?.[0]; }
// this.downloadVideo(mvUrl).then(path => { e.reply(`识别:网易云音乐,${ title }`);
// e.reply(segment.video(path + "/temp.mp4")); // const mvUrlJson = await getKugouMv(title, 1, 1, 0);
// }); // const mvUrl = mvUrlJson.map(item => item.mv_url)?.[0];
downloadMp3(url, 'follow').then(path => { // this.downloadVideo(mvUrl).then(path => {
e.group.sendFile(fs.readFileSync(path), '/', `${ title.replace(/[\/\?<>\\:\*\|".… ]/g, '') }.mp3`); // e.reply(segment.video(path + "/temp.mp4"));
}) // });
.catch(err => { downloadMp3(url, 'follow').then(path => {
console.error(`下载音乐失败,错误信息为: ${ err.message }`); // 判断是不是icqq
}); if (e.bot?.sendUni) {
}) e.group.fs.upload(path);
return true; } else {
} e.group.sendFile(path);
}
}).catch(err => {
logger.error(`下载音乐失败,错误信息为: ${ err.message }`);
});
})
return true;
} }
// 微博解析 // 微博解析

View File

@ -92,7 +92,7 @@ export const TWITTER_TWEET_INFO = "https://api.twitter.com/2/tweets?ids={}"
export const XHS_REQ_LINK = "https://www.xiaohongshu.com/explore/" export const XHS_REQ_LINK = "https://www.xiaohongshu.com/explore/"
/** /**
* 🍉 的请求链接 * 通用解析的请求链接
* @type {string} * @type {string}
*/ */
export const GENERAL_REQ_LINK = { export const GENERAL_REQ_LINK = {

View File

@ -130,39 +130,54 @@ export function generateRandomStr(randomlength = 16) {
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
export async function downloadMp3(mp3Url, path, redirect = "manual") { export async function downloadMp3(mp3Url, path, redirect = "manual") {
return fetch(mp3Url, { // 如果没有目录就创建一个
await mkdirIfNotExists(path)
// 补充保存文件名
path += "/temp.mp3";
if (fs.existsSync(path)) {
console.log(`音频已存在`);
fs.unlinkSync(path);
}
// 发起请求
const response = await fetch(mp3Url, {
headers: { headers: {
"User-Agent": "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", "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",
}, },
responseType: "stream", responseType: "stream",
redirect: redirect, redirect: redirect,
}).then(async res => {
// 如果没有目录就创建一个
await mkdirIfNotExists(path)
// 补充保存文件名
path += "/temp.mp3";
if (fs.existsSync(path)) {
console.log(`音频已存在`);
fs.unlinkSync(path);
}
// 开始下载
const fileStream = fs.createWriteStream(path);
res.body.pipe(fileStream);
return new Promise((resolve, reject) => {
fileStream.on("finish", () => {
fileStream.close(() => {
resolve(path);
});
});
fileStream.on("error", err => {
fs.unlink(path, () => {
reject(err);
});
});
});
}); });
if (!response.ok) {
throw new Error(`Failed to fetch ${response.statusText}`);
}
try {
const response = await axios({
method: 'get',
url: mp3Url,
responseType: 'stream',
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"
}
});
// 开始下载
const writer = fs.createWriteStream(path);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(path));
writer.on('error', reject);
});
} catch (error) {
console.error(`下载音乐失败,错误信息为: ${error.message}`);
throw error;
}
} }
/** /**

View File

@ -33,6 +33,7 @@ class GeneralLinkAdapter {
* @returns {*} * @returns {*}
*/ */
createReqLink(externalInterface, requestURL) { createReqLink(externalInterface, requestURL) {
// 这里必须使用{ ...GENERAL_REQ_LINK_2 }赋值,不然就是对象的引用赋值,会造成全局数据问题!
let reqLink = { ...externalInterface }; let reqLink = { ...externalInterface };
reqLink.link = reqLink.link.replace("{}", requestURL); reqLink.link = reqLink.link.replace("{}", requestURL);
return reqLink; return reqLink;
@ -81,8 +82,7 @@ class GeneralLinkAdapter {
async pipixia(link) { async pipixia(link) {
const msg = /https:\/\/h5\.pipix\.com\/s\/[A-Za-z0-9]+/.exec(link)?.[0]; const msg = /https:\/\/h5\.pipix\.com\/s\/[A-Za-z0-9]+/.exec(link)?.[0];
// 这里必须使用{ ...GENERAL_REQ_LINK_2 }赋值,不然就是对象的引用赋值,会造成全局数据问题! const reqLink = this.createReqLink(GENERAL_REQ_LINK, msg);
const reqLink = this.createReqLink(GENERAL_REQ_LINK_2, msg);
return { name: "皮皮虾", reqLink }; return { name: "皮皮虾", reqLink };
} }