From ebca7c93a5e498fde9d31c1b5041a312e8e9e4f7 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Sun, 4 Aug 2024 22:49:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=E5=88=87?= =?UTF-8?q?=E6=8D=A2=20`BBDown`=20=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ apps/tools.js | 61 +++++++++++++++++++++++++++++++++----------- config/tools.yaml | 1 + guoba.support.js | 8 ++++++ utils/bbdown-util.js | 53 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 utils/bbdown-util.js diff --git a/README.md b/README.md index 34c05b6..dc8085e 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,10 @@ aiApiKey: '' # 用于识图的api key,kimi接口申请:https://platform.moon ![](./img/xhs-ck-export.webp) +### 📺 关于使用 BBDown 下载 + +- Linux教程:https://pwa.sspai.com/post/83345 +- Windows教程:https://github.com/nilaoda/BBDown/issues/305 ## 🤺 R插件交流群 diff --git a/apps/tools.js b/apps/tools.js index e40b4a4..72be0a3 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -81,6 +81,7 @@ import {LagrangeAdapter} from "../utils/lagrange-adapter.js"; import path from "path"; import {OpenaiBuilder} from "../utils/openai-builder.js"; import {contentEstimator} from "../utils/link-share-summary-util.js"; +import {checkBBDown, startBBDown} from "../utils/bbdown-util.js"; export class tools extends plugin { /** @@ -206,6 +207,8 @@ export class tools extends plugin { this.biliSessData = this.toolsConfig.biliSessData; // 加载哔哩哔哩的限制时长 this.biliDuration = this.toolsConfig.biliDuration; + // 加载哔哩哔哩是否使用BBDown + this.biliUseBBDown = this.toolsConfig.biliUseBBDown; // 加载抖音Cookie this.douyinCookie = this.toolsConfig.douyinCookie; // 加载抖音是否压缩 @@ -593,25 +596,53 @@ export class tools extends plugin { // 加入队列 this.queue.add(async () => { // 下载文件 - getDownloadUrl(url) - .then(data => { - this.downBili(`${path}temp`, data.videoUrl, data.audioUrl) - .then(_ => { - this.sendVideoToUpload(e, `${path}temp.mp4`) - }) - .catch(err => { - logger.error(err); - e.reply("解析失败,请重试一下"); - }); - }) - .catch(err => { - logger.error(err); - e.reply("解析失败,请重试一下"); - }); + await this.biliDownloadStrategy(e, url, path); }) return true; } + /** + * 哔哩哔哩下载策略 + * @param e 事件 + * @param url 链接 + * @param path 保存路径 + * @returns {Promise} + */ + async biliDownloadStrategy(e, url, path) { + // =================以下是调用BBDown的逻辑===================== + // 下载视频和音频 + const tempPath = `${path}temp`; + // 检测是否开启BBDown + if (this.biliUseBBDown) { + // 检测环境的 BBDown + const isExistBBDown = await checkBBDown(); + // 存在 BBDown + if (isExistBBDown) { + // 删除之前的文件 + await checkAndRemoveFile(`${tempPath}.mp4`); + // 下载视频 + await startBBDown(url, path, this.biliSessData); + // 发送视频 + return this.sendVideoToUpload(e, `${tempPath}.mp4`); + } + e.reply("🚧 R插件提醒你:开启但未检测到当前环境有【BBDown】,即将使用默认下载方式 ( ◡̀_◡́)ᕤ"); + } + // =================默认下载方式===================== + try { + // 获取下载链接 + const data = await getDownloadUrl(url); + + await this.downBili(tempPath, data.videoUrl, data.audioUrl); + + // 上传视频 + return this.sendVideoToUpload(e, `${tempPath}.mp4`); + } catch (err) { + // 错误处理 + logger.error('[R插件][哔哩哔哩视频发送]下载错误,具体原因为:', err); + e.reply("解析失败,请重试一下"); + } + } + /** * 下载哔哩哔哩最高画质视频 * @param e 交互事件 diff --git a/config/tools.yaml b/config/tools.yaml index 735792c..cfd0d26 100644 --- a/config/tools.yaml +++ b/config/tools.yaml @@ -8,6 +8,7 @@ translateSecret: '' # 百度翻译密匙 biliSessData: '' # 哔哩哔哩的SESSDATA biliIntroLenLimit: 50 # 哔哩哔哩简介长度限制,填 0 或者 -1 可以不做任何限制,显示完整简介 biliDuration: 480 # 哔哩哔哩限制的最大视频时长(默认8分钟),单位:秒 +biliUseBBDown: false # 是否使用BBDown,默认不开启,开启后使用强劲的BBDown下载最高画质 douyinCookie: '' # douyin's cookie, 格式:odin_tt=xxx;passport_fe_beating_status=xxx;sid_guard=xxx;uid_tt=xxx;uid_tt_ss=xxx;sid_tt=xxx;sessionid=xxx;sessionid_ss=xxx;sid_ucp_v1=xxx;ssid_ucp_v1=xxx;passport_assist_user=xxx;ttwid=xxx; douyinCompression: true # true-压缩,false-不压缩;是否使用压缩视频格式的抖音(默认使用),使用后加速视频发送 diff --git a/guoba.support.js b/guoba.support.js index 60d1957..e7e96a2 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -110,6 +110,14 @@ export function supportGuoba() { placeholder: "请输入哔哩哔哩的简介长度限制(默认50个字符),填 0 或者 -1 可以不做任何限制,显示完整简介", }, }, + { + field: "tools.biliUseBBDown", + label: "使用BBDown下载", + bottomHelpMessage: + "【默认不开启,涉及范围只有哔哩哔哩,开启后默认最高画质发送】如果不爱折腾就使用默认下载方式,如果喜欢折腾就开启,开启后下载更强劲,并且一劳永逸!", + component: "Switch", + required: false, + }, { field: "tools.douyinCookie", label: "抖音的Cookie", diff --git a/utils/bbdown-util.js b/utils/bbdown-util.js new file mode 100644 index 0000000..35ac29d --- /dev/null +++ b/utils/bbdown-util.js @@ -0,0 +1,53 @@ +import { exec } from 'child_process'; +import os from 'os'; + +/** + * 检测 BBDown 是否存在 + * @param callback + */ +export function checkBBDown(callback) { + // 根据操作系统选择命令 + return new Promise((resolve, reject) => { + const command = os.platform() === 'win32' ? 'where BBDown' : 'which BBDown'; + + exec(command, (error, stdout, stderr) => { + if (error) { + logger.error(`[R插件][BBDown]未找到: ${stderr || error.message}`); + resolve(false); + } + logger.info(`[R插件][BBDown]找到: ${stdout.trim()}`); + resolve(true); + }); + }); +} + +/** + * 使用BBDown下载 + * @param videoUrl 视频链接 + * @param downloadDir 下载目录 + * @param biliSessData cookie + */ +export function startBBDown(videoUrl, downloadDir, biliSessData) { + return new Promise((resolve, reject) => { + // logger.info(videoUrl); + // 解析URL并提取参数p(页数) + const urlObj = new URL(videoUrl); + const pageParam = urlObj.searchParams.get('p'); + // 说明:-F 自定义名称,-c 自定义Cookie, --work-dir 设置下载目录,-M 多p下载的时候命名 + const command = `BBDown ${videoUrl} --work-dir ${downloadDir} ${biliSessData ? '-c ' + biliSessData : ''} ${pageParam ? '-p ' + pageParam + ' -M \"temp\"' : '-p 1' + ' -M \"temp\"'} -F temp`; + // logger.info(command); + // 直接调用BBDown,因为它已经在系统路径中 + exec(command, (error, stdout, stderr) => { + if (error) { + reject(`[R插件][BBDown]执行出错: ${error.message}`); + return; + } + if (stderr) { + reject(`[R插件][BBDown]错误信息: ${stderr}`); + return; + } + logger.info(`[R插件][BBDown]输出结果: ${stdout}`); + resolve(stdout); + }); + }); +} \ No newline at end of file