From fc0d4e372d087d5793c538c7c75b6312583b016f Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 21 Aug 2024 13:26:25 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20B=E7=AB=99=E7=95=AA?= =?UTF-8?q?=E5=89=A7=E9=93=BE=E6=8E=A5=E5=8A=A0=E5=85=A5=E7=BC=93=E5=AD=98?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E5=BF=AB=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/switchers.js | 14 ++++++------- apps/tools.js | 46 ++++++++++++++++++++++++++++++++++++------- constants/constant.js | 8 +++++++- utils/redis-util.js | 16 +++++++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/apps/switchers.js b/apps/switchers.js index 82922f8..79a868e 100644 --- a/apps/switchers.js +++ b/apps/switchers.js @@ -1,6 +1,6 @@ import config from "../model/config.js"; import schedule from 'node-schedule'; -import { REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE, REDOS_YUNZAI_WHITELIST } from "../constants/constant.js"; +import { REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE, REDIS_YUNZAI_WHITELIST } from "../constants/constant.js"; import { deleteFolderRecursive, readCurrentDir } from "../utils/file.js"; import { redisExistAndGetKey, redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.js"; @@ -142,7 +142,7 @@ export class switchers extends plugin { e.reply("无效的R信任用户"); return; } - let whiteList = await redisExistAndGetKey(REDOS_YUNZAI_WHITELIST); + let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST); // 不存在就创建 if (whiteList == null) { whiteList = []; @@ -154,7 +154,7 @@ export class switchers extends plugin { } whiteList = [...whiteList, trustUserId]; // 放置到Redis里 - await redisSetKey(REDOS_YUNZAI_WHITELIST, whiteList); + await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList); e.reply(`成功添加R信任用户:${ trustUserId }`); } @@ -164,7 +164,7 @@ export class switchers extends plugin { * @returns {Promise} */ async getWhiteList(e) { - let whiteList = await redisExistAndGetKey(REDOS_YUNZAI_WHITELIST); + let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST); if (whiteList == null) { whiteList = []; } @@ -191,7 +191,7 @@ export class switchers extends plugin { // 如果不是回复就看发送内容 trustUserId = e.msg.replace("#设置R信任用户", ""); } - let whiteList = await redisExistAndGetKey(REDOS_YUNZAI_WHITELIST); + let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST); if (whiteList == null) { e.reply("R插件当前没有任何信任用户!"); return; @@ -215,7 +215,7 @@ export class switchers extends plugin { trustUserId = e.msg.replace("#删除R信任用户", ""); } // 校准不是string的用户 - let whiteList = (await redisExistAndGetKey(REDOS_YUNZAI_WHITELIST)).map(item => + let whiteList = (await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST)).map(item => typeof item === 'string' ? item : item.toString() ); if (whiteList == null) { @@ -229,7 +229,7 @@ export class switchers extends plugin { } whiteList = whiteList.filter((item) => item !== trustUserId); // 放置到Redis里 - await redisSetKey(REDOS_YUNZAI_WHITELIST, whiteList); + await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList); e.reply(`成功删除R信任用户:${ trustUserId }`); } } diff --git a/apps/tools.js b/apps/tools.js index e9e203e..6451e56 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -16,10 +16,10 @@ import { douyinTypeMap, DOWNLOAD_WAIT_DETECT_FILE_TIME, HELP_DOC, - MESSAGE_RECALL_TIME, + MESSAGE_RECALL_TIME, REDIS_YUNZAI_ANIMELIST, REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE, - REDOS_YUNZAI_WHITELIST, + REDIS_YUNZAI_WHITELIST, SUMMARY_PROMPT, transMap, TWITTER_BEARER_TOKEN, @@ -85,7 +85,13 @@ 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 { redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.js"; +import { + redisExistAndGetKey, + redisExistAndInsertObject, + redisExistKey, + redisGetKey, + redisSetKey +} from "../utils/redis-util.js"; import { saveTDL, startTDL } from "../utils/tdl-util.js"; import Translate from "../utils/trans-strategy.js"; import { mid2id } from "../utils/weibo.js"; @@ -655,16 +661,42 @@ export class tools extends plugin { "追番": favorites, "收藏": favorite, }; + // 截断标题,查看Redis中是否存在,避免频繁走网络连接 + const { shortLink, shortLink2 } = await this.biliAnimeCacheDetect(result.title); e.reply([ segment.image(resp.result.cover), `${ this.identifyPrefix }识别:哔哩哔哩番剧,${ result.title }\n🎯 评分: ${ result?.rating?.score ?? '-' } / ${ result?.rating?.count ?? '-' }\n📺 ${ result.new_ep.desc }, ${ result.seasons[0].new_ep.index_show }\n`, `${ formatBiliInfo(dataProcessMap) }`, - `\n\n🪶 在线观看: ${ await urlTransformShortLink(ANIME_SERIES_SEARCH_LINK + result.title) }`, - `\n🌸 在线观看: ${ await urlTransformShortLink(ANIME_SERIES_SEARCH_LINK2 + result.title) }` + `\n\n🪶 在线观看: ${ shortLink }`, + `\n🌸 在线观看: ${ shortLink2 }` ], true); return ep; } + /** + * 短链接缓存 + * @param title + * @returns {Promise<{shortLink2: string, shortLink: string}|*>} + */ + async biliAnimeCacheDetect(title) { + const animeList = await redisExistAndGetKey(REDIS_YUNZAI_ANIMELIST) + if (animeList && animeList?.[title] !== undefined) { + return animeList?.[title]; + } + const shortLink = await urlTransformShortLink(ANIME_SERIES_SEARCH_LINK + title); + const shortLink2 = await urlTransformShortLink(ANIME_SERIES_SEARCH_LINK2 + title); + await redisExistAndInsertObject(REDIS_YUNZAI_ANIMELIST, { + [title]: { + shortLink, + shortLink2 + } + }); + return { + shortLink, + shortLink2 + } + } + /** * 哔哩哔哩下载策略 * @param e 事件 @@ -2286,10 +2318,10 @@ export class tools extends plugin { */ async isTrustUser(userId) { // 如果不存在则返回 - if (!(await redisExistKey(REDOS_YUNZAI_WHITELIST))) { + if (!(await redisExistKey(REDIS_YUNZAI_WHITELIST))) { return false; } - const whiteList = await redisGetKey(REDOS_YUNZAI_WHITELIST); + const whiteList = await redisGetKey(REDIS_YUNZAI_WHITELIST); return whiteList.includes(userId.toString()) || whiteList.includes(userId); } diff --git a/constants/constant.js b/constants/constant.js index baf6fd1..66cb408 100644 --- a/constants/constant.js +++ b/constants/constant.js @@ -84,7 +84,13 @@ export const REDIS_YUNZAI_LAGRANGE = "Yz:rconsole:tools:lagrange"; * 某些功能的解析白名单 * @type {string} */ -export const REDOS_YUNZAI_WHITELIST = "Yz:rconsole:tools:whitelist"; +export const REDIS_YUNZAI_WHITELIST = "Yz:rconsole:tools:whitelist"; + +/** + * 番剧列表缓存 + * @type {string} + */ +export const REDIS_YUNZAI_ANIMELIST = "Yz:rconsole:tools:anime"; export const TWITTER_BEARER_TOKEN = ""; diff --git a/utils/redis-util.js b/utils/redis-util.js index 34427a5..ba16904 100644 --- a/utils/redis-util.js +++ b/utils/redis-util.js @@ -39,4 +39,20 @@ export async function redisExistAndGetKey(key) { return redisGetKey(key); } return null; +} + +/** + * 在某个 key 的末尾插入一个对象 + * @param key + * @param obj + * @returns {Promise} + */ +export async function redisExistAndInsertObject(key, obj) { + let objs = await redisExistAndGetKey(key); + if (objs) { + objs = {...objs, ...obj}; + await redis.set(key, JSON.stringify(objs)); + } else { + await redis.set(key, JSON.stringify(obj)); + } } \ No newline at end of file