diff --git a/apps/set.js b/apps/set.js index aac442c..235745e 100644 --- a/apps/set.js +++ b/apps/set.js @@ -4,7 +4,7 @@ import lodash from "lodash"; import Common from "../components/Common.js"; import { Config } from '../components/index.js' - +let rediskey = `yenai:proxy` export class NewConfig extends plugin { constructor() { super({ @@ -32,10 +32,21 @@ export class NewConfig extends plugin { reg: '^#?椰奶(启用|禁用)全部通知$', fnc: 'SetAll' }, + { + reg: '^#椰奶更换代理(1|2)$', + fnc: 'proxy' + } ] }) } + //初始化 + async init() { + if (!await redis.get(rediskey)) { + await redis.set(rediskey, "i.pixiv.re") + } + } + // 更改配置 async Config_manage(e) { if (!e.isMaster) return @@ -72,7 +83,7 @@ export class NewConfig extends plugin { return true; } - + //修改设置 async SetAll(e) { if (!e.isMaster) return let yes = false; @@ -95,6 +106,7 @@ export class NewConfig extends plugin { this.yenaiset(e) return true; } + //渲染发送图片 async yenaiset(e) { if (!e.isMaster) return @@ -144,7 +156,26 @@ export class NewConfig extends plugin { scale: 2.0 }); } + + //更换代理 + async proxy(e) { + if (/1/.test(e.msg)) { + await redis.set(rediskey, "i.pixiv.re") + .then(() => e.reply("已经切换代理为1")) + .catch(err => console.log(err)) + } else if (/2/.test(e.msg)) { + await redis.set(rediskey, "proxy.pixivel.moe") + .then(() => e.reply("已经切换代理为2")) + .catch(err => console.log(err)) + } else { + await redis.set(rediskey, "i.pixiv.cat") + .then(() => e.reply("已经切换代理为3")) + .catch(err => console.log(err)) + } + } } + +//随机底图 const rodom = async function () { var image = fs.readdirSync(`./plugins/yenai-plugin/resources/admin/imgs/bg`); var list_img = []; diff --git a/apps/setu.js b/apps/setu.js index d861924..c6d0bcf 100644 --- a/apps/setu.js +++ b/apps/setu.js @@ -325,7 +325,8 @@ export class sese extends plugin { } else if (num > 6) { size = "small" } - let url = `${api}?r18=${r18}&num=${num}${tag}&proxy=proxy.pixivel.moe&size=${size}`; + let url = `${api}?r18=${r18}&num=${num}${tag}&proxy=${await redis.get(`yenai:proxy`)}&size=${size}`; + console.log(url); let result = await fetch(url).then(res => res.json()).catch(err => console.log(err)) if (!result) return false; return result.data @@ -340,8 +341,6 @@ export class sese extends plugin { if (fs.existsSync(this.path)) { cfgs = await Cfg.getread(this.path) } - //默认撤回间隔 - let time = def.recall //默认CD let cd = def.cd //获取当前时间 diff --git a/components/Config.js b/components/Config.js index 89b5c64..3a72ef8 100644 --- a/components/Config.js +++ b/components/Config.js @@ -1,6 +1,7 @@ import YAML from 'yaml' import chokidar from 'chokidar' import fs from 'node:fs' +import YamlReader from '../model/YamlReader.js' const Path = process.cwd(); const Plugin_Name = 'yenai-plugin' @@ -108,16 +109,8 @@ class Config { */ modify(name, key, value) { let path = `${Plugin_Path}/config/config/${name}.yaml` - let config = this.Notice - config[key] = value - try { - fs.writeFileSync(path, YAML.stringify(config), 'utf8') - delete this.config[`config.${name}`] - return true; - } catch (e) { - console.log(e); - return false; - } + new YamlReader(path).set(key, value) + delete this.config[`config.${name}`] } } diff --git a/model/Pixiv.js b/model/Pixiv.js index b3cdc95..15bee34 100644 --- a/model/Pixiv.js +++ b/model/Pixiv.js @@ -28,10 +28,11 @@ export default class Pixiv { let caption = res.caption.replace(/<.*>/g, "").trim() let { id: pid, title, meta_single_page, meta_pages, user, } = res let url = [] + let proxy = await redis.get(`yenai:proxy`) if (!lodash.isEmpty(meta_single_page)) { - url.push(meta_single_page.original_image_url.replace("i.pximg.net", "proxy.pixivel.moe")) + url.push(meta_single_page.original_image_url.replace("i.pximg.net", proxy)) } else { - url = meta_pages.map(item => item.image_urls.original.replace("i.pximg.net", "proxy.pixivel.moe")); + url = meta_pages.map(item => item.image_urls.original.replace("i.pximg.net", proxy)); } return { pid, @@ -83,8 +84,8 @@ export default class Pixiv { if (title == "wx" && uresname == "wx") continue let tags = i.tags ? lodash.truncate(i.tags.map((item) => item.name)) : "" - - let url = i.imageUrls[0].large.replace("i.pximg.net", "proxy.pixivel.moe") + let proxy = await redis.get(`yenai:proxy`) + let url = i.imageUrls[0].large.replace("i.pximg.net", proxy) list.push([ `标题:${title}\n`, `插画ID:${pid}\n`, @@ -157,9 +158,10 @@ export default class Pixiv { return false } let list = [] + let proxy = await redis.get(`yenai:proxy`) for (let i of res.trend_tags) { let { tag, translated_name } = i - let url = i.illust.image_urls.large.replace("i.pximg.net", "proxy.pixivel.moe") + let url = i.illust.image_urls.large.replace("i.pximg.net", proxy) list.push( [ `Tag:${tag}\n`, diff --git a/model/YamlReader.js b/model/YamlReader.js new file mode 100644 index 0000000..cd604fe --- /dev/null +++ b/model/YamlReader.js @@ -0,0 +1,110 @@ +import fs from 'fs' +import YAML from 'yaml' +import lodash from 'lodash' +import chokidar from 'chokidar' +// import Constant from '../server/constant/Constant.js' + +export default class YamlReader { + /** + * 读写yaml文件 + * + * @param yamlPath yaml文件绝对路径 + * @param isWatch 是否监听文件变化 + */ + constructor(yamlPath, isWatch = false) { + this.yamlPath = yamlPath + this.isWatch = isWatch + this.initYaml() + } + + initYaml() { + try { + // parseDocument 将会保留注释 + this.document = YAML.parseDocument(fs.readFileSync(this.yamlPath, 'utf8')) + } catch (error) { + throw error + } + if (this.isWatch && !this.watcher) { + this.watcher = chokidar.watch(this.yamlPath).on('change', () => { + if (this.isSave) { + this.isSave = false + return + } + this.initYaml() + }) + } + } + + /**返回读取的对象 */ + get jsonData() { + if (!this.document) { + return null + } + return this.document.toJSON() + } + + /*检查集合是否包含key的值*/ + has(keyPath) { + return this.document.hasIn(keyPath.split('.')) + } + /*返回key的值*/ + get(keyPath) { + return lodash.get(this.jsonData, keyPath) + } + /*修改某个key的值*/ + set(keyPath, value) { + this.document.setIn(keyPath.split('.'), value) + this.save() + } + /*删除key*/ + delete(keyPath) { + this.document.deleteIn(keyPath.split('.')) + this.save() + } + + /** + * 设置 document 的数据(递归式) + * @param data 要写入的数据 + */ + setData(data) { + this.setDataRecursion(data, []) + this.save() + } + + setDataRecursion(data, parentKeys) { + if (Array.isArray(data)) { + this.document.setIn(this.mapParentKeys(parentKeys), data) + } else if (typeof data === 'object' && data !== null) { + for (const [key, value] of Object.entries(data)) { + this.setDataRecursion(value, parentKeys.concat([key])) + } + } else { + parentKeys = this.mapParentKeys(parentKeys) + this.document.setIn(parentKeys, data) + } + } + + // 将数字key转为number类型,防止出现引号 + mapParentKeys(parentKeys) { + return parentKeys.map((k) => { + if (k.startsWith(Constant.CONFIG_INTEGER_KEY)) { + return Number.parseInt(k.replace(Constant.CONFIG_INTEGER_KEY, '')) + } + return k + }) + } + + // 彻底删除某个key + deleteKey(keyPath) { + let keys = keyPath.split('.') + keys = this.mapParentKeys(keys) + this.document.deleteIn(keys) + this.save() + } + + save() { + this.isSave = true + let yaml = this.document.toString() + fs.writeFileSync(this.yamlPath, yaml, 'utf8') + } +}