kokkoro-plugin-saucenao 2.0.1 → 2.0.2

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/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  SauceNAO 图片搜索,查找动漫、漫画、插画等图片的来源。
4
4
 
5
+ > [!IMPORTANT]
6
+ > 使用 SauceNAO API 前,需要先[申请 API Key](https://saucenao.com/user.php?page=search-api),并完成[环境变量配置](#环境变量)。
7
+
5
8
  ## 安装
6
9
 
7
10
  ```shell
@@ -18,12 +21,26 @@ bun add kokkoro-plugin-saucenao
18
21
 
19
22
  ## 快捷方式
20
23
 
21
- 发送「搜图」时附带图片,也会返回搜索结果。群聊需要开启「获取群内全部消息」权限。
24
+ 发送「搜图」时附带图片,也会返回搜索结果。要让普通群消息触发快捷方式,需要开启「获取群内全部消息」权限。
22
25
 
23
26
  ```text
24
27
  搜图
25
28
  ```
26
29
 
30
+ ## API
31
+
32
+ 其他插件可以从 `service` 入口导入 `fetchImageSources()` 搜索图片来源,再从 `util` 入口导入 `createMarkdown()` 生成 QQ Markdown:
33
+
34
+ ```typescript
35
+ import { fetchImageSources } from 'kokkoro-plugin-saucenao/service';
36
+ import { createMarkdown } from 'kokkoro-plugin-saucenao/util';
37
+
38
+ const { results } = await fetchImageSources('https://example.com/image.jpg');
39
+ const markdown = await createMarkdown(results);
40
+ ```
41
+
42
+ `fetchImageSources()` 返回 SauceNAO 的完整响应,可通过第二个参数传入日志记录器,记录完整请求参数和接口响应。`service` 入口同时导出 `SauceNao` 和 `ImageSource` 类型。
43
+
27
44
  ## 环境变量
28
45
 
29
46
  使用插件前,需要在项目根目录的 `.env` 文件中填写 [SauceNAO API Key](https://saucenao.com/user.php?page=search-api)。
@@ -37,17 +54,3 @@ SAUCENAO_SIMILARITY_THRESHOLD=50
37
54
  `SAUCENAO_NUMRES` 表示返回结果数量,默认值为 `3`。
38
55
 
39
56
  `SAUCENAO_SIMILARITY_THRESHOLD` 表示显示原缩略图的最低相似度,默认值为 `50`。低于该数值时,插件会使用表情包替换缩略图,避免搜出奇怪的图社死。
40
-
41
- ## API
42
-
43
- 其他插件可以从 `service` 入口导入 `fetchImageSources()` 搜索图片来源,再从 `util` 入口导入 `createMarkdown()` 生成 QQ Markdown:
44
-
45
- ```typescript
46
- import { fetchImageSources } from 'kokkoro-plugin-saucenao/service';
47
- import { createMarkdown } from 'kokkoro-plugin-saucenao/util';
48
-
49
- const { results } = await fetchImageSources('https://example.com/image.jpg');
50
- const markdown = await createMarkdown(results);
51
- ```
52
-
53
- `fetchImageSources()` 返回 SauceNAO 的完整响应。`service` 入口同时导出 `SauceNao` 和 `ImageSource` 类型。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokkoro-plugin-saucenao",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "SauceNAO 图片搜索,查找动漫、漫画、插画等图片的来源。",
5
5
  "keywords": [
6
6
  "bot",
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useCommand, useLogger } from '@kokkoro/core';
2
2
 
3
- import { fetchImageSources, SAUCENAO_API } from './service';
3
+ import { fetchImageSources } from './service';
4
4
  import { createMarkdown } from './util';
5
5
 
6
6
  const logger = useLogger();
@@ -12,16 +12,9 @@ export default () => {
12
12
  if (!image?.url) {
13
13
  throw new Error('请在指令中附带需要搜索的图片');
14
14
  }
15
- logger.debug('发送 SauceNAO 请求', {
16
- method: 'POST',
17
- url: SAUCENAO_API,
18
- payload: { url: image.url },
19
- });
20
-
21
- const result = await fetchImageSources(image.url);
15
+ const result = await fetchImageSources(image.url, logger);
22
16
  const { results } = result;
23
17
 
24
- logger.debug('收到 SauceNAO 响应', result);
25
18
  logger.info('已找到图片来源', { count: results.length });
26
19
 
27
20
  await context.reply({
package/src/service.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { type Logger } from '@kokkoro/core';
2
+
1
3
  /**
2
4
  * SauceNAO JSON API 搜索响应。
3
5
  *
@@ -23,7 +25,7 @@ export interface SauceNao {
23
25
  /** 请求失败时返回的错误信息。 */
24
26
  readonly message?: string;
25
27
  /** `numres` 请求的结果数量。 */
26
- readonly results_requested: string;
28
+ readonly results_requested: number;
27
29
  /** 参与搜索的索引状态,以索引编号为键。 */
28
30
  readonly index: Readonly<
29
31
  Record<
@@ -115,24 +117,33 @@ export interface ImageSource {
115
117
  };
116
118
  }
117
119
 
118
- const { SAUCENAO_API_KEY, SAUCENAO_NUMRES = 3 } = import.meta.env;
120
+ const { SAUCENAO_API_KEY, SAUCENAO_NUMRES = '3' } = import.meta.env;
119
121
  /** SauceNAO JSON API 的请求地址。 */
120
122
  export const SAUCENAO_API = 'https://saucenao.com/search.php';
121
123
 
122
124
  /** 使用 SauceNAO 搜索图片来源,并返回完整的接口响应。 */
123
- export async function fetchImageSources(image: string): Promise<SauceNao> {
125
+ export async function fetchImageSources(url: string, logger?: Logger): Promise<SauceNao> {
124
126
  if (!SAUCENAO_API_KEY) {
125
127
  throw new Error('未配置 SAUCENAO_API_KEY 环境变量');
126
128
  }
127
- const form = new FormData();
129
+ const endpoint = new URL(SAUCENAO_API);
130
+ const payload = {
131
+ api_key: SAUCENAO_API_KEY,
132
+ output_type: '2',
133
+ numres: SAUCENAO_NUMRES,
134
+ db: '999',
135
+ url,
136
+ };
137
+ const params = new URLSearchParams(payload);
128
138
 
129
- form.set('api_key', SAUCENAO_API_KEY);
130
- form.set('output_type', '2');
131
- form.set('numres', String(SAUCENAO_NUMRES));
132
- form.set('db', '999');
133
- form.set('url', image);
139
+ endpoint.search = params.toString();
140
+ logger?.debug('发送 SauceNAO 请求', {
141
+ method: 'GET',
142
+ url: SAUCENAO_API,
143
+ payload,
144
+ });
134
145
 
135
- const response = await fetch(SAUCENAO_API, { method: 'POST', body: form });
146
+ const response = await fetch(endpoint);
136
147
 
137
148
  if (!response.ok) {
138
149
  throw new Error(`接口请求失败,状态码 ${response.status}`);
@@ -140,6 +151,8 @@ export async function fetchImageSources(image: string): Promise<SauceNao> {
140
151
  const result = <SauceNao>await response.json();
141
152
  const { header, results } = result;
142
153
 
154
+ logger?.debug('收到 SauceNAO 响应', result);
155
+
143
156
  if (header.status !== 0) {
144
157
  throw new Error(header.message ?? `SauceNAO 搜索失败,状态码 ${header.status}`);
145
158
  }
package/src/util.ts CHANGED
@@ -36,11 +36,11 @@ export async function createMarkdown(results: readonly ImageSource[]): Promise<s
36
36
  const platform = getPlatform(indexName);
37
37
  const title = getTitle(data) || platform;
38
38
  const url = getUrl(data);
39
- const lines = ['### 标题', `- ${title}`, '### 平台', `- ${platform}`, '### 相似度', `- ${similarity}%`];
39
+ const lines = [`### ${index + 1}. ${title}`, `- 相似度:${similarity}%`, `- 平台:${platform}`];
40
40
 
41
41
  if (thumbnail) {
42
42
  if (Number(similarity) < similarityThreshold) {
43
- lines.push('### 缩略图', `![缩略图 #168px #142px](${LOW_SIMILARITY_THUMBNAIL})`);
43
+ lines.push('', `![缩略图 #168px #142px](${LOW_SIMILARITY_THUMBNAIL})`);
44
44
  } else {
45
45
  const response = await fetch(thumbnail);
46
46
 
@@ -49,20 +49,16 @@ export async function createMarkdown(results: readonly ImageSource[]): Promise<s
49
49
  }
50
50
  const { height, width } = await new Image(await response.blob()).metadata();
51
51
 
52
- lines.push('### 缩略图', `![缩略图 #${width}px #${height}px](${thumbnail})`);
52
+ lines.push('', `![缩略图 #${width}px #${height}px](${thumbnail})`);
53
53
  }
54
54
  }
55
55
 
56
56
  if (url) {
57
- lines.push(`[查看来源](${url})`);
58
- }
59
-
60
- if (index < results.length - 1) {
61
- lines.push('***');
57
+ lines.push('', `[查看来源](${url})`);
62
58
  }
63
59
  return lines.join('\n');
64
60
  }),
65
61
  );
66
62
 
67
- return ['## SauceNAO 搜图结果', '***', ...content].join('\n\n');
63
+ return ['## SauceNAO 搜图结果', ...content].join('\n***\n');
68
64
  }