From 796598df50ed9afe2be6557a781a676f38aaa901 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Sat, 17 Aug 2024 11:01:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore:=20=E6=B8=85=E7=90=86?= =?UTF-8?q?=E6=97=A0=E7=94=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/common.js | 109 +----------------------------------------------- 1 file changed, 1 insertion(+), 108 deletions(-) diff --git a/utils/common.js b/utils/common.js index a547749..fb23bd6 100644 --- a/utils/common.js +++ b/utils/common.js @@ -2,119 +2,12 @@ import axios from "axios"; import { exec } from "child_process"; import { HttpsProxyAgent } from 'https-proxy-agent'; import fetch from "node-fetch"; -import schedule from "node-schedule"; import fs from "node:fs"; import os from "os"; -import common from "../../../lib/common/common.js"; import path from 'path'; import { BILI_DOWNLOAD_METHOD, COMMON_USER_AGENT, TEN_THOUSAND } from "../constants/constant.js"; import { mkdirIfNotExists } from "./file.js"; -/** - * 请求模板 - */ -export class jFetch { - async get(url) { - const r = await fetch(url); - return await r.json(); - } - - async post(url, params) { - const r = await fetch(url, { ...params, method: "POST" }); - return await r.json(); - } -} - -/** - * 每日推送函数 - * @param func 回调函数 - * @param time cron - * @param isAutoPush 是否推送(开关) - */ -export function autoTask(func, time, groupList, isAutoPush = false) { - if (isAutoPush) { - schedule.scheduleJob(time, () => { - // 正常传输 - if (groupList instanceof Array) { - for (let i = 0; i < groupList.length; i++) { - const group = Bot.pickGroup(groupList[i]); - func(group); - common.sleep(1000); - } - // 防止恶意破坏函数 - } else if (groupList instanceof String) { - const group = Bot.pickGroup(groupList[i]); - func(group); - common.sleep(1000); - } else { - throw Error("错误传入每日推送参数!"); - } - }); - } -} - -/** - * 重试函数(暂时只用于抖音的api) - * @param func - * @param maxRetries - * @param delay - * @returns {Promise} - */ -export function retry(func, maxRetries = 3, delay = 1000) { - return new Promise((resolve, reject) => { - const attempt = (remainingTries) => { - func() - .then(resolve) - .catch(error => { - if (remainingTries === 1) { - reject(error); - } else { - console.log(`错误: ${ error }. 重试将在 ${ delay / 1000 } 秒...`); - setTimeout(() => attempt(remainingTries - 1), delay); - } - }); - }; - attempt(maxRetries); - }); -} - -/** - * 工具:下载pdf文件 - * @param url - * @param filename - * @returns {Promise} - */ -export function downloadPDF(url, filename) { - return axios({ - url: url, - responseType: "stream", - headers: { - "User-Agent": - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36", - }, - }).then(response => { - const writer = fs.createWriteStream(filename); - response.data.pipe(writer); - return new Promise((resolve, reject) => { - writer.on("finish", resolve); - writer.on("error", reject); - }); - }); -} - -/** - * 找到tiktok的视频id - * @param url - * @returns {Promise} - */ -export async function getIdVideo(url) { - const matching = url.includes("/video/"); - if (!matching) { - return null; - } - const idVideo = url.substring(url.indexOf("/video/") + 7, url.length); - return idVideo.length > 19 ? idVideo.substring(0, idVideo.indexOf("?")) : idVideo; -} export function generateRandomStr(randomlength = 16) { const base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789=' @@ -309,7 +202,7 @@ async function downloadImgWithAria2({ await mkdirIfNotExists(dir); // 构建 aria2c 命令 - let aria2cCmd = `aria2c "${ img }" --dir="${ dir }" --out="${ fileName }" --max-connection-per-server=${numThread} --split=${numThread} --min-split-size=1M --continue`; + let aria2cCmd = `aria2c "${ img }" --dir="${ dir }" --out="${ fileName }" --max-connection-per-server=${ numThread } --split=${ numThread } --min-split-size=1M --continue`; // 如果需要代理 if (isProxy) {