koishi-plugin-booth-get 0.0.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +42 -5
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -17,7 +17,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/index.ts
|
|
21
20
|
var src_exports = {};
|
|
22
21
|
__export(src_exports, {
|
|
23
22
|
Config: () => Config,
|
|
@@ -32,14 +31,21 @@ var name = "booth-get";
|
|
|
32
31
|
var inject = ["puppeteer"];
|
|
33
32
|
var Config = import_koishi.Schema.object({
|
|
34
33
|
loadTimeout: import_koishi.Schema.natural().role("ms").description("加载页面的最长时间。当一个页面等待时间超过这个值时,如果此页面主体已经加载完成,则会发送一条提示消息“正在加载中,请稍等片刻”并继续等待加载;否则会直接提示“无法打开页面”并终止加载。").default(import_koishi.Time.second * 5),
|
|
35
|
-
idleTimeout: import_koishi.Schema.natural().role("ms").description("等待页面空闲的最长时间。当一个页面等待时间超过这个值时,将停止进一步的加载并立即发送截图。").default(import_koishi.Time.second * 30)
|
|
34
|
+
idleTimeout: import_koishi.Schema.natural().role("ms").description("等待页面空闲的最长时间。当一个页面等待时间超过这个值时,将停止进一步的加载并立即发送截图。").default(import_koishi.Time.second * 30),
|
|
35
|
+
proxyServer: import_koishi.Schema.string().description("自定义代理服务器地址。例如:http://127.0.0.1:1080").default(""),
|
|
36
36
|
}).description("booth-get");
|
|
37
37
|
function apply(ctx, config) {
|
|
38
38
|
const logger = ctx.logger("screenshot");
|
|
39
39
|
const { defaultViewport } = ctx.puppeteer.config;
|
|
40
|
-
const { loadTimeout, idleTimeout } = config;
|
|
40
|
+
const { loadTimeout, idleTimeout,proxyServer } = config;
|
|
41
41
|
const booth_url = "https://booth.pm/zh-cn/items/";
|
|
42
42
|
const maxSize = 1024 * 1024;
|
|
43
|
+
const launchOptions = { ...ctx.puppeteer.config.launchOptions };
|
|
44
|
+
|
|
45
|
+
if (proxyServer) {
|
|
46
|
+
launchOptions.args = launchOptions.args || [];
|
|
47
|
+
launchOptions.args.push(`--proxy-server=${proxyServer}`);
|
|
48
|
+
}
|
|
43
49
|
ctx.command("摊位 <id>").action(async ({ session }, id) => {
|
|
44
50
|
if (!id)
|
|
45
51
|
return "请输入ID";
|
|
@@ -49,7 +55,7 @@ function apply(ctx, config) {
|
|
|
49
55
|
if (typeof result === "string")
|
|
50
56
|
return result;
|
|
51
57
|
let loaded = false;
|
|
52
|
-
const page = await ctx.puppeteer.page();
|
|
58
|
+
const page = await ctx.puppeteer.page(launchOptions);
|
|
53
59
|
page.on("load", () => loaded = true);
|
|
54
60
|
try {
|
|
55
61
|
await new Promise((resolve, reject) => {
|
|
@@ -141,9 +147,40 @@ ctx.command("摊位名称 <query>").action(async ({ session }, query) => {
|
|
|
141
147
|
await page.close();
|
|
142
148
|
}
|
|
143
149
|
});
|
|
150
|
+
async function captureBoothPage(url) {
|
|
151
|
+
const page = await ctx.puppeteer.page();
|
|
152
|
+
try {
|
|
153
|
+
await page.goto(url, { waitUntil: 'networkidle0' });
|
|
154
|
+
const shooter = await page.$('div.u-pt-600');
|
|
155
|
+
if (!shooter) {
|
|
156
|
+
throw new Error("找不到满足该选择器的元素。");
|
|
157
|
+
}
|
|
158
|
+
const buffer = await shooter.screenshot();
|
|
159
|
+
return import_koishi.h.image(buffer, "image/png");
|
|
160
|
+
} catch (error) {
|
|
161
|
+
throw error;
|
|
162
|
+
} finally {
|
|
163
|
+
await page.close();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
ctx.middleware((session, next) => {
|
|
167
|
+
const message = session.content;
|
|
168
|
+
const boothUrlRegex = /https:\/\/booth.pm\/[\w-]+\/items\/\d+/;
|
|
169
|
+
|
|
170
|
+
if (boothUrlRegex.test(message)) {
|
|
171
|
+
const boothUrl = message.match(boothUrlRegex)[0];
|
|
172
|
+
return captureBoothPage(boothUrl).then(image => {
|
|
173
|
+
return session.send(image);
|
|
174
|
+
}).catch(error => {
|
|
175
|
+
ctx.logger.debug(error);
|
|
176
|
+
return session.send("无法获取页面截图。");
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return next();
|
|
181
|
+
});
|
|
144
182
|
}
|
|
145
183
|
__name(apply, "apply");
|
|
146
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
147
184
|
0 && (module.exports = {
|
|
148
185
|
Config,
|
|
149
186
|
apply,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-booth-get",
|
|
3
3
|
"description": "通过url与名称检查摊位物品并反馈用户搜索的图片",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"rixiang <1148147857@qq.com>"
|
|
7
7
|
],
|
|
@@ -37,4 +37,4 @@
|
|
|
37
37
|
"pngjs": "^7.0.0",
|
|
38
38
|
"puppeteer-core": "^22.12.1"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|