From ec6248150a2fbca2dda8d783f105a84702ed1f6e Mon Sep 17 00:00:00 2001 From: zhiyu1998 Date: Fri, 24 Mar 2023 22:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=B3=A2=E7=82=B9=E9=9F=B3=E4=B9=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 58 +++++++++++++---------------- utils/bodian.js | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 33 deletions(-) create mode 100644 utils/bodian.js diff --git a/apps/tools.js b/apps/tools.js index 805270b..b102c60 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -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, + // 音频例子: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+"/temp.mp3"), + "/", + `${name}-${album}-${artist}.mp3`, + ); }) - .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 => { - Bot.acquireGfs(e.group_id).upload( - fs.readFileSync(path), - "/", - `${respJson.reqId}.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; } diff --git a/utils/bodian.js b/utils/bodian.js new file mode 100644 index 0000000..65b2f1f --- /dev/null +++ b/utils/bodian.js @@ -0,0 +1,97 @@ +import axios from "axios"; +import { downloadMp3, generateRandomStr } from "./common.js"; + +/** + * 获取音频 + * @param id + * @param path + * @returns {Promise} + */ +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>} + */ +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} + */ +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 +}