koishi-plugin-checkgal 1.2.1 → 1.3.1
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/api.d.ts +2 -1
- package/lib/api.js +51 -3
- package/lib/commands.js +13 -15
- package/package.json +2 -4
package/lib/api.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ import { Config } from './config';
|
|
|
4
4
|
export declare class TouchGalAPI {
|
|
5
5
|
private http;
|
|
6
6
|
private logger;
|
|
7
|
+
private tempDir;
|
|
7
8
|
constructor(ctx: Context);
|
|
8
9
|
searchGame(keyword: string, config: Config): Promise<GameInfo[]>;
|
|
9
10
|
getDownloads(patchId: number): Promise<DownloadResource[]>;
|
|
10
|
-
downloadAndConvertImage(url: string): Promise<
|
|
11
|
+
downloadAndConvertImage(url: string): Promise<string | null>;
|
|
11
12
|
}
|
package/lib/api.js
CHANGED
|
@@ -1,11 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.TouchGalAPI = void 0;
|
|
4
|
-
const
|
|
40
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const crypto = __importStar(require("crypto"));
|
|
43
|
+
const fs_1 = require("fs");
|
|
5
44
|
class TouchGalAPI {
|
|
6
45
|
constructor(ctx) {
|
|
7
46
|
this.http = ctx.http;
|
|
8
47
|
this.logger = ctx.logger('checkgal-api');
|
|
48
|
+
this.tempDir = path.join(ctx.app.baseDir, 'temp');
|
|
49
|
+
ctx.on('ready', async () => {
|
|
50
|
+
await fs_1.promises.mkdir(this.tempDir, { recursive: true });
|
|
51
|
+
});
|
|
9
52
|
}
|
|
10
53
|
async searchGame(keyword, config) {
|
|
11
54
|
const url = 'https://www.touchgal.us/api/search';
|
|
@@ -68,8 +111,13 @@ class TouchGalAPI {
|
|
|
68
111
|
});
|
|
69
112
|
if (!(response instanceof ArrayBuffer))
|
|
70
113
|
return null;
|
|
71
|
-
const
|
|
72
|
-
|
|
114
|
+
const imageBuffer = await (0, sharp_1.default)(Buffer.from(response)).jpeg().toBuffer();
|
|
115
|
+
const hash = crypto.createHash('md5').update(url).digest('hex');
|
|
116
|
+
const fileName = `${hash}.jpg`;
|
|
117
|
+
const filePath = path.join(this.tempDir, fileName);
|
|
118
|
+
await fs_1.promises.writeFile(filePath, imageBuffer);
|
|
119
|
+
// 返回相对于 koishi 根目录的相对路径
|
|
120
|
+
return `temp/${fileName}`;
|
|
73
121
|
}
|
|
74
122
|
catch (error) {
|
|
75
123
|
this.logger.error(`Failed to download or convert image from ${url}:`, error);
|
package/lib/commands.js
CHANGED
|
@@ -18,9 +18,15 @@ function apply(ctx, config) {
|
|
|
18
18
|
}
|
|
19
19
|
// 缓存结果
|
|
20
20
|
results.forEach(game => ctx.gameCache.set(game.id, game));
|
|
21
|
-
//
|
|
22
|
-
const
|
|
21
|
+
// 并发下载并转换所有图片
|
|
22
|
+
const imagePaths = await Promise.all(results.map(game => ctx.touchgal.downloadAndConvertImage(game.banner)));
|
|
23
|
+
const forwardMessages = results.map((game, index) => {
|
|
24
|
+
const imagePath = imagePaths[index];
|
|
25
|
+
const imageElement = imagePath
|
|
26
|
+
? (0, koishi_1.h)('image', { url: imagePath })
|
|
27
|
+
: (0, koishi_1.h)('text', { content: '封面图加载失败' });
|
|
23
28
|
const content = [
|
|
29
|
+
imageElement,
|
|
24
30
|
`ID: ${game.id}`,
|
|
25
31
|
`名称: ${game.name}`,
|
|
26
32
|
`平台: ${game.platform.join(', ')}`,
|
|
@@ -29,14 +35,6 @@ function apply(ctx, config) {
|
|
|
29
35
|
return (0, koishi_1.h)('message', { userId: session.bot.selfId, nickname: 'CheckGal Bot' }, content);
|
|
30
36
|
});
|
|
31
37
|
await session.send((0, koishi_1.h)('message', { forward: true }, forwardMessages));
|
|
32
|
-
await session.send('封面图将单独发送:');
|
|
33
|
-
// 单独逐张发送图片
|
|
34
|
-
for (const game of results) {
|
|
35
|
-
const imageBuffer = await ctx.touchgal.downloadAndConvertImage(game.banner);
|
|
36
|
-
if (imageBuffer) {
|
|
37
|
-
await session.send(koishi_1.h.image(imageBuffer, 'image/jpeg'));
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
38
|
});
|
|
41
39
|
ctx.command('下载gal <id:number>', '获取Galgame下载地址')
|
|
42
40
|
.action(async ({ session }, id) => {
|
|
@@ -63,12 +61,12 @@ function apply(ctx, config) {
|
|
|
63
61
|
return `未找到ID为 ${id} 的下载资源。`;
|
|
64
62
|
}
|
|
65
63
|
const gameTitle = gameInfo ? `游戏: ${gameInfo.name} (ID: ${id})` : `游戏ID: ${id}`;
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
64
|
+
const imagePath = gameInfo ? await ctx.touchgal.downloadAndConvertImage(gameInfo.banner) : null;
|
|
65
|
+
const imageElement = imagePath
|
|
66
|
+
? (0, koishi_1.h)('image', { url: imagePath })
|
|
67
|
+
: (0, koishi_1.h)('text', { content: gameInfo ? '封面图加载失败' : '' });
|
|
71
68
|
const header = [
|
|
69
|
+
imageElement,
|
|
72
70
|
gameTitle,
|
|
73
71
|
`共找到 ${downloads.length} 个下载资源:`,
|
|
74
72
|
].filter(Boolean).join('\n');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-checkgal",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "个人测试请勿使用",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -26,8 +26,6 @@
|
|
|
26
26
|
"typescript": "^5.9.3"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"@jimp/wasm-avif": "^1.6.0",
|
|
31
|
-
"jimp": "^1.6.0"
|
|
29
|
+
"sharp": "^0.34.4"
|
|
32
30
|
}
|
|
33
31
|
}
|