From 1dd533b9fb491ad84d8b251be6c8add464c49d1d Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Fri, 8 Mar 2024 17:07:18 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20V1.5.5=20[#I96AOZ]?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6=E6=9B=B4=E6=96=B0=E5=90=8E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BC=9A=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=20&=20=E8=A7=A3=E5=86=B3xhs=E6=97=A0=E6=B3=95=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=A7=86=E9=A2=91=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 解决xhs无法解析视频的bug(感谢群友Melatonin提供bug例子) 2. [#I96AOZ] beta: 修复插件更新后配置文件会重置 --- apps/tools.js | 4 ++-- apps/update.js | 54 +++++++++++++++++++++++++++++++++++++++++++-- config/version.yaml | 2 +- utils/file.js | 25 ++++++++++++++++++++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index aada31e..66d1718 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -676,7 +676,7 @@ export class tools extends plugin { const reg = /window\.__INITIAL_STATE__=(.*?)<\/script>/; const res = xhsHtml.match(reg)[1].replace(/undefined/g, "null"); const resJson = JSON.parse(res); - const noteData = resJson.note.noteDetailMap[id].note + const noteData = resJson.note.noteDetailMap[id].note; const { title, desc, type } = noteData; let imgPromise = []; if (type === "video") { @@ -684,7 +684,7 @@ export class tools extends plugin { const cover = noteData.imageList?.[0].urlDefault; e.reply([segment.image(cover), `识别:小红书, ${ title }\n${ desc }`]); // 构造xhs视频链接 - const xhsVideoUrl = `${ XHS_VIDEO }${ noteData.video.consumer.originVideoKey.replace('pre_post\/', '') }`; + const xhsVideoUrl = noteData.video.media.stream.h264?.[0]?.masterUrl; // 下载视频 this.downloadVideo(xhsVideoUrl).then(path => { if (path === undefined) { diff --git a/apps/update.js b/apps/update.js index 1e71c33..87a71cd 100644 --- a/apps/update.js +++ b/apps/update.js @@ -3,8 +3,10 @@ import Version from "../model/version.js"; import config from "../model/index.js"; import puppeteer from "../../../lib/puppeteer/puppeteer.js"; import lodash from "lodash"; +import YAML from "yaml"; import { exec, execSync } from "node:child_process"; +import { copyFiles, deleteFolderRecursive, readCurrentDir } from "../utils/file.js"; /** * 处理插件更新1 @@ -57,6 +59,9 @@ export class update extends plugin { const pluginName = "rconsole-plugin"; + // 保存配置文件 + await copyFiles(`./plugins/${pluginName}/config`, "./temp/rconsole-update-tmp"); + let command = `git -C ./plugins/${pluginName}/ pull --no-rebase`; if (isForce) { command = `git -C ./plugins/${pluginName}/ checkout . && ${command}`; @@ -78,6 +83,18 @@ export class update extends plugin { e.reply(`R插件更新成功,最后更新时间:${time}`); e.reply(await this.getLog(pluginName)); } + + // 读取配置文件比对更新 + const confFiles = await readCurrentDir("./temp/rconsole-update-tmp"); + for (let confFile of confFiles) { + await this.compareAndUpdateYaml( + `./temp/rconsole-update-tmp/${confFile}`, + `./plugins/${pluginName}/config/${confFile}` + ); + } + // 删除临时文件 + await deleteFolderRecursive("./temp/rconsole-update-tmp"); + return true; } @@ -148,8 +165,8 @@ export class update extends plugin { } else if (errMsg.includes("be overwritten by merge")) { await this.reply( msg + - `存在冲突:\n${errMsg}\n` + - "请解决冲突后再更新,或者执行#强制更新,放弃本地修改", + `存在冲突:\n${errMsg}\n` + + "请解决冲突后再更新,或者执行#强制更新,放弃本地修改", ); } else if (stdout.includes("CONFLICT")) { await this.reply([ @@ -162,4 +179,37 @@ export class update extends plugin { await this.reply([errMsg, stdout]); } } + + async compareAndUpdateYaml(sourcePath, updatedPath) { + try { + // Step 1 & 2: Read and parse YAML files + const sourceContent = await fs.readFileSync(sourcePath, 'utf8'); + const updatedContent = await fs.readFileSync(updatedPath, 'utf8'); + const sourceObj = YAML.parse(sourceContent); + const updatedObj = YAML.parse(updatedContent); + + // Step 3: Compare objects and merge changes + Object.keys(updatedObj).forEach(key => { + if (!sourceObj.hasOwnProperty(key)) { + sourceObj[key] = updatedObj[key]; // Add new keys with updated values + } + }); + + Object.keys(sourceObj).forEach(key => { + if (!updatedObj.hasOwnProperty(key)) { + delete sourceObj[key]; // Remove keys not present in updated object + } + }); + + // Step 4 & 5: Convert object back to YAML + const newYamlContent = YAML.stringify(sourceObj); + + // Step 6: Write the updated YAML back to the updatedPath + await fs.writeFileSync(updatedPath, newYamlContent, 'utf8'); + + logger.info(`[R插件更新配置文件记录]${updatedPath}`); + } catch (error) { + logger.error(error); + } + } } diff --git a/config/version.yaml b/config/version.yaml index 9ca62af..27913cf 100644 --- a/config/version.yaml +++ b/config/version.yaml @@ -1,5 +1,5 @@ - { - version: 1.5.4, + version: 1.5.5, data: [ 新增Pixivision解析功能, diff --git a/utils/file.js b/utils/file.js index ddf2c03..88d4b11 100644 --- a/utils/file.js +++ b/utils/file.js @@ -77,4 +77,27 @@ export async function readCurrentDir(path) { } catch (err) { logger.error(err); } -} \ No newline at end of file +} + +/** + * 拷贝文件 + * @param srcDir + * @param destDir + * @returns {Promise<*|null>} + */ +export async function copyFiles(srcDir, destDir) { + try { + await mkdirIfNotExists(destDir); + + const files = await readCurrentDir(srcDir); + + for (const file of files) { + const srcFile = path.join(srcDir, file); + const destFile = path.join(destDir, file); + await fs.promises.copyFile(srcFile, destFile); + } + } catch (error) { + logger.error(error); + } + return null; +}