diff --git a/apps/query.js b/apps/query.js index 2028bd6..812fce6 100644 --- a/apps/query.js +++ b/apps/query.js @@ -7,7 +7,7 @@ import axios from "axios"; // 常量 import { CAT_LIMIT, COMMON_USER_AGENT } from "../constants/constant.js"; // 配置文件 -import config from "../model/index.js"; +import config from "../model/config.js"; export class query extends plugin { diff --git a/apps/switchers.js b/apps/switchers.js index e180e4f..acfe8c2 100644 --- a/apps/switchers.js +++ b/apps/switchers.js @@ -1,4 +1,4 @@ -import config from "../model/index.js"; +import config from "../model/config.js"; import schedule from 'node-schedule'; import { REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE } from "../constants/constant.js"; import { deleteFolderRecursive, readCurrentDir } from "../utils/file.js"; diff --git a/apps/tools.js b/apps/tools.js index 884bc9e..4ab28c0 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -1,26 +1,13 @@ -// 主库 -import fetch from "node-fetch"; -import fs from "node:fs"; -import { Buffer } from 'node:buffer'; -// 其他库 import axios from "axios"; -import _ from "lodash"; -import { HttpsProxyAgent } from 'https-proxy-agent'; import { exec, execSync } from "child_process"; -import { checkAndRemoveFile, deleteFolderRecursive, mkdirIfNotExists } from "../utils/file.js"; -import { - downloadBFile, - filterBiliDescLink, - getBiliAudio, - getBiliVideoWithSession, - getDownloadUrl, - getDynamic, - getScanCodeData, - getVideoInfo, - m4sToMp3, - mergeFileToMp4 -} from "../utils/bilibili.js"; -import { downloadM3u8Videos, mergeAcFileToMp4, parseM3u8, parseUrl } from "../utils/acfun.js"; +import { HttpsProxyAgent } from 'https-proxy-agent'; +import _ from "lodash"; +import fetch from "node-fetch"; +import { Buffer } from 'node:buffer'; +import fs from "node:fs"; +import PQueue from 'p-queue'; +import path from "path"; +import querystring from "querystring"; import { BILI_CDN_SELECT_LIST, BILI_DEFAULT_INTRO_LEN_LIMIT, @@ -35,27 +22,6 @@ import { TWITTER_BEARER_TOKEN, XHS_NO_WATERMARK_HEADER, } from "../constants/constant.js"; -import { - checkCommandExists, - cleanFilename, - downloadAudio, - downloadImg, - estimateReadingTime, - formatBiliInfo, - retryAxiosReq, - saveJsonToFile, - secondsToTime, - testProxy, - truncateString -} from "../utils/common.js"; -import config from "../model/index.js"; -import Translate from "../utils/trans-strategy.js"; -import * as aBogus from "../utils/a-bogus.cjs"; -import { getBodianAudio, getBodianMusicInfo, getBodianMv } from "../utils/bodian.js"; -import { av2BV } from "../utils/bilibili-bv-av-convert.js"; -import querystring from "querystring"; -import PQueue from 'p-queue'; -import { getWbi } from "../utils/biliWbi.js"; import { BILI_STREAM_INFO, BILI_SUMMARY, @@ -73,14 +39,45 @@ import { WEISHI_VIDEO_INFO, XHS_REQ_LINK } from "../constants/tools.js"; -import { getDS } from "../utils/mihoyo.js"; -import GeneralLinkAdapter from "../utils/general-link-adapter.js"; -import { mid2id } from "../utils/weibo.js"; -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 config from "../model/config.js"; +import * as aBogus from "../utils/a-bogus.cjs"; +import { downloadM3u8Videos, mergeAcFileToMp4, parseM3u8, parseUrl } from "../utils/acfun.js"; import { checkBBDown, startBBDown } from "../utils/bbdown-util.js"; +import { av2BV } from "../utils/bilibili-bv-av-convert.js"; +import { + downloadBFile, + filterBiliDescLink, + getBiliAudio, + getBiliVideoWithSession, + getDownloadUrl, + getDynamic, + getScanCodeData, + getVideoInfo, + m4sToMp3, + mergeFileToMp4 +} from "../utils/bilibili.js"; +import { getWbi } from "../utils/biliWbi.js"; +import { getBodianAudio, getBodianMusicInfo, getBodianMv } from "../utils/bodian.js"; +import { + checkCommandExists, + cleanFilename, + downloadAudio, + downloadImg, + estimateReadingTime, + formatBiliInfo, + retryAxiosReq, + secondsToTime, + testProxy, + truncateString +} from "../utils/common.js"; +import { checkAndRemoveFile, deleteFolderRecursive, mkdirIfNotExists } from "../utils/file.js"; +import GeneralLinkAdapter from "../utils/general-link-adapter.js"; +import { LagrangeAdapter } from "../utils/lagrange-adapter.js"; +import { contentEstimator } from "../utils/link-share-summary-util.js"; +import { getDS } from "../utils/mihoyo.js"; +import { OpenaiBuilder } from "../utils/openai-builder.js"; +import Translate from "../utils/trans-strategy.js"; +import { mid2id } from "../utils/weibo.js"; import { textArrayToMakeForward } from "../utils/yunzai-util.js"; export class tools extends plugin { diff --git a/apps/update.js b/apps/update.js index 2049474..a6dcbe3 100644 --- a/apps/update.js +++ b/apps/update.js @@ -1,6 +1,6 @@ // 主库 import Version from "../model/version.js"; -import config from "../model/index.js"; +import config from "../model/config.js"; import puppeteer from "../../../lib/puppeteer/puppeteer.js"; import lodash from "lodash"; import YAML from "yaml"; @@ -12,7 +12,12 @@ import { copyFiles, deleteFolderRecursive, readCurrentDir } from "../utils/file. /** * 处理插件更新1 */ -export class update extends plugin { +export class Update extends plugin { + static pluginName = (() => { + const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json'); + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + return packageJson.name; + })(); constructor() { super({ name: "R插件更新插件", @@ -58,16 +63,14 @@ export class update extends plugin { let isForce = !!e.msg.includes("强制"); - const pluginName = "rconsole-plugin"; - // 保存配置文件 - await copyFiles(`./plugins/${pluginName}/config`, "./temp/rconsole-update-tmp"); + await copyFiles(`./plugins/${Update.pluginName}/config`, "./temp/rconsole-update-tmp"); - let command = `git -C ./plugins/${pluginName}/ pull --no-rebase`; + let command = `git -C ./plugins/${Update.pluginName}/ pull --no-rebase`; if (isForce) { - command = `git -C ./plugins/${pluginName}/ checkout . && ${command}`; + command = `git -C ./plugins/${Update.pluginName}/ checkout . && ${command}`; } - this.oldCommitId = await this.getCommitId(pluginName); + this.oldCommitId = await this.getCommitId(Update.pluginName); await e.reply("正在执行更新操作,请稍等"); let ret = await this.execSync(command); @@ -76,13 +79,13 @@ export class update extends plugin { await this.gitErr(ret.error, ret.stdout); return false; } - const time = await this.getTime(pluginName); + const time = await this.getTime(Update.pluginName); if (/Already up|已经是最新/g.test(ret.stdout)) { e.reply(`R插件已经是最新: ${this.versionData[0].version}`); } else { this.isUp = true; e.reply(`R插件更新成功,最后更新时间:${time}`); - e.reply(await this.getLog(pluginName)); + e.reply(await this.getLog(Update.pluginName)); } // 读取配置文件比对更新 @@ -90,7 +93,7 @@ export class update extends plugin { for (let confFile of confFiles) { await this.compareAndUpdateYaml( `./temp/rconsole-update-tmp/${confFile}`, - `./plugins/${pluginName}/config/${confFile}` + `./plugins/${Update.pluginName}/config/${confFile}` ); } // 删除临时文件 diff --git a/guoba.support.js b/guoba.support.js index 470e7ef..a753a15 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -1,14 +1,16 @@ -import path from "path"; -import model from "./model/index.js"; import _ from "lodash"; +import path from "path"; import { BILI_CDN_SELECT_LIST, BILI_DOWNLOAD_METHOD } from "./constants/constant.js"; +import model from "./model/config.js"; -const _path = process.cwd() + "/plugins/rconsole-plugin"; +const pluginName = `rconsole-plugin`; + +const _path = process.cwd() + `/plugins/${pluginName}`; export function supportGuoba() { return { pluginInfo: { name: "R插件", - title: "rconsole-plugin", + title: pluginName, author: "@zhiyu", authorLink: "https://gitee.com/kyrzy0416", link: "https://gitee.com/kyrzy0416/rconsole-plugin", @@ -261,9 +263,8 @@ export function supportGuoba() { for (let [key, value] of Object.entries(data)) { _.set(config, key, value); } - // TODO 目前只有一个文件的配置,暂时这样写 config = _.merge({}, model.getConfig("tools"), config.tools); - model.saveSet("tools", config); + model.saveAllConfig("tools", config); return Result.ok({}, "保存成功~"); }, }, diff --git a/index.js b/index.js index 39fb56c..30a65e5 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,18 @@ import fs from "node:fs"; -import RConfig from "./model/index.js"; +import path from "path"; +import config from "./model/config.js"; if (!global.segment) { global.segment = (await import("oicq")).segment } -const versionData = RConfig.getConfig("version"); +const versionData = config.getConfig("version"); +const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json'); +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); +const pluginName = packageJson.name; -logger.info(`R插件${versionData[0].version}初始化`); +logger.info(logger.yellow(`R插件${versionData[0].version}初始化,欢迎加入【R插件和它的朋友们】秋秋群:575663150`)); -const files = fs.readdirSync("./plugins/rconsole-plugin/apps").filter(file => file.endsWith(".js")); +const files = fs.readdirSync(`./plugins/${pluginName}/apps`).filter(file => file.endsWith(".js")); let ret = []; diff --git a/model/base.js b/model/base.js index ef3f345..865ccd5 100644 --- a/model/base.js +++ b/model/base.js @@ -1,13 +1,22 @@ -export default class base { +import path from 'path'; +import fs from "fs"; + +export default class Base { + static pluginName = (() => { + const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json'); + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + return packageJson.name; + })(); + constructor (e = {}) { this.e = e this.userId = e?.user_id - this.model = 'rconsole-plugin' + this.model = Base.pluginName this._path = process.cwd().replace(/\\/g, '/') } get prefix () { - return `Yz:rconsole-plugin:${this.model}:` + return `Yz:${Base.pluginName}:${this.model}:` } /** @@ -19,9 +28,9 @@ export default class base { get screenData () { return { saveId: this.userId, - tplFile: `./plugins/rconsole-plugin/resources/html/${this.model}/${this.model}.html`, + tplFile: `./plugins/${Base.pluginName}/resources/html/${this.model}/${this.model}.html`, /** 绝对路径 */ - pluResPath: `${this._path}/plugins/rconsole-plugin/resources/` + pluResPath: `${this._path}/plugins/${Base.pluginName}/resources/` } } } diff --git a/model/config.js b/model/config.js new file mode 100644 index 0000000..27424af --- /dev/null +++ b/model/config.js @@ -0,0 +1,79 @@ +import fs from 'node:fs'; +import _ from 'lodash'; +import YAML from 'yaml'; +import chokidar from 'chokidar'; +import Base from "./base.js"; + +class RConfig extends Base{ + constructor(e) { + super(e); + this.configPath = `./plugins/${RConfig.pluginName}/config/`; + this.change = new Map(); + this.previousContent = new Map(); // 存储每个文件之前的内容 + } + + getConfig(name) { + return this.getYaml(name); + } + + getField(name, field) { + const config = this.getConfig(name); + return config[field]; + } + + updateField(name, field, value) { + let config = this.getConfig(name); + config[field] = value; + logger.mark(`[R插件][修改配置项][${name}][${field}]修改为:${value}`); + this.saveAllConfig(name, config); + } + + deleteField(name, field) { + let config = this.getConfig(name); + delete config[field]; + this.saveAllConfig(name, config); + } + + getYaml(name, isWatch = true) { + let file = this.getFilePath(name); + const yaml = YAML.parse(fs.readFileSync(file, 'utf8')); + + if (isWatch) { + this.change.set(name); + this.previousContent.set(name, yaml); // 保存初始内容 + this.watch(file, name); + } + return yaml; + } + + getFilePath(name) { + return `${this.configPath}${name}.yaml`; + } + + watch(file, name) { + const watcher = chokidar.watch(file); + + watcher.on('change', path => { + const currentContent = YAML.parse(fs.readFileSync(path, 'utf8')); + const previousContent = this.previousContent.get(name); + + if (!_.isEqual(previousContent, currentContent)) { + logger.mark(`[R插件][配置文件]:${name}已经被重置`); + this.previousContent.set(name, currentContent); // 更新之前的内容 + } + }); + } + + saveAllConfig(name, data) { + let file = this.getFilePath(name); + if (_.isEmpty(data)) { + fs.existsSync(file) && fs.unlinkSync(file); + } else { + let yaml = YAML.stringify(data); + fs.writeFileSync(file, yaml, 'utf8'); + } + this.watch(file, name); + } +} + +export default new RConfig(); diff --git a/model/help.js b/model/help.js index 6b8aa70..bacd51f 100644 --- a/model/help.js +++ b/model/help.js @@ -1,8 +1,8 @@ -import base from './base.js' -import config from './index.js' +import Base from './base.js' +import config from './config.js' import cfg from '../../../lib/config/config.js' -export default class Help extends base { +export default class Help extends Base { constructor(e) { super(e) this.model = 'help' diff --git a/model/index.js b/model/index.js deleted file mode 100644 index 1c8a88b..0000000 --- a/model/index.js +++ /dev/null @@ -1,104 +0,0 @@ -import fs from 'node:fs' -import _ from 'lodash' -import YAML from 'yaml' -import chokidar from 'chokidar' - -class RConfig { - constructor () { - // 配置文件 - this.configPath = './plugins/rconsole-plugin/config/' - this.config = {} - - // 监听文件 - this.watcher = { config: {} } - } - - /** - * 获取配置文件 - * @param name - * @returns {any} - */ - getConfig (name) { - let ignore = [] - - if (ignore.includes(`${name}`)) { - return this.getYaml(name) - } - - return this.getYaml(name) - } - - // 获取指定配置的某个字段 - getField(name, field) { - const config = this.getConfig(name); - return config[field]; - } - - // 更新指定配置的某个字段 - updateField(name, field, value) { - let config = this.getConfig(name); - config[field] = value; // 更新字段值 - this.saveSet(name, config); // 保存更改 - } - - // 删除指定配置的某个字段 - deleteField(name, field) { - let config = this.getConfig(name); - delete config[field]; // 删除指定字段 - this.saveSet(name, config); // 保存更改 - } - - /** - * 获取配置yaml - * @param name 名称 - */ - getYaml (name) { - // 获取文件路径 - let file = this.getFilePath(name) - // 解析xml - const yaml = YAML.parse(fs.readFileSync(file, 'utf8')) - // 监听文件 - this.watch(file, name) - return yaml - } - - /** - * 获取文件路径 - * @param name - * @returns {string} - */ - getFilePath (name) { - return `${this.configPath}${name}.yaml` - } - - /** - * 听配置文件 - * @param file - * @param name - */ - watch (file, name) { - const watcher = chokidar.watch(file) - - watcher.on('change', (path) => { - delete - logger.mark(`[修改配置文件][${name}]`) - }) - } - - /** - * 保存配置 - * @param name - * @param data - */ - saveSet (name, data) { - let file = this.getFilePath(name) - if (_.isEmpty(data)) { - fs.existsSync(file) && fs.unlinkSync(file) - } else { - let yaml = YAML.stringify(data) - fs.writeFileSync(file, yaml, 'utf8') - } - } -} - -export default new RConfig() diff --git a/model/version.js b/model/version.js index 0ab653c..d89d205 100644 --- a/model/version.js +++ b/model/version.js @@ -1,6 +1,6 @@ -import base from './base.js' +import Base from './base.js' -export default class Version extends base { +export default class Version extends Base { constructor (e) { super(e) this.model = 'version'