koishi-plugin-booru
Version:
Image service for Koishi
72 lines (71 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const koishi_1 = require("koishi");
const source_1 = require("../../source");
class e621ImageSource extends source_1.ImageSource {
constructor(ctx, config) {
super(ctx, config);
this.languages = ['en'];
this.source = 'e621';
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) {
if (!query.tags.find((t) => t.startsWith('order:')))
query.tags.push('order:random');
const keyPair = this.keyPair;
const data = await this.http.get((0, koishi_1.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: (0, koishi_1.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,
};
});
}
}
(function (e621ImageSource) {
e621ImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'e621' }),
koishi_1.Schema.object({
endpoint: koishi_1.Schema.string().description('e621/e926 的 URL。').default('https://e621.net/'),
keyPairs: koishi_1.Schema.array(koishi_1.Schema.object({
login: koishi_1.Schema.string().required().description('e621/e926 的用户名。'),
apiKey: koishi_1.Schema.string().required().role('secret').description('e621/e926 的 API Key。'),
}))
.default([])
.description('e621/e926 的登录凭据。'),
userAgent: koishi_1.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('搜索设置'),
]);
})(e621ImageSource || (e621ImageSource = {}));
exports.default = e621ImageSource;