🦄 refactor: 重构波点音乐

This commit is contained in:
zhiyu1998 2023-03-24 22:49:28 +08:00
parent 6bbe9ca3ed
commit ec6248150a
2 changed files with 122 additions and 33 deletions

View File

@ -11,12 +11,13 @@ import { mkdirsSync } from "../utils/file.js";
import { downloadBFile, getDownloadUrl, mergeFileToMp4, getDynamic } from "../utils/bilibili.js";
import { parseUrl, parseM3u8, downloadM3u8Videos, mergeAcFileToMp4 } from "../utils/acfun.js";
import { transMap, douyinTypeMap, XHS_CK, TEN_THOUSAND } from "../utils/constant.js";
import { getIdVideo, generateRandomStr, downloadMp3 } from "../utils/common.js";
import { getIdVideo, generateRandomStr } from "../utils/common.js";
import config from "../model/index.js";
import Translate from "../utils/trans-strategy.js";
import { getXB } from "../utils/x-bogus.js";
import { getVideoInfo } from "../utils/biliInfo.js";
import { getBiliGptInputText } from "../utils/biliSummary.js";
import {getBodianAudio, getBodianMv, getBodianMusicInfo} from "../utils/bodian.js";
import { ChatGPTClient } from "@waylaidwanderer/chatgpt-api";
export class tools extends plugin {
@ -912,39 +913,30 @@ export class tools extends plugin {
// 波点音乐解析
async bodianMusic(e) {
// 例子https://h5app.kuwo.cn/m/bodian/playMusic.html?uid=3216773&musicId=192015898&opusId=&extendType=together
const id = /(?=musicId).*?(?=&)/.exec(e.msg.trim())[0].replace("musicId=", "");
const API = `https://bd-api.kuwo.cn/api/service/music/audioUrl/${id}?format=mp3&br=320kmp3&songType=&fromList=&weListenUid=&weListenDevId=`;
const headers = {
"User-Agent": "bodian/106 CFNetwork/1399 Darwin/22.1.0",
devId: `95289318-8847-43D5-8477-85296654785${String.fromCharCode(
65 + Math.floor(Math.random() * 26),
)}`,
Host: "bd-api.kuwo.cn",
plat: "ip",
ver: "3.1.0",
"Cache-Control": "no-cache",
channel: "appstore",
};
await axios
.get(API, {
headers,
})
.then(resp => {
const respJson = resp.data;
const audioUrl = respJson.data.audioUrl;
downloadMp3(audioUrl, `${this.defaultPath}${this.e.group_id || this.e.user_id}`)
.then(path => {
// 音频例子https://h5app.kuwo.cn/m/bodian/playMusic.html?uid=3216773&musicId=192015898&opusId=&extendType=together
// 视频例子https://h5app.kuwo.cn/m/bodian/play.html?uid=3216773&mvId=118987&opusId=770096&extendType=together
const id = /(?=musicId).*?(?=&)/.exec(e.msg.trim())?.[0].replace("musicId=", "")
|| /(?=mvId).*?(?=&)/.exec(e.msg.trim())?.[0].replace("mvId=", "");
const {name, album, artist, albumPic120, categorys} = await getBodianMusicInfo(id)
e.reply([`识别:波点音乐,${name}-${album}-${artist}\n标签:${categorys.map(item=>item.name).join(" | ")}`, segment.image(albumPic120)])
if (e.msg.includes("musicId")) {
const path = `${this.defaultPath}${this.e.group_id || this.e.user_id}`
await getBodianAudio(id, path).then(_ => {
Bot.acquireGfs(e.group_id).upload(
fs.readFileSync(path),
fs.readFileSync(path+"/temp.mp3"),
"/",
`${respJson.reqId}.mp3`,
`${name}-${album}-${artist}.mp3`,
);
})
.catch(err => {
console.error(`下载音乐失败,错误信息为: ${err.message}`);
});
} else if (e.msg.includes("mvId")) {
await getBodianMv(id).then(res => {
// 下载 && 发送
const { coverUrl, highUrl, lowUrl, shortLowUrl } = res
this.downloadVideo(lowUrl).then(path => {
e.reply(segment.video(path + "/temp.mp4"));
});
})
}
return true;
}

97
utils/bodian.js Normal file
View File

@ -0,0 +1,97 @@
import axios from "axios";
import { downloadMp3, generateRandomStr } from "./common.js";
/**
* 获取音频
* @param id
* @param path
* @returns {Promise<any>}
*/
async function getBodianAudio(id, path) {
// 音乐数据
const API = `https://bd-api.kuwo.cn/api/service/music/audioUrl/${id}?format=mp3&br=320kmp3&songType=&fromList=&weListenUid=&weListenDevId=`;
const headers = {
"User-Agent": "bodian/106 CFNetwork/1399 Darwin/22.1.0",
devId: `95289318-8847-43D5-8477-85296654785${String.fromCharCode(
65 + Math.floor(Math.random() * 26),
)}`,
Host: "bd-api.kuwo.cn",
plat: "ip",
ver: "3.1.0",
"Cache-Control": "no-cache",
channel: "appstore",
};
return await axios
.get(API, {
headers,
})
.then(async resp => {
const respJson = resp.data;
const audioUrl = respJson.data.audioUrl;
await downloadMp3(audioUrl, path)
.catch(err => {
console.error(`下载音乐失败,错误信息为: ${err.message}`);
});
return respJson;
});
}
/**
* 获取MV地址
* @param id
* @returns {Promise<(fid: string, pid: string) => Promise<void>>}
*/
async function getBodianMv(id) {
// mv数据
const API = `https://bd-api.kuwo.cn/api/service/mv/info?musicId=${id}&wifi=1&noWifi=1&uid=-1&token=`
const headers = {
"User-Agent": "Dart/2.18 (dart:io)",
plat: "ar",
ver: "3.1.0",
host: "bd-api.kuwo.cn",
channel: "aliopen",
devId: generateRandomStr(16)
}
return await axios.get(API, {
headers
}).then(async resp => {
const res = resp.data;
// 如果没有,直接返回
if (res.data.lowUrl === null || res.data.highUrl === null) {
return;
}
// 波点音乐信息
return res.data.mv;
}).catch(err => {
logger.error("波点音乐错误");
});
}
/**
* 获取音乐信息
* @returns {Promise<void>}
*/
async function getBodianMusicInfo(id) {
const API = `https://bd-api.kuwo.cn/api/service/music/info?musicId=${id}&uid=-1&token=`
const headers = {
"User-Agent": "Dart/2.18 (dart:io)",
plat: "ar",
ver: "3.1.0",
host: "bd-api.kuwo.cn",
channel: "aliopen",
devId: generateRandomStr(16)
}
return await axios.get(API, {
headers
}).then(async resp => {
return resp.data?.data;
}).catch(err => {
logger.error("波点音乐错误");
});
}
export {
getBodianAudio,
getBodianMv,
getBodianMusicInfo
}