koishi-plugin-booru
Version:
Image service for Koishi
82 lines (81 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_crypto_1 = require("node:crypto");
const koishi_1 = require("koishi");
const source_1 = require("../../source");
/**
* Konachan requires a password hash for authentication.
*
* @see https://konachan.net/help/api
*/
function hashPassword(password) {
const salted = `So-I-Heard-You-Like-Mupkids-?--${password}--`;
// do a SHA1 hash of the salted password
const hash = (0, node_crypto_1.createHash)('sha1');
hash.update(salted);
return hash.digest('hex');
}
class KonachanImageSource extends source_1.ImageSource {
constructor() {
super(...arguments);
this.languages = ['en'];
this.source = 'konachan';
}
get keyPair() {
if (!this.config.keyPairs.length)
return;
const key = this.config.keyPairs[Math.floor(Math.random() * this.config.keyPairs.length)];
return {
login: key.login,
password_hash: hashPassword(key.password),
};
}
async get(query) {
// API docs: https://konachan.net/help/api and https://konachan.com/help/api
const params = {
tags: query.tags.join('+') + '+order:random',
limit: `${query.count}`,
};
const url = (0, koishi_1.trimSlash)(this.config.endpoint) + '/post.json';
const keyPair = this.keyPair;
if (keyPair) {
params['login'] = keyPair.login;
params['password_hash'] = keyPair.password_hash;
}
const data = await this.http.get(url, { params: new URLSearchParams(params) });
if (!Array.isArray(data)) {
return;
}
return data.map((post) => {
return {
urls: {
original: post.file_url,
medium: post.sample_url,
thumbnail: post.preview_url,
},
pageUrl: post.source,
author: post.author.replace(/ /g, ', ').replace(/_/g, ' '),
tags: post.tags.split(' ').map((t) => t.replace(/_/g, ' ')),
nsfw: ['e', 'q'].includes(post.rating),
};
});
}
}
(function (KonachanImageSource) {
KonachanImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'konachan' }),
koishi_1.Schema.object({
endpoint: koishi_1.Schema.union([
koishi_1.Schema.const('https://konachan.com/').description('Konachan.com (NSFW)'),
koishi_1.Schema.const('https://konachan.net/').description('Konachan.net (SFW)'),
])
.description('Konachan 的 URL。')
.default('https://konachan.com/'),
keyPairs: koishi_1.Schema.array(koishi_1.Schema.object({
login: koishi_1.Schema.string().required().description('用户名'),
password: koishi_1.Schema.string().required().role('secret').description('密码'),
})).description('Konachan 的登录凭据。'),
}).description('搜索设置'),
]);
})(KonachanImageSource || (KonachanImageSource = {}));
exports.default = KonachanImageSource;