koishi-plugin-booru
Version:
Image service for Koishi
61 lines (60 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const koishi_1 = require("koishi");
const source_1 = require("../../source");
class DanbooruImageSource extends source_1.ImageSource {
constructor() {
super(...arguments);
this.languages = ['en'];
this.source = 'danbooru';
}
get keyPair() {
if (!this.config.keyPairs.length)
return;
return this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)];
}
async get(query) {
const keyPair = this.keyPair;
const data = await this.http.get((0, koishi_1.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',
};
});
}
}
(function (DanbooruImageSource) {
DanbooruImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'danbooru' }),
koishi_1.Schema.object({
endpoint: koishi_1.Schema.string().description('Danbooru 的 URL。').default('https://danbooru.donmai.us/'),
/**
* @see https://danbooru.donmai.us/wiki_pages/help%3Aapi
*/
keyPairs: koishi_1.Schema.array(koishi_1.Schema.object({
login: koishi_1.Schema.string().required().description('用户名。'),
apiKey: koishi_1.Schema.string().required().role('secret').description('API 密钥。'),
})).description('API 密钥对。[点击前往获取及设置教程](https://booru.koishi.chat/zh-CN/plugins/danbooru.html#获取与设置登录凭据)'),
}).description('搜索设置'),
]);
})(DanbooruImageSource || (DanbooruImageSource = {}));
exports.default = DanbooruImageSource;