koishi-plugin-booru
Version:
Image service for Koishi
111 lines (110 loc) • 4.73 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const koishi_1 = require("koishi");
const source_1 = require("../../source");
const consts = __importStar(require("./constants"));
class SankakuComplexImageSource extends source_1.ImageSource {
constructor(ctx, config) {
super(ctx, config);
this.languages = ['en'];
this.source = 'sankaku';
this.http = this.http.extend({
headers: {
'User-Agent': config.userAgent,
},
});
}
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://chan.sankakucomplex.com/cn/help/api (Link not available)
const params = {
tags: [...query.tags, 'order:random'].join('+'),
limit: `${query.count}`,
};
const keyPair = this.keyPair;
if (!keyPair.accessToken) {
await this._login(keyPair);
}
const data = await this.http.get(consts.POSTS_URL, {
params,
headers: keyPair.accessToken ? { Authentication: `${keyPair.tokenType} ${keyPair.accessToken}` } : {},
});
console.log(data);
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.name.replace(/ /g, ', ').replace(/_/g, ' '),
tags: post.tags.map((t) => t.name.replace(/_/g, ' ')),
nsfw: ['e', 'q'].includes(post.rating),
};
});
}
async _login(keyPair) {
if (!keyPair.accessToken) {
const data = await this.http.post(consts.LOGIN_URL, {
login: keyPair.login,
password: keyPair.password,
});
console.log(data);
if (data.access_token) {
keyPair.accessToken = data.access_token;
keyPair.tokenType = data.token_type;
this.ctx.setTimeout(() => {
this.ctx.scope.update(this.config);
}, 0);
}
}
return keyPair;
}
}
(function (SankakuComplexImageSource) {
SankakuComplexImageSource.Config = koishi_1.Schema.intersect([
source_1.ImageSource.createSchema({ label: 'sankaku' }),
koishi_1.Schema.object({
keyPairs: koishi_1.Schema.array(koishi_1.Schema.object({
login: koishi_1.Schema.string().required().description('SankakuComplex 用户名'),
password: koishi_1.Schema.string().required().role('secret').description('SankakuComplex 密码'),
tokenType: koishi_1.Schema.string().hidden().default('Bearer').description('SankakuComplex 访问令牌类型'),
accessToken: koishi_1.Schema.string().hidden().description('SankakuComplex 访问令牌'),
}).description('SankakuComplex 的登录凭证')),
userAgent: koishi_1.Schema.string().description('设置请求的 User Agent。').default(
// eslint-disable-next-line max-len
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'),
}).description('搜索设置'),
]);
})(SankakuComplexImageSource || (SankakuComplexImageSource = {}));
exports.default = SankakuComplexImageSource;