Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-fish committed Mar 4, 2024
2 parents 9fa4693 + 97dca72 commit bb93226
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions src/plugins/pixiv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
* @desc pixiv插画查看工具
* @authority 1
*/
import { Context, Time, segment } from 'koishi'
import { Context, Time, h } from 'koishi'

import BasePlugin from '~/_boilerplate'

import { BulkMessageBuilder } from '$utils/BulkMessageBuilder'
import fexios from 'fexios'

// const API_BASE = process.env.API_PIXIV_BASE
import { Fexios } from 'fexios'

const defaultConfigs = {
baseURL: 'https://www.pixiv.net',
pximgURL: '',
}

export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
constructor(
public ctx: Context,
initOptions?: Partial<typeof defaultConfigs>
) {
readonly request: Fexios

constructor(ctx: Context, initOptions?: Partial<typeof defaultConfigs>) {
super(
ctx,
{
Expand All @@ -32,14 +29,21 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
'pixiv'
)

const { baseURL } = this.options
const ajax = fexios.create({
const { baseURL = defaultConfigs.baseURL } = this.options
this.request = new Fexios({
baseURL,
headers: {
referer: 'https://www.pixiv.net',
},
})

this.#setupCommands()
this.#setupMiddlewares()
}

#setupCommands() {
const ctx = this.ctx
const req = this.request
ctx
.command('pixiv [id:posint]', 'pixiv.net 相关功能')
.action(({ session, name }, id) => {
Expand Down Expand Up @@ -68,13 +72,13 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
let info, pages
try {
;[{ data: info }, { data: pages }] = await Promise.all([
ajax.get(`/ajax/illust/${id}?full=1`),
ajax.get(`/ajax/illust/${id}/pages`),
req.get(`/ajax/illust/${id}?full=1`),
req.get(`/ajax/illust/${id}/pages`),
])
} catch (error) {
this.logger.warn(error)
return [
segment.quote(session.messageId as string),
h.quote(session.messageId as string),
error?.response?.data?.message || error.message || '出现未知问题',
].join('')
}
Expand All @@ -93,15 +97,15 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
const builder = new BulkMessageBuilder(session)
builder.prependOriginal()
const lines = [
segment.image(`${baseURL}${imageUrl}`),
h.image(this.makePximgURL(imageUrl)),
totalImages ? `第 ${selectedPage} 张,共 ${totalImages} 张` : null,
`${info.title}`,
desc.length > 500 ? desc.substring(0, 500) + '...' : desc,
`作者: ${info.userName} (ID: ${info.userId})`,
`👍${info.likeCount} ❤️${info.bookmarkCount} 👀${info.viewCount}`,
`发布时间: ${new Date(info.createDate).toLocaleString()}`,
allTags.length ? allTags.join(' ') : null,
`${baseURL}/i/${info.id}`,
new URL(`/i/${id}`, this.options.baseURL).href,
].map((i) =>
typeof i === 'string' ? i.trim().replace(/\n+/g, '\n') : i
)
Expand All @@ -119,13 +123,13 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
return session.execute({ name: cmdName, options: { help: true } })
}

let data
let data: any
try {
data = (await fexios.get(`${baseURL}/ajax/user/${id}?full=1`)).data
data = (await req.get(`/ajax/user/${id}?full=1`)).data
} catch (error) {
this.logger.warn(error)
return [
segment.quote(session.messageId as string),
h.quote(session.messageId as string),
error.message || '出现未知问题',
].join('')
}
Expand All @@ -135,7 +139,7 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
const builder = new BulkMessageBuilder(session)
builder.prependOriginal()
const lines = [
segment.image(`${baseURL}${imageBig}`),
h.image(this.makePximgURL(imageBig)),
`${name} (${userId})`,
comment,
].map((i) =>
Expand All @@ -145,8 +149,10 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {

return builder.all()
})
}

// 快捷方式
#setupMiddlewares() {
const ctx = this.ctx
ctx.middleware(async (session, next) => {
await next()
const reg =
Expand All @@ -157,4 +163,14 @@ export default class PluginPixiv extends BasePlugin<typeof defaultConfigs> {
}
})
}

makePximgURL(url: string) {
if (url.startsWith('http')) {
if (!this.options.pximgURL) {
return url
}
url = new URL(url).pathname
}
return new URL(url, this.options.pximgURL || this.options.baseURL).href
}
}

0 comments on commit bb93226

Please sign in to comment.