🦄 reactor: V1.6.7 整理多线程下载函数downloadVideo

This commit is contained in:
zhiyu1998 2024-04-05 12:00:19 +08:00
parent 434643b758
commit b5b4cd6c66
2 changed files with 159 additions and 121 deletions

View File

@ -2,9 +2,6 @@
import fetch from "node-fetch";
import fs from "node:fs";
import { Buffer } from 'node:buffer';
import { execFile } from 'child_process';
import { promisify } from 'util';
import path from "path";
// 其他库
import axios from "axios";
import _ from "lodash";
@ -1542,12 +1539,15 @@ export class tools extends plugin {
* @returns {Promise<void>}
*/
async downloadVideo(url, isProxy = false, headers = null, numThreads = 1) {
// 构造群信息参数
const { groupPath, target } = this.getGroupPathAndTarget.call(this);
await mkdirIfNotExists(groupPath);
// 构造header部分内容
const userAgent = "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36";
// 用户设置优先策略逻辑解释如果使用了这个函数优先查看用户是否设置了大于1的线程如果设置了优先使用没设置就开发者设定的函数设置
numThreads = this.videoDownloadConcurrency !== 1 ? this.videoDownloadConcurrency : numThreads;
// 构造代理参数
const proxyOption = {
...(isProxy && {
httpAgent: tunnel.httpOverHttp({
@ -1559,30 +1559,37 @@ export class tools extends plugin {
}),
}
/**
* 构造下载视频参数
* 构造信息链接头信息userAgent代理信息下载位置返回的路径
* @type {{headers: null, userAgent: string, groupPath: string, url, proxyOption: {}, target: string}}
*/
const downloadVideoParams = {
url,
headers,
userAgent,
proxyOption,
target,
groupPath,
}
// 如果是用户设置了单线程,则不分片下载
if (numThreads === 1) {
const axiosConfig = {
headers: headers || { "User-Agent": userAgent },
responseType: "stream",
...proxyOption
};
try {
await checkAndRemoveFile(target);
const res = await axios.get(url, axiosConfig);
logger.mark(`开始下载: ${ url }`);
const writer = fs.createWriteStream(target);
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(groupPath));
writer.on("error", reject);
});
} catch (err) {
logger.error(`下载视频发生错误!\ninfo:${ err }`);
}
return await this.downloadVideoWithSingleThread(downloadVideoParams);
} else {
return await this.downloadVideoWithMultiThread(downloadVideoParams, numThreads);
}
}
/**
* 多线程下载视频
* @link {downloadVideo}
* @param downloadVideoParams
* @param numThreads
* @returns {Promise<*>}
*/
async downloadVideoWithMultiThread(downloadVideoParams, numThreads) {
const { url, headers, userAgent, proxyOption, target, groupPath } = downloadVideoParams;
try {
// Step 1: 请求视频资源获取 Content-Length
const headRes = await axios.head(url, {
@ -1655,6 +1662,36 @@ export class tools extends plugin {
logger.error(`下载视频发生错误!\ninfo:${ err }`);
}
}
/**
* 单线程下载视频
* @link {downloadVideo}
* @returns {Promise<unknown>}
* @param downloadVideoParams
*/
async downloadVideoWithSingleThread(downloadVideoParams) {
const { url, headers, userAgent, proxyOption, target, groupPath } = downloadVideoParams;
const axiosConfig = {
headers: headers || { "User-Agent": userAgent },
responseType: "stream",
...proxyOption
};
try {
await checkAndRemoveFile(target);
const res = await axios.get(url, axiosConfig);
logger.mark(`开始下载: ${ url }`);
const writer = fs.createWriteStream(target);
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(groupPath));
writer.on("error", reject);
});
} catch (err) {
logger.error(`下载视频发生错误!\ninfo:${ err }`);
}
}
/**
@ -1725,10 +1762,11 @@ export class tools extends plugin {
const videoSize = (stats.size / (1024 * 1024)).toFixed(2);
if (videoSize > videoSizeLimit) {
e.reply(`当前视频大小:${ videoSize }MB\n大于设置的最大限制,\n改为上传群文件`);
if (this.e.bot?.sendUni) {
this.e.group.fs.upload(path);
// 判断是不是icqq
if (e.bot?.sendUni) {
e.group.fs.upload(path);
} else {
this.e.group.sendFile(path);
e.group.sendFile(path);
}
} else {
e.reply(segment.video(path));

View File

@ -1,10 +1,10 @@
- {
version: 1.6.6,
version: 1.6.7,
data:
[
新增<span class="cmd">超过文件大小转上传</span>功能,
新增<span class="cmd">B站下载</span>功能,
新增<span class="cmd">B站扫码</span>功能,
新增<span class="cmd">即刻解析</span>功能,
支持<span class="cmd">锅巴</span>插件,方便查看和修改配置,
添加<span class="cmd">#R帮助</span>获取插件帮助,
添加<span class="cmd">#R版本</span>获取插件版本,