koishi-plugin-booru
Version:
Image service for Koishi
77 lines (76 loc) • 2.9 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");
/**
* Yande.re requires a password hash for authentication.
*
* @see https://yande.re/help/api
*/
function hashPassword(password) {
const salted = `choujin-steiner--${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 YandeImageSource extends source_1.ImageSource {
constructor() {
super(...arguments);
this.languages = ['en'];
this.source = 'yande';
}
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://yande.re/help/api
const params = {
tags: [...query.tags, 'order:random'].join('+'),
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 });
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 (YandeImageSource) {
YandeImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'yande' }),
koishi_1.Schema.object({
endpoint: koishi_1.Schema.string().description('Yande.re 的 URL。').default('https://yande.re'),
keyPairs: koishi_1.Schema.array(koishi_1.Schema.object({
login: koishi_1.Schema.string().required().description('Yande.re 的用户名。'),
password: koishi_1.Schema.string().required().role('secret').description('Yande.re 的密码。'),
})).description('Yande.re 的登录凭据。'),
}).description('搜索设置'),
]);
})(YandeImageSource || (YandeImageSource = {}));
exports.default = YandeImageSource;