Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refa: migrate most small booru to subpackages of core #164

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@
"lib",
"dist"
],
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json",
"./danbooru": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/danbooru/index.js"
},
"./e621": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/e621/index.js"
},
"./gelbooru": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/gelbooru/index.js"
},
"./konachan": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/konachan/index.js"
},
"./lolibooru": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/lolibooru/index.js"
},
"./safebooru": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/safebooru/index.js"
},
"./sankaku": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/sankaku/index.js"
},
"./yande": {
"types": "./lib/sources/danbooru/index.d.ts",
"default": "./lib/sources/yande/index.js"
}
},
"author": "Shigma <[email protected]>",
"license": "MIT",
"repository": {
Expand Down
77 changes: 77 additions & 0 deletions packages/core/src/sources/danbooru/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Context, Schema, trimSlash } from 'koishi'

import { ImageSource } from '../../source'

import { Danbooru } from './types'

class DanbooruImageSource extends ImageSource<DanbooruImageSource.Config> {
languages = ['en']
source = 'danbooru'

constructor(ctx: Context, config: DanbooruImageSource.Config) {

Check warning on line 11 in packages/core/src/sources/danbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/danbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/danbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/danbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor
super(ctx, config)
}

get keyPair() {
if (!this.config.keyPairs.length) return
return this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)]
}

async get(query: ImageSource.Query): Promise<ImageSource.Result[]> {
const keyPair = this.keyPair
const data = await this.http.get<Danbooru.Post[]>(trimSlash(this.config.endpoint) + '/posts.json', {
params: {
tags: query.tags.join(' '),
random: true,
limit: query.count,
...(keyPair ? { login: keyPair.login, api_key: keyPair.apiKey } : {}),
},
})

if (!Array.isArray(data)) {
return
}

return data.map((post) => {
return {
// Size: file_url > large_file_url > preview_file_url
urls: {
original: post.file_url,
large: post.large_file_url,
thumbnail: post.preview_file_url,
},
pageUrl: post.source,
author: post.tag_string_artist.replace(/ /g, ', ').replace(/_/g, ' '),
tags: post.tag_string.split(' ').map((t) => t.replace(/_/g, ' ')),
nsfw: post.rating === 'e' || post.rating === 'q',
}
})
}
}

namespace DanbooruImageSource {
export interface Config extends ImageSource.Config {
endpoint: string
keyPairs: { login: string; apiKey: string }[]
}

export const Config: Schema<Config> = Schema.intersect([
ImageSource.createSchema({ label: 'danbooru' }),
Schema.object({
endpoint: Schema.string().description('Danbooru 的 URL。').default('https://danbooru.donmai.us/'),
/**
* @see https://danbooru.donmai.us/wiki_pages/help%3Aapi
*/
keyPairs: Schema.array(
Schema.object({
login: Schema.string().required().description('用户名。'),
apiKey: Schema.string().required().role('secret').description('API 密钥。'),
}),
).description(
'API 密钥对。[点击前往获取及设置教程](https://booru.koishi.chat/zh-CN/plugins/danbooru.html#获取与设置登录凭据)',
),
}).description('搜索设置'),
])
}

export default DanbooruImageSource
92 changes: 92 additions & 0 deletions packages/core/src/sources/e621/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Context, Quester, Schema, trimSlash } from 'koishi'

import { ImageSource } from '../../source'

import { e621 } from './types'

class e621ImageSource extends ImageSource<e621ImageSource.Config> {
languages = ['en']
source = 'e621'
http: Quester

constructor(ctx: Context, config: e621ImageSource.Config) {
super(ctx, config)
this.http = this.http.extend({
headers: {
'User-Agent': config.userAgent,
},
})
}

get keyPair() {
if (!this.config.keyPairs.length) return
return this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)]
}

async get(query: ImageSource.Query): Promise<ImageSource.Result[]> {
if (!query.tags.find((t) => t.startsWith('order:'))) query.tags.push('order:random')
const keyPair = this.keyPair
const data = await this.http.get<{
posts: e621.Post[]
}>(trimSlash(this.config.endpoint) + '/posts.json', {
params: {
tags: query.tags.join(' '),
limit: query.count,
},
headers: keyPair
? { Authorization: 'Basic ' + Buffer.from(`${keyPair.login}:${keyPair.apiKey}`).toString('base64') }
: {},
})

if (!Array.isArray(data.posts)) {
return
}

return data.posts.map((post) => {
return {
// Size: file > sample > preview
urls: {
original: post.file.url,
medium: post.sample.url,
thumbnail: post.preview.url,
},
pageUrl: trimSlash(this.config.endpoint) + `/post/${post.id}`,
author: post.tags.artist.join(', '),
tags: Object.values(post.tags).flat(),
nsfw: post.rating !== 's',
desc: post.description,
}
})
}
}

namespace e621ImageSource {
export interface Config extends ImageSource.Config {
endpoint: string
keyPairs: { login: string; apiKey: string }[]
userAgent: string
}

export const Config: Schema<Config> = Schema.intersect([
ImageSource.createSchema({ label: 'e621' }),
Schema.object({
endpoint: Schema.string().description('e621/e926 的 URL。').default('https://e621.net/'),
keyPairs: Schema.array(
Schema.object({
login: Schema.string().required().description('e621/e926 的用户名。'),
apiKey: Schema.string().required().role('secret').description('e621/e926 的 API Key。'),
}),
)
.default([])
.description(
'e621/e926 的登录凭据。[点击前往获取及设置教程](https://booru.koishi.chat/zh-CN/plugins/e621.html#configure-credentials)',
),
userAgent: Schema.string().description('设置请求的 User Agent。').default(
// eslint-disable-next-line max-len
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37',
),
}).description('搜索设置'),
])
}

export default e621ImageSource
File renamed without changes.
85 changes: 85 additions & 0 deletions packages/core/src/sources/gelbooru/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Context, Schema, trimSlash } from 'koishi'

import { ImageSource } from '../../source'

import { Gelbooru } from './types'

class GelbooruImageSource extends ImageSource<GelbooruImageSource.Config> {
languages = ['en']
source = 'gelbooru'

constructor(ctx: Context, config: GelbooruImageSource.Config) {

Check warning on line 11 in packages/core/src/sources/gelbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/gelbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/gelbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor

Check warning on line 11 in packages/core/src/sources/gelbooru/index.ts

View workflow job for this annotation

GitHub Actions / build / lint

Useless constructor
super(ctx, config)
}

get keyPair() {
if (!this.config.keyPairs.length) return
return this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)]
}

async get(query: ImageSource.Query): Promise<ImageSource.Result[]> {
// API docs: https://gelbooru.com/index.php?page=help&topic=dapi
const params = {
tags: query.tags.join('+') + '+sort:random',
page: 'dapi',
s: 'post',
q: 'index',
json: 1,
limit: query.count,
}
let url =
trimSlash(this.config.endpoint) +
'?' +
Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join('&')

const keyPair = this.keyPair
if (keyPair) {
// The keyPair from Gelbooru is already url-encoded.
url += keyPair
}

const data = await this.http.get<Gelbooru.Response>(url)

if (!Array.isArray(data.post)) {
return
}

return data.post.map((post) => {
return {
// Size: file_url > sample_url > preview_url
urls: {
original: post.file_url,
medium: post.sample_url,
thumbnail: post.preview_url,
},
pageUrl: post.source,
author: post.owner.replace(/ /g, ', ').replace(/_/g, ' '),
tags: post.tags.split(' ').map((t) => t.replace(/_/g, ' ')),
nsfw: ['explicit', 'questionable'].includes(post.rating),
}
})
}
}

namespace GelbooruImageSource {
export interface Config extends ImageSource.Config {
endpoint: string
keyPairs: string[]
}

export const Config: Schema<Config> = Schema.intersect([
ImageSource.createSchema({ label: 'gelbooru' }),
Schema.object({
endpoint: Schema.string().description('Gelbooru 的 URL。').default('https://gelbooru.com/index.php'),
keyPairs: Schema.array(Schema.string().required().role('secret'))
.description(
'Gelbooru 的登录凭据。[点击前往获取及设置教程](https://booru.koishi.chat/zh-CN/plugins/gelbooru.html#configure-credentials)',
)
.default([]),
}).description('搜索设置'),
])
}

export default GelbooruImageSource
Loading
Loading