Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Runtus committed Mar 9, 2023
0 parents commit cfb697b
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text eol=lf

*.png -text
*.jpg -text
*.ico -text
*.gif -text
*.webp -text
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions client/index.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
4 changes: 4 additions & 0 deletions client/page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<k-layout>扩展内容</k-layout>
</template>

14 changes: 14 additions & 0 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"rootDir": ".",
"module": "esnext",
"moduleResolution": "node",
"jsx": "preserve",
"types": [
"@koishijs/client/global"
]
},
"include": [
"."
]
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/components/buffers.ts
Original file line number Diff line number Diff line change
@@ -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"))
);
});
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './buffers'
77 changes: 77 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<Config> = 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))



}


37 changes: 37 additions & 0 deletions src/middleware/illustPush.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
4 changes: 4 additions & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './illustPush'
export * from './search'


32 changes: 32 additions & 0 deletions src/middleware/search.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
}

19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
],
}

0 comments on commit cfb697b

Please sign in to comment.