koishi-plugin-booru
Version:
Image service for Koishi
60 lines (59 loc) • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const koishi_1 = require("koishi");
const source_1 = require("../../source");
class SafebooruImageSource extends source_1.ImageSource {
constructor() {
super(...arguments);
this.languages = ['en'];
this.source = 'safebooru';
}
async get(query) {
// API docs: https://safebooru.org/index.php?page=help&topic=dapi
const params = {
// TODO random 无效
tags: query.tags.join('+') + '+sort:random',
page: 'dapi',
s: 'post',
q: 'index',
json: 1,
limit: query.count,
};
const url = (0, koishi_1.trimSlash)(this.config.endpoint) +
'?' +
Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join('&');
const data = await this.http.get(url);
if (!Array.isArray(data)) {
return;
}
return data.map((post) => {
return {
// Safebooru didn't straightly provide image urls, so we should construct them manually.
// `sample` url only exists when the image is too large, in that case, `post.sample`
// would be `true`, and then we could construct the sample url.
urls: {
original: `https://safebooru.org/images/${post.directory}/${post.image}?${post.id}`,
large: post.sample
? `https://safebooru.org/samples/${post.directory}/sample_${post.image}?${post.id}`
: undefined,
thumbnail: `https://safebooru.org/thumbnails/${post.directory}/thumbnail_${post.image}?${post.id}`,
},
// pageUrl: post.source,
author: post.owner.replace(/ /g, ', ').replace(/_/g, ' '),
tags: post.tags.split(' ').map((t) => t.replace(/_/g, ' ')),
nsfw: !['safe', 'general'].includes(post.rating),
};
});
}
}
(function (SafebooruImageSource) {
SafebooruImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'safebooru' }),
koishi_1.Schema.object({
endpoint: koishi_1.Schema.string().description('Safebooru 的 URL。').default('https://safebooru.org/index.php'),
}).description('搜索设置'),
]);
})(SafebooruImageSource || (SafebooruImageSource = {}));
exports.default = SafebooruImageSource;