kokkoro-plugin-saucenao 1.0.2 → 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/LICENSE +21 -21
- package/README.md +39 -41
- package/package.json +18 -23
- package/src/index.ts +230 -0
- package/images/h.jpg +0 -0
- package/lib/index.js +0 -47
- package/lib/service.js +0 -50
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Yuki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,41 +1,39 @@
|
|
|
1
|
-
# kokkoro-plugin-saucenao
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```shell
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
SAUCENAO_API_KEY=3d14159265358979323846264338327950288419
|
|
41
|
-
```
|
|
1
|
+
# kokkoro-plugin-saucenao
|
|
2
|
+
|
|
3
|
+
使用 SauceNAO 搜索图片来源。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
bun add kokkoro-plugin-saucenao
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 指令
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
/搜图
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
发送「/搜图」时附带图片,插件会搜索消息中的第一张图片,并以 Markdown 返回搜索结果,默认显示相似度最高的 3 条。
|
|
18
|
+
|
|
19
|
+
## 快捷方式
|
|
20
|
+
|
|
21
|
+
发送「搜图」时附带图片,也会返回搜索结果。群聊需要开启「获取群内全部消息」权限。
|
|
22
|
+
|
|
23
|
+
```text
|
|
24
|
+
搜图
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 环境变量
|
|
28
|
+
|
|
29
|
+
使用插件前,需要在项目根目录的 `.env` 文件中填写 [SauceNAO API Key](https://saucenao.com/user.php?page=search-api)。
|
|
30
|
+
|
|
31
|
+
```ini
|
|
32
|
+
SAUCENAO_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
33
|
+
SAUCENAO_NUMRES=3
|
|
34
|
+
SAUCENAO_SIMILARITY_THRESHOLD=50
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`SAUCENAO_NUMRES` 表示返回结果数量,默认值为 `3`。
|
|
38
|
+
|
|
39
|
+
`SAUCENAO_SIMILARITY_THRESHOLD` 表示显示原缩略图的最低相似度,默认值为 `50`。低于该数值时,插件会使用表情包替换缩略图,避免搜出奇怪的图社死。
|
package/package.json
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kokkoro-plugin-saucenao",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "SauceNAO 图片搜索",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bot",
|
|
7
|
+
"kokkoro",
|
|
8
|
+
"saucenao",
|
|
9
|
+
"qq"
|
|
10
|
+
],
|
|
11
|
+
"bugs": "https://github.com/kokkorojs/kokkoro/issues",
|
|
9
12
|
"repository": {
|
|
10
13
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/kokkorojs/kokkoro
|
|
14
|
+
"url": "git+https://github.com/kokkorojs/kokkoro.git",
|
|
15
|
+
"directory": "plugins/saucenao"
|
|
12
16
|
},
|
|
13
|
-
"keywords": [
|
|
14
|
-
"kokkoro"
|
|
15
|
-
],
|
|
16
|
-
"author": "yuki <mail@yuki.sh>",
|
|
17
17
|
"license": "MIT",
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"homepage": "https://github.com/kokkorojs/kokkoro-plugin-saucenao#readme",
|
|
22
|
-
"devDependencies": {
|
|
23
|
-
"@types/node": "^18.11.18",
|
|
24
|
-
"kokkoro": "^1.3.0"
|
|
25
|
-
},
|
|
18
|
+
"author": "Yuki <admin@yuki.sh>",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": "./src/index.ts",
|
|
26
21
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"images"
|
|
22
|
+
"src"
|
|
29
23
|
],
|
|
30
|
-
"
|
|
31
|
-
"
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@kokkoro/core": "^3.1.3",
|
|
26
|
+
"typescript": "^6.0.3"
|
|
32
27
|
}
|
|
33
|
-
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { Image } from 'bun';
|
|
2
|
+
|
|
3
|
+
import { type CommandReply, useCommand } from '@kokkoro/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* SauceNAO JSON API 搜索响应。
|
|
7
|
+
*
|
|
8
|
+
* @see https://saucenao.com/user.php?page=search-api
|
|
9
|
+
*/
|
|
10
|
+
interface SauceNao {
|
|
11
|
+
/** 搜索请求、配额和索引状态。 */
|
|
12
|
+
readonly header: {
|
|
13
|
+
/** SauceNAO 用户 ID。 */
|
|
14
|
+
readonly user_id: string;
|
|
15
|
+
/** 账户类型。 */
|
|
16
|
+
readonly account_type: string;
|
|
17
|
+
/** 30 秒内允许的请求数量。 */
|
|
18
|
+
readonly short_limit: string;
|
|
19
|
+
/** 24 小时内允许的请求数量。 */
|
|
20
|
+
readonly long_limit: string;
|
|
21
|
+
/** 24 小时内剩余的请求数量。 */
|
|
22
|
+
readonly long_remaining: number;
|
|
23
|
+
/** 30 秒内剩余的请求数量。 */
|
|
24
|
+
readonly short_remaining: number;
|
|
25
|
+
/** 请求状态,`0` 表示成功。 */
|
|
26
|
+
readonly status: number;
|
|
27
|
+
/** 请求失败时返回的错误信息。 */
|
|
28
|
+
readonly message?: string;
|
|
29
|
+
/** `numres` 请求的结果数量。 */
|
|
30
|
+
readonly results_requested: string;
|
|
31
|
+
/** 参与搜索的索引状态,以索引编号为键。 */
|
|
32
|
+
readonly index: Readonly<
|
|
33
|
+
Record<
|
|
34
|
+
string,
|
|
35
|
+
{
|
|
36
|
+
/** 索引状态。 */
|
|
37
|
+
readonly status: number;
|
|
38
|
+
/** 父索引编号。 */
|
|
39
|
+
readonly parent_id: number;
|
|
40
|
+
/** 索引编号。 */
|
|
41
|
+
readonly id: number;
|
|
42
|
+
/** 该索引匹配到的结果数量。 */
|
|
43
|
+
readonly results: number;
|
|
44
|
+
}
|
|
45
|
+
>
|
|
46
|
+
>;
|
|
47
|
+
/** 本次搜索的深度。 */
|
|
48
|
+
readonly search_depth: string;
|
|
49
|
+
/** 返回结果中的最低相似度。 */
|
|
50
|
+
readonly minimum_similarity: number;
|
|
51
|
+
/** 用于显示查询图片的路径。 */
|
|
52
|
+
readonly query_image_display: string;
|
|
53
|
+
/** 查询图片的文件名。 */
|
|
54
|
+
readonly query_image: string;
|
|
55
|
+
/** 实际返回的结果数量。 */
|
|
56
|
+
readonly results_returned: number;
|
|
57
|
+
};
|
|
58
|
+
/** 匹配到的图片来源。 */
|
|
59
|
+
readonly results: readonly ImageSource[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** SauceNAO 返回的一条图片来源。 */
|
|
63
|
+
interface ImageSource {
|
|
64
|
+
/** 匹配结果信息。 */
|
|
65
|
+
readonly header: {
|
|
66
|
+
/** 相似度百分比。 */
|
|
67
|
+
readonly similarity: string;
|
|
68
|
+
/** 缩略图地址。 */
|
|
69
|
+
readonly thumbnail: string;
|
|
70
|
+
/** 搜索索引编号。 */
|
|
71
|
+
readonly index_id: number;
|
|
72
|
+
/** 搜索索引名称。 */
|
|
73
|
+
readonly index_name: string;
|
|
74
|
+
/** 重复结果数量。 */
|
|
75
|
+
readonly dupes: number;
|
|
76
|
+
/** 隐藏状态。 */
|
|
77
|
+
readonly hidden: number;
|
|
78
|
+
};
|
|
79
|
+
/** 来源数据。字段由 `header.index_id` 对应的搜索索引决定。 */
|
|
80
|
+
readonly data: {
|
|
81
|
+
/** 搜索索引可能返回的其他专有字段。 */
|
|
82
|
+
readonly [key: string]: unknown;
|
|
83
|
+
/** 来源页面地址。 */
|
|
84
|
+
readonly ext_urls?: readonly string[];
|
|
85
|
+
/** 作品标题。 */
|
|
86
|
+
readonly title?: string;
|
|
87
|
+
/** 作品或系列名称。 */
|
|
88
|
+
readonly material?: string;
|
|
89
|
+
/** 日文名称。 */
|
|
90
|
+
readonly jp_name?: string;
|
|
91
|
+
/** 英文名称。 */
|
|
92
|
+
readonly eng_name?: string;
|
|
93
|
+
/** 原始来源。 */
|
|
94
|
+
readonly source?: string;
|
|
95
|
+
/** 创作者名称。 */
|
|
96
|
+
readonly creator?: string | readonly string[];
|
|
97
|
+
/** 角色名称。 */
|
|
98
|
+
readonly characters?: string;
|
|
99
|
+
/** 发布时间。 */
|
|
100
|
+
readonly created_at?: string;
|
|
101
|
+
/** Danbooru 作品 ID。 */
|
|
102
|
+
readonly danbooru_id?: number;
|
|
103
|
+
/** e621 作品 ID。 */
|
|
104
|
+
readonly e621_id?: number;
|
|
105
|
+
/** Gelbooru 作品 ID。 */
|
|
106
|
+
readonly gelbooru_id?: number;
|
|
107
|
+
/** Pixiv 作者 ID。 */
|
|
108
|
+
readonly member_id?: number;
|
|
109
|
+
/** Pixiv 作者名称。 */
|
|
110
|
+
readonly member_name?: string;
|
|
111
|
+
/** Pixiv 作品 ID。 */
|
|
112
|
+
readonly pixiv_id?: number;
|
|
113
|
+
/** 推文 ID。 */
|
|
114
|
+
readonly tweet_id?: string;
|
|
115
|
+
/** Twitter 用户名。 */
|
|
116
|
+
readonly twitter_user_handle?: string;
|
|
117
|
+
/** Twitter 用户 ID。 */
|
|
118
|
+
readonly twitter_user_id?: string;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const { SAUCENAO_API_KEY, SAUCENAO_NUMRES = 3, SAUCENAO_SIMILARITY_THRESHOLD = 50 } = import.meta.env;
|
|
123
|
+
const SAUCENAO_API = new URL('https://saucenao.com/search.php');
|
|
124
|
+
const LOW_SIMILARITY_THUMBNAIL = 'https://kokkoro.js.org/saucenao-low-similarity.jpg';
|
|
125
|
+
const similarityThreshold = Number(SAUCENAO_SIMILARITY_THRESHOLD);
|
|
126
|
+
|
|
127
|
+
function getTitle(data: ImageSource['data']) {
|
|
128
|
+
const { eng_name: engName, jp_name: jpName, material, source, title } = data;
|
|
129
|
+
return title || material || jpName || engName || source;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function getPlatform(indexName: string) {
|
|
133
|
+
const [, platform] = indexName.match(/^Index #\d+:\s*(.*?)(?:\s+-\s+.*)?$/) ?? [];
|
|
134
|
+
return platform ?? indexName;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getUrl(data: ImageSource['data']) {
|
|
138
|
+
const [url] = data.ext_urls ?? [];
|
|
139
|
+
|
|
140
|
+
if (url) {
|
|
141
|
+
return url;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (data.source && URL.canParse(data.source)) {
|
|
145
|
+
return data.source;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function searchImage(image: string) {
|
|
150
|
+
if (!SAUCENAO_API_KEY) {
|
|
151
|
+
throw new Error('未配置 SAUCENAO_API_KEY 环境变量');
|
|
152
|
+
}
|
|
153
|
+
const form = new FormData();
|
|
154
|
+
|
|
155
|
+
form.set('api_key', SAUCENAO_API_KEY);
|
|
156
|
+
form.set('output_type', '2');
|
|
157
|
+
form.set('numres', String(SAUCENAO_NUMRES));
|
|
158
|
+
form.set('db', '999');
|
|
159
|
+
form.set('url', image);
|
|
160
|
+
|
|
161
|
+
const response = await fetch(SAUCENAO_API, { method: 'POST', body: form });
|
|
162
|
+
|
|
163
|
+
if (!response.ok) {
|
|
164
|
+
throw new Error(`接口请求失败,状态码 ${response.status}`);
|
|
165
|
+
}
|
|
166
|
+
const { header, results } = <SauceNao>await response.json();
|
|
167
|
+
|
|
168
|
+
if (header.status !== 0) {
|
|
169
|
+
throw new Error(header.message ?? `SauceNAO 搜索失败,状态码 ${header.status}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (results.length === 0) {
|
|
173
|
+
throw new Error('没有找到图片来源');
|
|
174
|
+
}
|
|
175
|
+
return results;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function createMessage(results: readonly ImageSource[]) {
|
|
179
|
+
const content = await Promise.all(
|
|
180
|
+
results.map(async ({ data, header }, index) => {
|
|
181
|
+
const { index_name: indexName, similarity, thumbnail } = header;
|
|
182
|
+
const platform = getPlatform(indexName);
|
|
183
|
+
const title = getTitle(data) || platform;
|
|
184
|
+
const url = getUrl(data);
|
|
185
|
+
const lines = ['### 标题', `- ${title}`, '### 平台', `- ${platform}`, '### 相似度', `- ${similarity}%`];
|
|
186
|
+
|
|
187
|
+
if (thumbnail) {
|
|
188
|
+
if (Number(similarity) < similarityThreshold) {
|
|
189
|
+
lines.push('### 缩略图', ``);
|
|
190
|
+
} else {
|
|
191
|
+
const response = await fetch(thumbnail);
|
|
192
|
+
|
|
193
|
+
if (!response.ok) {
|
|
194
|
+
throw new Error(`缩略图请求失败,状态码 ${response.status}`);
|
|
195
|
+
}
|
|
196
|
+
const { height, width } = await new Image(await response.blob()).metadata();
|
|
197
|
+
|
|
198
|
+
lines.push('### 缩略图', ``);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (url) {
|
|
203
|
+
lines.push(`[查看来源](${url})`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (index < results.length - 1) {
|
|
207
|
+
lines.push('***');
|
|
208
|
+
}
|
|
209
|
+
return lines.join('\n');
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
msg_type: 2,
|
|
215
|
+
markdown: {
|
|
216
|
+
content: ['## SauceNAO 搜图结果', '***', ...content].join('\n\n'),
|
|
217
|
+
},
|
|
218
|
+
} satisfies CommandReply;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export default () => {
|
|
222
|
+
useCommand('/搜图', async context => {
|
|
223
|
+
const image = context.attachments?.find(attachment => attachment.content_type?.startsWith('image/'));
|
|
224
|
+
|
|
225
|
+
if (!image?.url) {
|
|
226
|
+
throw new Error('请在指令中附带需要搜索的图片');
|
|
227
|
+
}
|
|
228
|
+
return createMessage(await searchImage(image.url));
|
|
229
|
+
}).shortcut('搜图');
|
|
230
|
+
};
|
package/images/h.jpg
DELETED
|
Binary file
|
package/lib/index.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const kokkoro_1 = require("kokkoro");
|
|
4
|
-
const service_1 = require("./service");
|
|
5
|
-
const option = {
|
|
6
|
-
apply: true,
|
|
7
|
-
lock: false,
|
|
8
|
-
results: 5,
|
|
9
|
-
mask: [],
|
|
10
|
-
excludeMask: [],
|
|
11
|
-
testMode: false,
|
|
12
|
-
db: 999,
|
|
13
|
-
similarity: 50,
|
|
14
|
-
};
|
|
15
|
-
const { version } = require('../package.json');
|
|
16
|
-
const plugin = new kokkoro_1.Plugin('saucenao', option);
|
|
17
|
-
const service = new service_1.SaucenaoService(plugin.logger, process.env.SAUCENAO_API_KEY);
|
|
18
|
-
plugin
|
|
19
|
-
.version(version);
|
|
20
|
-
plugin
|
|
21
|
-
.command('search', 'group')
|
|
22
|
-
.sugar(/^搜图$/)
|
|
23
|
-
.action(async (ctx) => {
|
|
24
|
-
const { bot, option, sender, group_id } = ctx;
|
|
25
|
-
const { user_id } = sender;
|
|
26
|
-
const scanner = new kokkoro_1.Scanner(bot);
|
|
27
|
-
await ctx.reply(`请发送你要搜索的图片 (●'◡'●)`);
|
|
28
|
-
const { url } = await scanner.nextImage({
|
|
29
|
-
user_id, group_id,
|
|
30
|
-
});
|
|
31
|
-
const results = await service.searchImage(url, option);
|
|
32
|
-
const results_length = results.length;
|
|
33
|
-
const forwardMessage = [];
|
|
34
|
-
for (let i = 0; i < results_length; i++) {
|
|
35
|
-
const result = results[i];
|
|
36
|
-
const { site, similarity, thumbnail, authorName, url } = result;
|
|
37
|
-
const message = {
|
|
38
|
-
message: [`封面:\n`, kokkoro_1.segment.image(thumbnail), `\n平台:${site}\n相似度:${similarity}\n作者:${authorName}\n来源:${url}`],
|
|
39
|
-
user_id: ctx.self_id,
|
|
40
|
-
nickname: bot.nickname,
|
|
41
|
-
};
|
|
42
|
-
forwardMessage.push(message);
|
|
43
|
-
}
|
|
44
|
-
await bot
|
|
45
|
-
.makeForwardMsg(forwardMessage)
|
|
46
|
-
.then(xml => ctx.reply(xml));
|
|
47
|
-
});
|
package/lib/service.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SaucenaoService = void 0;
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const sagiri_1 = __importDefault(require("sagiri"));
|
|
9
|
-
const xcw = (0, path_1.join)(__dirname, '../images/h.jpg');
|
|
10
|
-
class SaucenaoService {
|
|
11
|
-
logger;
|
|
12
|
-
api_key;
|
|
13
|
-
/** API */
|
|
14
|
-
api;
|
|
15
|
-
client;
|
|
16
|
-
constructor(
|
|
17
|
-
/** 日志 */
|
|
18
|
-
logger,
|
|
19
|
-
/** API key */
|
|
20
|
-
api_key) {
|
|
21
|
-
this.logger = logger;
|
|
22
|
-
this.api_key = api_key;
|
|
23
|
-
this.api = 'https://saucenao.com/search.php';
|
|
24
|
-
this.client = this.api_key ? (0, sagiri_1.default)(this.api_key) : undefined;
|
|
25
|
-
if (!this.api_key) {
|
|
26
|
-
this.logger.warn('你没有添加 api key ,saucenao 服务将无法正常使用');
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async searchImage(url, option) {
|
|
30
|
-
if (!this.client) {
|
|
31
|
-
throw new Error('你没有添加 api key ,saucenao 服务将无法正常使用');
|
|
32
|
-
}
|
|
33
|
-
const options = {
|
|
34
|
-
results: option.results,
|
|
35
|
-
mask: option.mask.length ? option.mask : undefined,
|
|
36
|
-
excludeMask: option.excludeMask,
|
|
37
|
-
testMode: option.testMode,
|
|
38
|
-
db: option.db,
|
|
39
|
-
};
|
|
40
|
-
const results = await this.client(url, options);
|
|
41
|
-
results.map((result) => {
|
|
42
|
-
if (result.similarity < option.similarity) {
|
|
43
|
-
result.thumbnail = xcw;
|
|
44
|
-
}
|
|
45
|
-
return result;
|
|
46
|
-
});
|
|
47
|
-
return results;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
exports.SaucenaoService = SaucenaoService;
|