UNPKG

koishi-plugin-booru

Version:
68 lines (67 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const koishi_1 = require("koishi"); const source_1 = require("../../source"); class GelbooruImageSource extends source_1.ImageSource { constructor() { super(...arguments); this.languages = ['en']; this.source = 'gelbooru'; } get keyPair() { if (!this.config.keyPairs.length) return; return this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)]; } async get(query) { // 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 = (0, koishi_1.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(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), }; }); } } (function (GelbooruImageSource) { GelbooruImageSource.Config = koishi_1.Schema.intersect([ source_1.ImageSource.createSchema({ label: 'gelbooru' }), koishi_1.Schema.object({ endpoint: koishi_1.Schema.string().description('Gelbooru 的 URL。').default('https://gelbooru.com/index.php'), keyPairs: koishi_1.Schema.array(koishi_1.Schema.string().required().role('secret')) .description('Gelbooru 的登录凭据。') .default([]), }).description('搜索设置'), ]); })(GelbooruImageSource || (GelbooruImageSource = {})); exports.default = GelbooruImageSource;