From cfb697b565fcec0f84e9a73ee218add0b9f41d30 Mon Sep 17 00:00:00 2001 From: Runtus <893119806@qq.com> Date: Thu, 9 Mar 2023 22:03:56 +0800 Subject: [PATCH] init --- .editorconfig | 9 +++++ .gitattributes | 7 ++++ .gitignore | 17 ++++++++ client/index.ts | 11 ++++++ client/page.vue | 4 ++ client/tsconfig.json | 14 +++++++ package.json | 26 ++++++++++++ readme.md | 5 +++ src/components/buffers.ts | 16 ++++++++ src/components/index.ts | 1 + src/index.ts | 77 ++++++++++++++++++++++++++++++++++++ src/middleware/illustPush.ts | 37 +++++++++++++++++ src/middleware/index.ts | 4 ++ src/middleware/search.ts | 32 +++++++++++++++ tsconfig.json | 19 +++++++++ 15 files changed, 279 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 client/index.ts create mode 100644 client/page.vue create mode 100644 client/tsconfig.json create mode 100644 package.json create mode 100644 readme.md create mode 100644 src/components/buffers.ts create mode 100644 src/components/index.ts create mode 100644 src/index.ts create mode 100644 src/middleware/illustPush.ts create mode 100644 src/middleware/index.ts create mode 100644 src/middleware/search.ts create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8f02e58 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +insert_final_newline = true +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f6b1d23 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +* text eol=lf + +*.png -text +*.jpg -text +*.ico -text +*.gif -text +*.webp -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de8b44d --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +lib +dist + +node_modules +npm-debug.log +yarn-debug.log +yarn-error.log +tsconfig.tsbuildinfo + +.eslintcache +.DS_Store +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln diff --git a/client/index.ts b/client/index.ts new file mode 100644 index 0000000..a1afc04 --- /dev/null +++ b/client/index.ts @@ -0,0 +1,11 @@ +import { Context } from '@koishijs/client' +import Page from './page.vue' + + +export default (ctx: Context) => { + ctx.page({ + name: '扩展页面', + path: '/custom-page', + component: Page, + }) +} diff --git a/client/page.vue b/client/page.vue new file mode 100644 index 0000000..ac7f0e7 --- /dev/null +++ b/client/page.vue @@ -0,0 +1,4 @@ + + diff --git a/client/tsconfig.json b/client/tsconfig.json new file mode 100644 index 0000000..02b01b5 --- /dev/null +++ b/client/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "rootDir": ".", + "module": "esnext", + "moduleResolution": "node", + "jsx": "preserve", + "types": [ + "@koishijs/client/global" + ] + }, + "include": [ + "." + ] +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..da2f624 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "koishi-plugin-rpixiv", + "description": "search pixiv", + "version": "0.0.1", + "main": "lib/index.js", + "typings": "lib/index.d.ts", + "files": [ + "lib", + "dist" + ], + "license": "MIT", + "scripts": {}, + "keywords": [ + "chatbot", + "koishi", + "plugin" + ], + "peerDependencies": { + "koishi": "^4.11.6" + }, + "dependencies": { + "axios": "0.26.1", + "dotenv": "^16.0.3", + "runtu-pixiv-sdk": "^1.0.18" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..35a0d16 --- /dev/null +++ b/readme.md @@ -0,0 +1,5 @@ +# koishi-plugin-rpixiv + +[![npm](https://img.shields.io/npm/v/koishi-plugin-rpixiv?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-rpixiv) + +search pixiv diff --git a/src/components/buffers.ts b/src/components/buffers.ts new file mode 100644 index 0000000..ce20d78 --- /dev/null +++ b/src/components/buffers.ts @@ -0,0 +1,16 @@ +import { RPixiv, WebPixivType } from "runtu-pixiv-sdk"; +import { h } from "koishi"; + +export const requestBuffers = (urls: WebPixivType["illusts"], r: RPixiv) => { + const promise = []; + urls.slice(0, 10).forEach((item) => { + promise.push(r.getPixivStream(item.image_urls.medium, "arraybuffer")); + }); + return Promise.all(promise).then((res) => { + return h( + "p", + {}, + res.map((item) => h.image(item, "image/png")) + ); + }); +}; diff --git a/src/components/index.ts b/src/components/index.ts new file mode 100644 index 0000000..c94af5f --- /dev/null +++ b/src/components/index.ts @@ -0,0 +1 @@ +export * from './buffers' \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..8e986ea --- /dev/null +++ b/src/index.ts @@ -0,0 +1,77 @@ +import { Context, Schema } from 'koishi' +import { RPixiv } from 'runtu-pixiv-sdk' +import { rPixivIllustsSearch, illustsPush } from './middleware/index' + + +export const name = 'rpixiv' + + +export interface Config { + refresh: string, + keywords: { + start: string, + day: string, + week: string, + month: string, + searchIllusts: string, + searchAuthor: string, + }, + proxy: { + host: string, + port: number + } +} + +export const Config: Schema = Schema.object({ + refresh: Schema.string().required().default("").description("Pixiv的RefreshToken"), + keywords: Schema.object({ + start: Schema.string().default("rpixiv酱").description("机器人的触发词。"), + day: Schema.string().default("查询每日推荐榜").description("每日推荐榜的触发语,紧跟着start字段触发词"), + week: Schema.string().default("查询每周推荐榜").description("每周推荐榜的触发语,紧跟着start字段触发词"), + month: Schema.string().default("查询每月推荐榜").description("每周推荐榜的触发语,紧跟着start字段触发词"), + searchIllusts: Schema.string().default("查询作品").description("每周推荐榜的触发语,紧跟着start字段触发词"), + searchAuthor: Schema.string().default("查询作者").description("每周推荐榜的触发语,紧跟着start字段触发词"), + }), + proxy: Schema.object({ + host: Schema.string().default("").description("代理的host"), + port: Schema.number().default(0).description("代理端口") + }) +}) + + + + +export function apply(ctx: Context, config: Config) { + + const keywords = { + ...config.keywords + } + + for (const [key, word] of Object.entries(config.keywords)) { + if (key !== config.keywords.start) { + keywords[key] = config.keywords.start + word + } + } + + const rPixiv = new RPixiv({ + host: "127.0.0.1", + port: 7890 + }) + + // 环境变量设置 + process.env.REFEESH_TOKEN = config.refresh + // token初始化 + rPixiv.token() + + // 触发 + console.log(keywords) + ctx.middleware(illustsPush(keywords.day, 'day', rPixiv)) + ctx.middleware(illustsPush(keywords.week, 'week', rPixiv)) + ctx.middleware(illustsPush(keywords.month, 'month', rPixiv)) + ctx.middleware(rPixivIllustsSearch(keywords.searchIllusts, rPixiv)) + + + +} + + diff --git a/src/middleware/illustPush.ts b/src/middleware/illustPush.ts new file mode 100644 index 0000000..bce9379 --- /dev/null +++ b/src/middleware/illustPush.ts @@ -0,0 +1,37 @@ +import { Middleware } from 'koishi' +import { RPixiv } from 'runtu-pixiv-sdk' +import { requestBuffers } from '../components' + + +export const illustsPush: (trigger: string, type: string, r: RPixiv) => Middleware = (trigger, type, rPixiv) => { + const triggerC = trigger + console.log("sdsdsds",trigger) + let requestFn: RPixiv['getMonthRanks'] | RPixiv['getDayRanks'] | RPixiv['getWeekRanks'] = undefined + if (type === 'day') { + requestFn = rPixiv.getDayRanks + } else if (type === 'week') { + requestFn = rPixiv.getWeekRanks + } else if (type === 'month') { + requestFn = rPixiv.getMonthRanks + } else { + requestFn = null + } + return (session, next) => { + if (session.content.startsWith(triggerC)) { + console.log('触发了') + requestFn.call(rPixiv, "").then(res => { + if (res.code === 200) { + return requestBuffers(res.data.illusts, rPixiv) + } else { + session.send("网络出现错误,请联系管理员") + } + }).then(info => { + session.send(info) + }).catch(() => { + session.send("出现了渣不多得勒的错误") + }) + } else { + next() + } + } +} \ No newline at end of file diff --git a/src/middleware/index.ts b/src/middleware/index.ts new file mode 100644 index 0000000..96162e8 --- /dev/null +++ b/src/middleware/index.ts @@ -0,0 +1,4 @@ +export * from './illustPush' +export * from './search' + + diff --git a/src/middleware/search.ts b/src/middleware/search.ts new file mode 100644 index 0000000..4186de8 --- /dev/null +++ b/src/middleware/search.ts @@ -0,0 +1,32 @@ +import { Middleware, h } from 'koishi' +import { RPixiv } from 'runtu-pixiv-sdk' +import { requestBuffers } from '../components' + + +export const rPixivIllustsSearch: (trigger: string, r: RPixiv) => Middleware = (trigger, rpixiv) => { + return (session, next) => { + // 指定前缀 + if (session.content.startsWith(trigger)) { + const words = session.content.slice(trigger.length) + rpixiv.searchIllusts(words) + .then((res) => { + if (res.code === 400) { + session.send("网络出现问题,请联系管理员") + } else { + return requestBuffers(res.data.illusts, rpixiv) + } + }) + .then(info => { + session.send(info) + }) + .catch((err) => { + console.log(err) + session.send("出现了渣不多得勒的错误,请联系管理员") + }) + + } else { + next() + } + } +} + diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a62f0d9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "rootDir": "src", + "outDir": "lib", + "target": "es2020", + "module": "commonjs", + "declaration": true, + "composite": true, + "incremental": true, + "skipLibCheck": true, + "esModuleInterop": true, + "moduleResolution": "node", + "jsx": "react-jsx", + "jsxImportSource": "@satorijs/element", + }, + "include": [ + "src", + ], +}