feat: 添加网易云云盘功能

This commit is contained in:
zhiyu1998 2023-03-27 02:28:49 +08:00
parent 95bab724ea
commit a98855e2ea
3 changed files with 74 additions and 22 deletions

View File

@ -13,7 +13,7 @@ import {
getSongDetail, getSongDetail,
getUserRecord, getUserRecord,
getCloud, getCloud,
getCloudMusicDetail getCloudMusicDetail,
} from "../utils/netease.js"; } from "../utils/netease.js";
import { ha12store, store2ha1 } from "../utils/encrypt.js"; import { ha12store, store2ha1 } from "../utils/encrypt.js";
import { downloadMp3 } from "../utils/common.js"; import { downloadMp3 } from "../utils/common.js";
@ -49,6 +49,14 @@ export class neteasepro extends plugin {
reg: "^#网易云云盘$", reg: "^#网易云云盘$",
fnc: "neteaseCloud", fnc: "neteaseCloud",
}, },
{
reg: "^#网易云云盘下载(.*)",
fnc: "neteaseCloudDownload",
},
{
reg: "^#网易云云盘(.*)",
fnc: "neteaseCloudApplet",
},
{ {
reg: "music.163.com", reg: "music.163.com",
fnc: "netease", fnc: "netease",
@ -153,22 +161,62 @@ export class neteasepro extends plugin {
if (realCookie === "") { if (realCookie === "") {
return true; return true;
} }
const cloudMusics = (await getCloud(realCookie)).map(async item => { const cloudMusics = await (
let music = await getSong(item.songId, realCookie) await getCloud(realCookie)
let finalMusic; ).map(item => `${item.songId}: ${item?.songName}-${item?.artists}`);
if (music instanceof Array) { // 获取用户信息
music = music[0]; const { profile } = await getLoginStatus(realCookie);
finalMusic = await this.cloudMusicPack(item, music.url); cloudMusics.unshift(`<${profile.nickname}> 的网易云云盘`);
} else { e.reply(cloudMusics.join("\n"));
finalMusic = await this.musicPack(music); return true;
} }
return {
message: segment.json(finalMusic), async neteaseCloudDownload(e) {
nickname: e.sender.card || e.user_id, const id = e.msg.replace("#网易云云盘下载", "").trim();
user_id: e.user_id, const userInfo = await this.aopBefore(e);
} const realCookie = userInfo.cookie;
}) if (realCookie === "") {
let forwardMsg = await Bot.makeForwardMsg(await Promise.all(cloudMusics)); return true;
}
const music = (await getSong(id, realCookie))[0];
const item = (await getCloudMusicDetail(id, realCookie)).data[0];
const simpleSong = item.simpleSong;
e.reply([
segment.image(simpleSong?.al?.picUrl),
`识别:云盘音乐,${simpleSong?.name}-${simpleSong?.al?.name}`,
]);
const downloadPath = `./data/rcmp4/${this.e.group_id || this.e.user_id}`;
await downloadMp3(music.url, downloadPath)
.then(path => {
Bot.acquireGfs(e.group_id).upload(
fs.readFileSync(path),
"/",
`${simpleSong?.name}.mp3`,
);
})
.catch(err => {
console.error(`下载音乐失败,错误信息为: ${err.message}`);
});
return true;
}
async neteaseCloudApplet(e) {
const id = e.msg.replace("#网易云云盘", "").trim();
logger.mark(id);
const userInfo = await this.aopBefore(e);
const realCookie = userInfo.cookie;
if (realCookie === "") {
return true;
}
const music = (await getSong(id, realCookie))[0];
const item = (await getCloudMusicDetail(id, realCookie)).data[0];
logger.mark(music, item);
const appletMusic = {
message: segment.json(await this.cloudMusicPack(item, music.url)),
nickname: e.sender.card || e.user_id,
user_id: e.user_id,
};
let forwardMsg = await Bot.makeForwardMsg(appletMusic);
await e.reply(await this.musicForwardPack(forwardMsg)); await e.reply(await this.musicForwardPack(forwardMsg));
} }
@ -181,7 +229,7 @@ export class neteasepro extends plugin {
musicUrlReg2.exec(message)?.[3] || musicUrlReg2.exec(message)?.[3] ||
musicUrlReg.exec(message)?.[2] || musicUrlReg.exec(message)?.[2] ||
/id=(\d+)/.exec(message)[1]; /id=(\d+)/.exec(message)[1];
const downloadPath = `./data/rcmp4/${this.e.group_id || this.e.user_id}` const downloadPath = `./data/rcmp4/${this.e.group_id || this.e.user_id}`;
// 是游客 // 是游客
if (!(await redis.get(await this.getRedisKey(e.user_id)))) { if (!(await redis.get(await this.getRedisKey(e.user_id)))) {
// 是小程序 // 是小程序
@ -202,7 +250,11 @@ export class neteasepro extends plugin {
e.reply(`识别:网易云音乐,${title}`); e.reply(`识别:网易云音乐,${title}`);
} }
// 下载游客歌曲 // 下载游客歌曲
downloadMp3(`https://music.163.com/song/media/outer/url?id=${id}`, downloadPath, "follow") downloadMp3(
`https://music.163.com/song/media/outer/url?id=${id}`,
downloadPath,
"follow",
)
.then(path => { .then(path => {
Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), "/", `${id}.mp3`); Bot.acquireGfs(e.group_id).upload(fs.readFileSync(path), "/", `${id}.mp3`);
}) })
@ -345,7 +397,7 @@ export class neteasepro extends plugin {
forward: true, forward: true,
ctime: Date.now(), ctime: Date.now(),
}, },
} };
} }
// 包装分享小程序数据 // 包装分享小程序数据

View File

@ -143,7 +143,7 @@ async function downloadMp3(mp3Url, path, redirect = "manual") {
// 补充保存文件名 // 补充保存文件名
path += "/temp.mp3"; path += "/temp.mp3";
if (fs.existsSync(path)) { if (fs.existsSync(path)) {
console.log(`频已存在`); console.log(`频已存在`);
fs.unlinkSync(path); fs.unlinkSync(path);
} }
// 开始下载 // 开始下载

View File

@ -2,7 +2,7 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import axios from "axios"; import axios from "axios";
const BASE_URL = "http://cloud-music.pl-fe.cn"; const BASE_URL = "http://127.0.0.1:3000";
/** /**
* 获取cookie * 获取cookie