mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-12-05 18:01:56 +00:00
🦄 reactor: V1.6.7 整理多线程下载函数downloadVideo
This commit is contained in:
parent
434643b758
commit
b5b4cd6c66
@ -2,9 +2,6 @@
|
|||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { Buffer } from 'node:buffer';
|
import { Buffer } from 'node:buffer';
|
||||||
import { execFile } from 'child_process';
|
|
||||||
import { promisify } from 'util';
|
|
||||||
import path from "path";
|
|
||||||
// 其他库
|
// 其他库
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
@ -1542,12 +1539,15 @@ export class tools extends plugin {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async downloadVideo(url, isProxy = false, headers = null, numThreads = 1) {
|
async downloadVideo(url, isProxy = false, headers = null, numThreads = 1) {
|
||||||
|
// 构造群信息参数
|
||||||
const { groupPath, target } = this.getGroupPathAndTarget.call(this);
|
const { groupPath, target } = this.getGroupPathAndTarget.call(this);
|
||||||
await mkdirIfNotExists(groupPath);
|
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";
|
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的线程,如果设置了优先使用,没设置就开发者设定的函数设置
|
// 用户设置优先策略,逻辑解释:如果使用了这个函数优先查看用户是否设置了大于1的线程,如果设置了优先使用,没设置就开发者设定的函数设置
|
||||||
numThreads = this.videoDownloadConcurrency !== 1 ? this.videoDownloadConcurrency : numThreads;
|
numThreads = this.videoDownloadConcurrency !== 1 ? this.videoDownloadConcurrency : numThreads;
|
||||||
|
|
||||||
|
// 构造代理参数
|
||||||
const proxyOption = {
|
const proxyOption = {
|
||||||
...(isProxy && {
|
...(isProxy && {
|
||||||
httpAgent: tunnel.httpOverHttp({
|
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) {
|
if (numThreads === 1) {
|
||||||
const axiosConfig = {
|
return await this.downloadVideoWithSingleThread(downloadVideoParams);
|
||||||
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 }`);
|
|
||||||
}
|
|
||||||
} else {
|
} 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 {
|
try {
|
||||||
// Step 1: 请求视频资源获取 Content-Length
|
// Step 1: 请求视频资源获取 Content-Length
|
||||||
const headRes = await axios.head(url, {
|
const headRes = await axios.head(url, {
|
||||||
@ -1655,6 +1662,36 @@ export class tools extends plugin {
|
|||||||
logger.error(`下载视频发生错误!\ninfo:${ err }`);
|
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);
|
const videoSize = (stats.size / (1024 * 1024)).toFixed(2);
|
||||||
if (videoSize > videoSizeLimit) {
|
if (videoSize > videoSizeLimit) {
|
||||||
e.reply(`当前视频大小:${ videoSize }MB,\n大于设置的最大限制,\n改为上传群文件`);
|
e.reply(`当前视频大小:${ videoSize }MB,\n大于设置的最大限制,\n改为上传群文件`);
|
||||||
if (this.e.bot?.sendUni) {
|
// 判断是不是icqq
|
||||||
this.e.group.fs.upload(path);
|
if (e.bot?.sendUni) {
|
||||||
|
e.group.fs.upload(path);
|
||||||
} else {
|
} else {
|
||||||
this.e.group.sendFile(path);
|
e.group.sendFile(path);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
e.reply(segment.video(path));
|
e.reply(segment.video(path));
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
- {
|
- {
|
||||||
version: 1.6.6,
|
version: 1.6.7,
|
||||||
data:
|
data:
|
||||||
[
|
[
|
||||||
|
新增<span class="cmd">超过文件大小转上传</span>功能,
|
||||||
新增<span class="cmd">B站下载</span>功能,
|
新增<span class="cmd">B站下载</span>功能,
|
||||||
新增<span class="cmd">B站扫码</span>功能,
|
新增<span class="cmd">B站扫码</span>功能,
|
||||||
新增<span class="cmd">即刻解析</span>功能,
|
|
||||||
支持<span class="cmd">锅巴</span>插件,方便查看和修改配置,
|
支持<span class="cmd">锅巴</span>插件,方便查看和修改配置,
|
||||||
添加<span class="cmd">#R帮助</span>获取插件帮助,
|
添加<span class="cmd">#R帮助</span>获取插件帮助,
|
||||||
添加<span class="cmd">#R版本</span>获取插件版本,
|
添加<span class="cmd">#R版本</span>获取插件版本,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user