aiconductor-cli 1.2.1 → 1.2.3
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/CLAUDE.md +76 -0
- package/README.md +266 -108
- package/bin/aiconductor-cli.js +20 -0
- package/package.json +4 -3
- package/src/api.js +65 -26
- package/src/commands/douyin.js +109 -151
- package/src/commands/html_publish.js +54 -91
- package/src/commands/md_publish.js +87 -118
- package/src/commands/web_search.js +84 -120
- package/src/commands/xhs.js +55 -0
- package/src/lib/cli-utils.js +80 -0
- package/AGENTS.md +0 -79
- package/AIC /346/212/226/351/237/263/347/263/273/345/210/227API/346/216/245/345/217/243.txt" +0 -417
- package/CODEBUDDY.md +0 -79
package/src/api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const https = require("https");
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const path = require("path");
|
|
4
4
|
|
|
@@ -13,6 +13,7 @@ const DOUYIN_USER_DETAIL_PATH = "/api/douyin_user_detail";
|
|
|
13
13
|
const DOUYIN_USER_SEARCH_PATH = "/api/douyin_user_search";
|
|
14
14
|
const DOUYIN_VIDEO_SEARCH_PATH = "/api/douyin_video_search";
|
|
15
15
|
const DOUYIN_LINK_TO_TEXT_PATH = "/api/douyin_link_to_text_v2";
|
|
16
|
+
const XHS_NOTE_PUBLISH_PATH = "/api/xhs_note_publish";
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* 解析 API Key,三级优先级:命令行参数 > 系统环境变量 > .env 文件
|
|
@@ -40,6 +41,31 @@ function resolveApiKey(optionKey) {
|
|
|
40
41
|
return null;
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
/**
|
|
45
|
+
* 过滤响应中的计费信息
|
|
46
|
+
* @param {object} resp
|
|
47
|
+
* @returns {object}
|
|
48
|
+
*/
|
|
49
|
+
function stripBilling(resp) {
|
|
50
|
+
if (!resp || typeof resp !== "object") return resp;
|
|
51
|
+
const result = { ...resp };
|
|
52
|
+
delete result.billing;
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 校验整数参数,非有效正整数时报错
|
|
58
|
+
* @param {*} value
|
|
59
|
+
* @param {string} name
|
|
60
|
+
*/
|
|
61
|
+
function validateInt(value, name) {
|
|
62
|
+
if (value === undefined || value === null) return;
|
|
63
|
+
const n = Number(value);
|
|
64
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
65
|
+
throw Object.assign(new Error(`${name} 须为非负整数,收到: ${value}`), { type: "validation_error" });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
43
69
|
/**
|
|
44
70
|
* HTTP POST 请求封装
|
|
45
71
|
* @param {string} host
|
|
@@ -53,7 +79,7 @@ function httpPost(host, path, body, headers = {}) {
|
|
|
53
79
|
|
|
54
80
|
const options = {
|
|
55
81
|
hostname: host,
|
|
56
|
-
port:
|
|
82
|
+
port: 443,
|
|
57
83
|
path: path,
|
|
58
84
|
method: "POST",
|
|
59
85
|
headers: {
|
|
@@ -63,7 +89,7 @@ function httpPost(host, path, body, headers = {}) {
|
|
|
63
89
|
},
|
|
64
90
|
};
|
|
65
91
|
|
|
66
|
-
const req =
|
|
92
|
+
const req = https.request(options, (res) => {
|
|
67
93
|
let chunks = [];
|
|
68
94
|
res.on("data", (chunk) => chunks.push(chunk));
|
|
69
95
|
res.on("end", () => {
|
|
@@ -112,10 +138,7 @@ async function publishHtml(apiKey, htmlCode, title) {
|
|
|
112
138
|
if (title) reqBody.title = title;
|
|
113
139
|
|
|
114
140
|
const resp = await httpPost(API_HOST, API_PATH, reqBody);
|
|
115
|
-
|
|
116
|
-
// 过滤计费信息
|
|
117
|
-
const { billing, ...safe } = resp;
|
|
118
|
-
return safe;
|
|
141
|
+
return stripBilling(resp);
|
|
119
142
|
}
|
|
120
143
|
|
|
121
144
|
/**
|
|
@@ -125,6 +148,10 @@ async function publishHtml(apiKey, htmlCode, title) {
|
|
|
125
148
|
* @returns {Promise<object>}
|
|
126
149
|
*/
|
|
127
150
|
async function webSearch(apiKey, params) {
|
|
151
|
+
// 输入校验
|
|
152
|
+
validateInt(params.count, "count");
|
|
153
|
+
validateInt(params.authLevel, "authLevel");
|
|
154
|
+
|
|
128
155
|
const reqBody = {
|
|
129
156
|
query: params.query,
|
|
130
157
|
search_type: params.searchType,
|
|
@@ -140,9 +167,7 @@ async function webSearch(apiKey, params) {
|
|
|
140
167
|
Authorization: `Bearer ${apiKey}`,
|
|
141
168
|
});
|
|
142
169
|
|
|
143
|
-
|
|
144
|
-
const { billing, ...safe } = resp;
|
|
145
|
-
return safe;
|
|
170
|
+
return stripBilling(resp);
|
|
146
171
|
}
|
|
147
172
|
|
|
148
173
|
/**
|
|
@@ -163,8 +188,7 @@ async function publishMdToHtml(apiKey, mdText, filename, addCopyButton = false)
|
|
|
163
188
|
|
|
164
189
|
const resp = await httpPost(API_HOST, MD_TO_HTML_API_PATH, reqBody);
|
|
165
190
|
|
|
166
|
-
|
|
167
|
-
return safe;
|
|
191
|
+
return stripBilling(resp);
|
|
168
192
|
}
|
|
169
193
|
|
|
170
194
|
/**
|
|
@@ -178,8 +202,7 @@ async function douyinVideoInfo(apiKey, shareUrl) {
|
|
|
178
202
|
api_key: apiKey,
|
|
179
203
|
share_url: shareUrl,
|
|
180
204
|
});
|
|
181
|
-
|
|
182
|
-
return safe;
|
|
205
|
+
return stripBilling(resp);
|
|
183
206
|
}
|
|
184
207
|
|
|
185
208
|
/**
|
|
@@ -193,8 +216,7 @@ async function douyinVideoComments(apiKey, videoUrl, maxCount) {
|
|
|
193
216
|
const body = { api_key: apiKey, video_url: videoUrl };
|
|
194
217
|
if (maxCount !== undefined && maxCount !== null) body.max_count = maxCount;
|
|
195
218
|
const resp = await httpPost(API_HOST, DOUYIN_VIDEO_COMMENTS_PATH, body);
|
|
196
|
-
|
|
197
|
-
return safe;
|
|
219
|
+
return stripBilling(resp);
|
|
198
220
|
}
|
|
199
221
|
|
|
200
222
|
/**
|
|
@@ -210,8 +232,7 @@ async function douyinUserVideos(apiKey, homePage, maxSeconds, maxPages) {
|
|
|
210
232
|
if (maxSeconds !== undefined && maxSeconds !== null) body.max_seconds = maxSeconds;
|
|
211
233
|
if (maxPages !== undefined && maxPages !== null) body.max_pages = maxPages;
|
|
212
234
|
const resp = await httpPost(API_HOST, DOUYIN_USER_VIDEOS_PATH, body);
|
|
213
|
-
|
|
214
|
-
return safe;
|
|
235
|
+
return stripBilling(resp);
|
|
215
236
|
}
|
|
216
237
|
|
|
217
238
|
/**
|
|
@@ -225,8 +246,7 @@ async function douyinUserDetail(apiKey, homePage) {
|
|
|
225
246
|
api_key: apiKey,
|
|
226
247
|
home_page: homePage,
|
|
227
248
|
});
|
|
228
|
-
|
|
229
|
-
return safe;
|
|
249
|
+
return stripBilling(resp);
|
|
230
250
|
}
|
|
231
251
|
|
|
232
252
|
/**
|
|
@@ -243,8 +263,7 @@ async function douyinUserSearch(apiKey, keyword, opts = {}) {
|
|
|
243
263
|
if (opts.fansRange) body.douyin_user_fans = opts.fansRange;
|
|
244
264
|
if (opts.userType) body.douyin_user_type = opts.userType;
|
|
245
265
|
const resp = await httpPost(API_HOST, DOUYIN_USER_SEARCH_PATH, body);
|
|
246
|
-
|
|
247
|
-
return safe;
|
|
266
|
+
return stripBilling(resp);
|
|
248
267
|
}
|
|
249
268
|
|
|
250
269
|
/**
|
|
@@ -265,8 +284,7 @@ async function douyinVideoSearch(apiKey, keyword, opts = {}) {
|
|
|
265
284
|
const resp = await httpPost(API_HOST, DOUYIN_VIDEO_SEARCH_PATH, body, {
|
|
266
285
|
Authorization: `Bearer ${apiKey}`,
|
|
267
286
|
});
|
|
268
|
-
|
|
269
|
-
return safe;
|
|
287
|
+
return stripBilling(resp);
|
|
270
288
|
}
|
|
271
289
|
|
|
272
290
|
/**
|
|
@@ -280,8 +298,28 @@ async function douyinLinkToText(apiKey, videoUrl) {
|
|
|
280
298
|
api_key: apiKey,
|
|
281
299
|
video_url: videoUrl,
|
|
282
300
|
});
|
|
283
|
-
|
|
284
|
-
|
|
301
|
+
return stripBilling(resp);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 小红书笔记发布 — 生成扫码发布 H5 页面与二维码
|
|
306
|
+
* @param {string} apiKey
|
|
307
|
+
* @param {{ type: string, title?: string, content?: string, images?: string[], video?: string, cover?: string, transferToOss?: boolean }} params
|
|
308
|
+
* @returns {Promise<object>}
|
|
309
|
+
*/
|
|
310
|
+
async function xhsNotePublish(apiKey, params) {
|
|
311
|
+
const body = { type: params.type };
|
|
312
|
+
if (params.title) body.title = params.title;
|
|
313
|
+
if (params.content) body.content = params.content;
|
|
314
|
+
if (params.images && params.images.length > 0) body.images = params.images;
|
|
315
|
+
if (params.video) body.video = params.video;
|
|
316
|
+
if (params.cover) body.cover = params.cover;
|
|
317
|
+
if (params.transferToOss !== undefined) body.transfer_to_oss = params.transferToOss;
|
|
318
|
+
|
|
319
|
+
const resp = await httpPost(API_HOST, XHS_NOTE_PUBLISH_PATH, body, {
|
|
320
|
+
Authorization: `Bearer ${apiKey}`,
|
|
321
|
+
});
|
|
322
|
+
return stripBilling(resp);
|
|
285
323
|
}
|
|
286
324
|
|
|
287
325
|
module.exports = {
|
|
@@ -296,4 +334,5 @@ module.exports = {
|
|
|
296
334
|
douyinUserSearch,
|
|
297
335
|
douyinVideoSearch,
|
|
298
336
|
douyinLinkToText,
|
|
337
|
+
xhsNotePublish,
|
|
299
338
|
};
|
package/src/commands/douyin.js
CHANGED
|
@@ -1,151 +1,109 @@
|
|
|
1
|
-
const {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} = require("../
|
|
11
|
-
|
|
12
|
-
function
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
process.exit(0);
|
|
111
|
-
} catch (err) { handleError(err, format); process.exit(1); }
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function videoSearchCommand(keyword, options = {}) {
|
|
115
|
-
const format = options.format || "json";
|
|
116
|
-
const key = resolveKey(options.apiKey, format);
|
|
117
|
-
try {
|
|
118
|
-
const maxPages = options.maxPages ? parseInt(options.maxPages, 10) : undefined;
|
|
119
|
-
const maxSeconds = options.maxSeconds ? parseInt(options.maxSeconds, 10) : undefined;
|
|
120
|
-
const result = await douyinVideoSearch(key, keyword, {
|
|
121
|
-
maxPages,
|
|
122
|
-
maxSeconds,
|
|
123
|
-
sortType: options.sortType,
|
|
124
|
-
publishTime: options.publishTime,
|
|
125
|
-
filterDuration: options.filterDuration,
|
|
126
|
-
contentType: options.contentType,
|
|
127
|
-
});
|
|
128
|
-
outputResult(result, format);
|
|
129
|
-
process.exit(0);
|
|
130
|
-
} catch (err) { handleError(err, format); process.exit(1); }
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async function linkToTextCommand(videoUrl, options = {}) {
|
|
134
|
-
const format = options.format || "json";
|
|
135
|
-
const key = resolveKey(options.apiKey, format);
|
|
136
|
-
try {
|
|
137
|
-
const result = await douyinLinkToText(key, videoUrl);
|
|
138
|
-
outputResult(result, format);
|
|
139
|
-
process.exit(0);
|
|
140
|
-
} catch (err) { handleError(err, format); process.exit(1); }
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
module.exports = {
|
|
144
|
-
videoInfoCommand,
|
|
145
|
-
videoCommentsCommand,
|
|
146
|
-
userVideosCommand,
|
|
147
|
-
userDetailCommand,
|
|
148
|
-
userSearchCommand,
|
|
149
|
-
videoSearchCommand,
|
|
150
|
-
linkToTextCommand,
|
|
151
|
-
};
|
|
1
|
+
const {
|
|
2
|
+
douyinVideoInfo,
|
|
3
|
+
douyinVideoComments,
|
|
4
|
+
douyinUserVideos,
|
|
5
|
+
douyinUserDetail,
|
|
6
|
+
douyinUserSearch,
|
|
7
|
+
douyinVideoSearch,
|
|
8
|
+
douyinLinkToText,
|
|
9
|
+
} = require("../api");
|
|
10
|
+
const { outputResult, resolveKeyOrExit, handleApiError, safeParseInt } = require("../lib/cli-utils");
|
|
11
|
+
|
|
12
|
+
async function videoInfoCommand(shareUrl, options = {}) {
|
|
13
|
+
const format = options.format || "json";
|
|
14
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
15
|
+
try {
|
|
16
|
+
const result = await douyinVideoInfo(apiKey, shareUrl);
|
|
17
|
+
outputResult(result, format);
|
|
18
|
+
process.exit(0);
|
|
19
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function videoCommentsCommand(videoUrl, options = {}) {
|
|
23
|
+
const format = options.format || "json";
|
|
24
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
25
|
+
try {
|
|
26
|
+
const maxCount = safeParseInt(options.maxCount, undefined);
|
|
27
|
+
const result = await douyinVideoComments(apiKey, videoUrl, maxCount);
|
|
28
|
+
outputResult(result, format);
|
|
29
|
+
process.exit(0);
|
|
30
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function userVideosCommand(homePage, options = {}) {
|
|
34
|
+
const format = options.format || "json";
|
|
35
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
36
|
+
try {
|
|
37
|
+
const maxSeconds = safeParseInt(options.maxSeconds, undefined);
|
|
38
|
+
const maxPages = safeParseInt(options.maxPages, undefined);
|
|
39
|
+
const result = await douyinUserVideos(apiKey, homePage, maxSeconds, maxPages);
|
|
40
|
+
outputResult(result, format);
|
|
41
|
+
process.exit(0);
|
|
42
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function userDetailCommand(homePage, options = {}) {
|
|
46
|
+
const format = options.format || "json";
|
|
47
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
48
|
+
try {
|
|
49
|
+
const result = await douyinUserDetail(apiKey, homePage);
|
|
50
|
+
outputResult(result, format);
|
|
51
|
+
process.exit(0);
|
|
52
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function userSearchCommand(keyword, options = {}) {
|
|
56
|
+
const format = options.format || "json";
|
|
57
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
58
|
+
try {
|
|
59
|
+
const maxPages = safeParseInt(options.maxPages, undefined);
|
|
60
|
+
const maxSeconds = safeParseInt(options.maxSeconds, undefined);
|
|
61
|
+
const result = await douyinUserSearch(apiKey, keyword, {
|
|
62
|
+
maxPages,
|
|
63
|
+
maxSeconds,
|
|
64
|
+
fansRange: options.fansRange,
|
|
65
|
+
userType: options.userType,
|
|
66
|
+
});
|
|
67
|
+
outputResult(result, format);
|
|
68
|
+
process.exit(0);
|
|
69
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function videoSearchCommand(keyword, options = {}) {
|
|
73
|
+
const format = options.format || "json";
|
|
74
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
75
|
+
try {
|
|
76
|
+
const maxPages = safeParseInt(options.maxPages, undefined);
|
|
77
|
+
const maxSeconds = safeParseInt(options.maxSeconds, undefined);
|
|
78
|
+
const result = await douyinVideoSearch(apiKey, keyword, {
|
|
79
|
+
maxPages,
|
|
80
|
+
maxSeconds,
|
|
81
|
+
sortType: options.sortType,
|
|
82
|
+
publishTime: options.publishTime,
|
|
83
|
+
filterDuration: options.filterDuration,
|
|
84
|
+
contentType: options.contentType,
|
|
85
|
+
});
|
|
86
|
+
outputResult(result, format);
|
|
87
|
+
process.exit(0);
|
|
88
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function linkToTextCommand(videoUrl, options = {}) {
|
|
92
|
+
const format = options.format || "json";
|
|
93
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
94
|
+
try {
|
|
95
|
+
const result = await douyinLinkToText(apiKey, videoUrl);
|
|
96
|
+
outputResult(result, format);
|
|
97
|
+
process.exit(0);
|
|
98
|
+
} catch (err) { handleApiError(err, format); process.exit(1); }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = {
|
|
102
|
+
videoInfoCommand,
|
|
103
|
+
videoCommentsCommand,
|
|
104
|
+
userVideosCommand,
|
|
105
|
+
userDetailCommand,
|
|
106
|
+
userSearchCommand,
|
|
107
|
+
videoSearchCommand,
|
|
108
|
+
linkToTextCommand,
|
|
109
|
+
};
|
|
@@ -1,91 +1,54 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
const filePath = path.resolve(trimmed);
|
|
15
|
-
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
|
16
|
-
return fs.readFileSync(filePath, "utf-8");
|
|
17
|
-
}
|
|
18
|
-
} catch (_) {
|
|
19
|
-
// 不是文件路径,按原始字符串处理
|
|
20
|
-
}
|
|
21
|
-
return trimmed;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
process.
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
* publish 命令(子命令模式)
|
|
56
|
-
* @param {string} htmlCode - HTML 代码或文件路径
|
|
57
|
-
* @param {{ apiKey?: string, format?: string, title?: string }} options
|
|
58
|
-
*/
|
|
59
|
-
async function publishCommand(htmlCode, options = {}) {
|
|
60
|
-
const format = options.format || "json";
|
|
61
|
-
const resolvedKey = resolveApiKey(options.apiKey);
|
|
62
|
-
|
|
63
|
-
if (!resolvedKey) {
|
|
64
|
-
outputError(
|
|
65
|
-
{ message: "缺少 API Key。请通过 -k 参数、AICONDUCTOR_API_KEY 环境变量或 .env 文件提供。" },
|
|
66
|
-
format
|
|
67
|
-
);
|
|
68
|
-
process.exit(1);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
const code = resolveHtmlInput(htmlCode);
|
|
73
|
-
const result = await publishHtml(resolvedKey, code, options.title);
|
|
74
|
-
outputResult(result, format);
|
|
75
|
-
process.exit(0);
|
|
76
|
-
} catch (err) {
|
|
77
|
-
if (err.type === "http_error") {
|
|
78
|
-
outputError(
|
|
79
|
-
{ message: `API 返回错误 (HTTP ${err.statusCode}): ${err.body?.message || JSON.stringify(err.body)}` },
|
|
80
|
-
format
|
|
81
|
-
);
|
|
82
|
-
} else if (err.type === "network_error") {
|
|
83
|
-
outputError({ message: `网络错误: ${err.message}` }, format);
|
|
84
|
-
} else {
|
|
85
|
-
outputError({ message: `未知错误: ${err.message || err}` }, format);
|
|
86
|
-
}
|
|
87
|
-
process.exit(1);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
module.exports = { publishCommand };
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { publishHtml } = require("../api");
|
|
4
|
+
const { outputResult, outputError, resolveKeyOrExit, handleApiError } = require("../lib/cli-utils");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 智能解析输入:如果是已存在的文件路径则读取,否则当作原始 HTML 字符串
|
|
8
|
+
* @param {string} input
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
function resolveHtmlInput(input) {
|
|
12
|
+
const trimmed = input.trim();
|
|
13
|
+
try {
|
|
14
|
+
const filePath = path.resolve(trimmed);
|
|
15
|
+
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
|
16
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
17
|
+
}
|
|
18
|
+
} catch (_) {
|
|
19
|
+
// 不是文件路径,按原始字符串处理
|
|
20
|
+
}
|
|
21
|
+
return trimmed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** text 模式的自定义格式化 */
|
|
25
|
+
function formatPublishText(data) {
|
|
26
|
+
let out = "✓ 发布成功\n";
|
|
27
|
+
out += ` URL: ${data.online_url}\n`;
|
|
28
|
+
if (data.message) {
|
|
29
|
+
out += ` 信息: ${data.message}\n`;
|
|
30
|
+
}
|
|
31
|
+
return out.trimEnd();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* publish 命令(子命令模式)
|
|
36
|
+
* @param {string} htmlCode - HTML 代码或文件路径
|
|
37
|
+
* @param {{ apiKey?: string, format?: string, title?: string }} options
|
|
38
|
+
*/
|
|
39
|
+
async function publishCommand(htmlCode, options = {}) {
|
|
40
|
+
const format = options.format || "json";
|
|
41
|
+
const apiKey = resolveKeyOrExit(options.apiKey, format);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const code = resolveHtmlInput(htmlCode);
|
|
45
|
+
const result = await publishHtml(apiKey, code, options.title);
|
|
46
|
+
outputResult(result, format, formatPublishText);
|
|
47
|
+
process.exit(0);
|
|
48
|
+
} catch (err) {
|
|
49
|
+
handleApiError(err, format);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = { publishCommand };
|