feat: 增加B站番剧解析

This commit is contained in:
zhiyu1998 2024-08-19 14:55:36 +08:00
parent 6dd178be9d
commit eb70f27f8f
4 changed files with 91 additions and 52 deletions

View File

@ -26,6 +26,7 @@ import {
XHS_NO_WATERMARK_HEADER,
} from "../constants/constant.js";
import {
BILI_EP_INFO,
BILI_ONLINE,
BILI_STREAM_INFO,
BILI_SUMMARY,
@ -49,6 +50,7 @@ import { downloadM3u8Videos, mergeAcFileToMp4, parseM3u8, parseUrl } from "../ut
import { startBBDown } from "../utils/bbdown-util.js";
import { av2BV } from "../utils/bilibili-bv-av-convert.js";
import {
BILI_HEADER,
downloadBFile,
filterBiliDescLink,
getBiliAudio,
@ -516,26 +518,17 @@ export class tools extends plugin {
]);
return true;
}
// 处理番剧
if (url.includes("ep")) {
const ep = url.match(/\/ep(\d+)/)?.[1];
await this.biliEpInfo(ep, e);
return true
}
// 处理专栏
if (e.msg !== undefined && e.msg.includes("read\/cv")) {
this.linkShareSummary(e);
return true;
}
// 处理下载逻辑
if (e.msg !== undefined && e.msg.startsWith("下载")) {
// 检测是否扫码了,如果没有扫码数据终止下载
if (_.isEmpty(this.biliSessData)) {
e.reply("检测到没有填写biliSessData下载终止");
return true;
}
await this.downloadBiliVideo(e, url, this.biliSessData);
return true;
}
// 只提取音乐处理
if (e.msg !== undefined && e.msg.startsWith("音乐")) {
e.reply(`${this.identifyPrefix} 识别:哔哩哔哩音乐,正在提取请稍候...`)
return await this.biliMusic(e, url);
}
// 动态处理
if (url.includes("t.bilibili.com") || url.includes("bilibili.com\/opus")) {
if (_.isEmpty(this.biliSessData)) {
@ -582,9 +575,8 @@ export class tools extends plugin {
if (!_.isEmpty(summary)) {
Msg = await Bot.makeForwardMsg(textArrayToMakeForward(e, [`「R插件 x bilibili」联合为您总结内容`, summary]));
}
// 不提取音乐,正常处理
if (isLimitDuration) {
// 限制视频解析
if (isLimitDuration) {
const durationInMinutes = (curDuration / 60).toFixed(0);
biliInfo.push(`${ DIVIDING_LINE.replace('{}', '限制说明') }\n当前视频时长约:${ durationInMinutes }分钟,\n大于管理员设置的最大时长 ${ this.biliDuration / 60 } 分钟!`);
Msg && (await e.reply(Msg));
@ -594,6 +586,21 @@ export class tools extends plugin {
Msg && (await e.reply(Msg));
e.reply(biliInfo);
}
// 处理下载逻辑
if (e.msg !== undefined && e.msg.startsWith("下载")) {
// 检测是否扫码了,如果没有扫码数据终止下载
if (_.isEmpty(this.biliSessData)) {
e.reply("检测到没有填写biliSessData下载终止");
return true;
}
await this.downloadBiliVideo(e, url, this.biliSessData);
return true;
}
// 只提取音乐处理
if (e.msg !== undefined && e.msg.startsWith("音乐")) {
e.reply(`${ this.identifyPrefix } 识别:哔哩哔哩音乐,正在提取请稍候...`)
return await this.biliMusic(e, url);
}
// 创建文件,如果不存在
const path = `${ this.getCurDownloadPath(e) }/`;
@ -606,6 +613,32 @@ export class tools extends plugin {
return true;
}
/**
* 获取哔哩哔哩番剧信息
* @param ep
* @param e
* @returns {Promise<void>}
*/
async biliEpInfo(ep, e) {
const resp = await (await fetch(BILI_EP_INFO.replace("{}", ep), {
headers: BILI_HEADER
})).json();
const result = resp.result;
const dataProcessMap = {
"播放": result.stat.views,
"弹幕": result.stat.danmakus,
"点赞": result.stat.likes,
"分享": result.stat.coins,
"追番": result.stat.favorites,
"收藏": result.stat.favorite,
};
e.reply([
segment.image(resp.result.cover),
`${ this.identifyPrefix }识别:哔哩哔哩番剧,${ result.title }\n🎯 评分: ${ result?.rating?.score ?? '-' } / ${ result?.rating?.count ?? '-' }\n📺 ${ result.new_ep.desc }, ${ result.seasons[0].new_ep.index_show }\n`,
`${ formatBiliInfo(dataProcessMap) }`,
], true)
}
/**
* 哔哩哔哩下载策略
* @param e 事件

View File

@ -68,6 +68,13 @@ export const BILI_STREAM_INFO = "https://api.live.bilibili.com/room/v1/Room/get_
*/
export const BILI_ONLINE = "https://api.bilibili.com/x/player/online/total?bvid={0}&cid={1}"
/**
* 剧集基本信息
* https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/bangumi/info.md
* @type {string}
*/
export const BILI_EP_INFO = "https://api.bilibili.com/pgc/view/web/season?ep_id={}"
/**
* 米游社网页端获取文章
* https://github.com/UIGF-org/mihoyo-api-collect/blob/main/hoyolab/article/article.md#%E8%8E%B7%E5%8F%96%E5%AE%8C%E6%95%B4%E6%96%87%E7%AB%A0%E4%BF%A1%E6%81%AF

View File

@ -10,13 +10,12 @@ export default class Version extends Base {
async getData (versionData) {
const version =
(versionData && versionData.length && versionData[0].version) || '1.0.0'
let data = {
return {
...this.screenData,
userId: version,
quality: 100,
saveId: version,
versionData
}
return data
}
}

View File

@ -15,7 +15,7 @@ import { mkdirIfNotExists } from "./file.js";
import { exec, spawn } from 'child_process';
import qrcode from "qrcode"
const biliHeaders = {
export const BILI_HEADER = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
referer: 'https://www.bilibili.com',
@ -56,7 +56,7 @@ async function normalDownloadBFile(url, fullFileName, progressCallback) {
.get(url, {
responseType: 'stream',
headers: {
...biliHeaders
...BILI_HEADER
},
})
.then(({ data, headers }) => {
@ -152,7 +152,7 @@ async function axelDownloadBFile(url, fullFileName, progressCallback, videoDownl
fullFileName = path.resolve(fullFileName);
// 构建 -H 参数
const headerParams = Object.entries(biliHeaders).map(
const headerParams = Object.entries(BILI_HEADER).map(
([key, value]) => `--header="${ key }: ${ value }"`
).join(' ');
@ -205,7 +205,7 @@ export async function getDownloadUrl(url) {
return axios
.get(url, {
headers: {
...biliHeaders
...BILI_HEADER
},
})
.then(({ data }) => {
@ -275,7 +275,7 @@ export async function m4sToMp3(m4sUrl, path) {
.get(m4sUrl, {
responseType: 'stream',
headers: {
...biliHeaders
...BILI_HEADER
},
}).then(async res => {
// 如果没有目录就创建一个
@ -394,7 +394,7 @@ export async function getDynamic(dynamicId, SESSDATA) {
const dynamicApi = BILI_DYNAMIC.replace("{}", dynamicId);
return axios.get(dynamicApi, {
headers: {
...biliHeaders,
...BILI_HEADER,
Cookie: `SESSDATA=${ SESSDATA }`
},
}).then(resp => {
@ -430,7 +430,7 @@ export async function getDynamic(dynamicId, SESSDATA) {
export async function getScanCodeData(qrcodeSavePath = 'qrcode.png', detectTime = 10, hook = () => {
}) {
try {
const resp = await axios.get(BILI_SCAN_CODE_GENERATE, { ...biliHeaders });
const resp = await axios.get(BILI_SCAN_CODE_GENERATE, { ...BILI_HEADER });
// 保存扫码的地址、扫码登录秘钥
const { url: scanUrl, qrcode_key } = resp.data.data;
await qrcode.toFile(qrcodeSavePath, scanUrl);
@ -446,7 +446,7 @@ export async function getScanCodeData(qrcodeSavePath = 'qrcode.png', detectTime
hook();
// 检测扫码情况默认 10s 检测一次并且尝试3次没扫就拜拜
while (code !== 0 && attemptCount < maxAttempts) {
loginResp = await axios.get(BILI_SCAN_CODE_DETECT.replace("{}", qrcode_key), { ...biliHeaders });
loginResp = await axios.get(BILI_SCAN_CODE_DETECT.replace("{}", qrcode_key), { ...BILI_HEADER });
code = loginResp.data.data.code;
await new Promise(resolve => setTimeout(resolve, detectTime * 1000)); // Wait for detectTime seconds
}