aiconductor-cli 1.2.1 → 1.2.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.
@@ -1,151 +1,109 @@
1
- const {
2
- resolveApiKey,
3
- douyinVideoInfo,
4
- douyinVideoComments,
5
- douyinUserVideos,
6
- douyinUserDetail,
7
- douyinUserSearch,
8
- douyinVideoSearch,
9
- douyinLinkToText,
10
- } = require("../api");
11
-
12
- function outputResult(data, format) {
13
- if (format === "json") {
14
- process.stdout.write(JSON.stringify({ success: true, data }, null, 2) + "\n");
15
- } else {
16
- process.stdout.write("\u2713 \u64cd\u4f5c\u6210\u529f\n");
17
- process.stdout.write(JSON.stringify(data, null, 2) + "\n");
18
- }
19
- }
20
-
21
- function outputError(err, format) {
22
- if (format === "json") {
23
- process.stderr.write(JSON.stringify({ success: false, error: err.message || String(err) }, null, 2) + "\n");
24
- } else {
25
- process.stderr.write("\u2717 \u9519\u8bef: " + (err.message || err) + "\n");
26
- }
27
- }
28
-
29
- function resolveKey(optionKey, format) {
30
- const key = resolveApiKey(optionKey);
31
- if (!key) {
32
- outputError(
33
- { message: "\u7f3a\u5c11 API Key\u3002\u8bf7\u901a\u8fc7 -k \u53c2\u6570\u3001AICONDUCTOR_API_KEY \u73af\u5883\u53d8\u91cf\u6216 .env \u6587\u4ef6\u63d0\u4f9b\u3002" },
34
- format
35
- );
36
- process.exit(1);
37
- }
38
- return key;
39
- }
40
-
41
- function handleError(err, format) {
42
- if (err.type === "http_error") {
43
- outputError(
44
- { message: "API \u8fd4\u56de\u9519\u8bef (HTTP " + err.statusCode + "): " + (err.body?.message || JSON.stringify(err.body)) },
45
- format
46
- );
47
- } else if (err.type === "network_error") {
48
- outputError({ message: "\u7f51\u7edc\u9519\u8bef: " + err.message }, format);
49
- } else {
50
- outputError({ message: "\u672a\u77e5\u9519\u8bef: " + (err.message || err) }, format);
51
- }
52
- }
53
-
54
- async function videoInfoCommand(shareUrl, options = {}) {
55
- const format = options.format || "json";
56
- const key = resolveKey(options.apiKey, format);
57
- try {
58
- const result = await douyinVideoInfo(key, shareUrl);
59
- outputResult(result, format);
60
- process.exit(0);
61
- } catch (err) { handleError(err, format); process.exit(1); }
62
- }
63
-
64
- async function videoCommentsCommand(videoUrl, options = {}) {
65
- const format = options.format || "json";
66
- const key = resolveKey(options.apiKey, format);
67
- try {
68
- const maxCount = options.maxCount ? parseInt(options.maxCount, 10) : undefined;
69
- const result = await douyinVideoComments(key, videoUrl, maxCount);
70
- outputResult(result, format);
71
- process.exit(0);
72
- } catch (err) { handleError(err, format); process.exit(1); }
73
- }
74
-
75
- async function userVideosCommand(homePage, options = {}) {
76
- const format = options.format || "json";
77
- const key = resolveKey(options.apiKey, format);
78
- try {
79
- const maxSeconds = options.maxSeconds ? parseInt(options.maxSeconds, 10) : undefined;
80
- const maxPages = options.maxPages ? parseInt(options.maxPages, 10) : undefined;
81
- const result = await douyinUserVideos(key, homePage, maxSeconds, maxPages);
82
- outputResult(result, format);
83
- process.exit(0);
84
- } catch (err) { handleError(err, format); process.exit(1); }
85
- }
86
-
87
- async function userDetailCommand(homePage, options = {}) {
88
- const format = options.format || "json";
89
- const key = resolveKey(options.apiKey, format);
90
- try {
91
- const result = await douyinUserDetail(key, homePage);
92
- outputResult(result, format);
93
- process.exit(0);
94
- } catch (err) { handleError(err, format); process.exit(1); }
95
- }
96
-
97
- async function userSearchCommand(keyword, options = {}) {
98
- const format = options.format || "json";
99
- const key = resolveKey(options.apiKey, format);
100
- try {
101
- const maxPages = options.maxPages ? parseInt(options.maxPages, 10) : undefined;
102
- const maxSeconds = options.maxSeconds ? parseInt(options.maxSeconds, 10) : undefined;
103
- const result = await douyinUserSearch(key, keyword, {
104
- maxPages,
105
- maxSeconds,
106
- fansRange: options.fansRange,
107
- userType: options.userType,
108
- });
109
- outputResult(result, format);
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 { resolveApiKey, publishHtml } = require("../api");
4
-
5
- /**
6
- * 智能解析输入:如果是已存在的文件路径则读取,否则当作原始 HTML 字符串
7
- * @param {string} input
8
- * @returns {string}
9
- */
10
- function resolveHtmlInput(input) {
11
- const trimmed = input.trim();
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
- * @param {object} data
27
- * @param {"json"|"text"} format
28
- */
29
- function outputResult(data, format) {
30
- if (format === "json") {
31
- process.stdout.write(JSON.stringify({ success: true, data }, null, 2) + "\n");
32
- } else {
33
- process.stdout.write(`✓ 发布成功\n`);
34
- process.stdout.write(` URL: ${data.online_url}\n`);
35
- if (data.message) {
36
- process.stdout.write(` 信息: ${data.message}\n`);
37
- }
38
- }
39
- }
40
-
41
- /**
42
- * 输出错误
43
- * @param {object} err
44
- * @param {"json"|"text"} format
45
- */
46
- function outputError(err, format) {
47
- if (format === "json") {
48
- process.stderr.write(JSON.stringify({ success: false, error: err.message || String(err) }, null, 2) + "\n");
49
- } else {
50
- process.stderr.write(`✗ 错误: ${err.message || err}\n`);
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 };
@@ -1,118 +1,87 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const { resolveApiKey, publishHtml, publishMdToHtml } = require("../api");
4
-
5
- /**
6
- * 解析输入内容:如果是已存在的文件路径则读取,否则当作原始字符串
7
- * @param {string} input
8
- * @returns {{ content: string, filePath: string|null }}
9
- */
10
- function resolveInput(input) {
11
- const trimmed = input.trim();
12
- try {
13
- const filePath = path.resolve(trimmed);
14
- if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
15
- return { content: fs.readFileSync(filePath, "utf-8"), filePath };
16
- }
17
- } catch (_) {}
18
- return { content: trimmed, filePath: null };
19
- }
20
-
21
- /**
22
- * 自动检测内容类型:Markdown 或 HTML
23
- * @param {string} content
24
- * @param {string|null} filePath
25
- * @returns {"md"|"html"}
26
- */
27
- function detectType(content, filePath) {
28
- // 优先用文件扩展名判断
29
- if (filePath) {
30
- const ext = path.extname(filePath).toLowerCase();
31
- if (ext === ".md" || ext === ".markdown") return "md";
32
- if (ext === ".html" || ext === ".htm") return "html";
33
- }
34
- // 内容特征判断
35
- const trimmed = content.trim();
36
- if (trimmed.startsWith("<!DOCTYPE") || trimmed.startsWith("<html") || trimmed.startsWith("<head")) {
37
- return "html";
38
- }
39
- if (trimmed.startsWith("<") && /<\w+[^>]*>/.test(trimmed.substring(0, 200))) {
40
- return "html";
41
- }
42
- // 默认 Markdown
43
- return "md";
44
- }
45
-
46
- function outputResult(data, format) {
47
- if (format === "json") {
48
- process.stdout.write(JSON.stringify({ success: true, data }, null, 2) + "\n");
49
- } else {
50
- process.stdout.write(`✓ 发布成功\n`);
51
- process.stdout.write(` URL: ${data.online_url}\n`);
52
- if (data.message) {
53
- process.stdout.write(` 信息: ${data.message}\n`);
54
- }
55
- }
56
- }
57
-
58
- function outputError(err, format) {
59
- if (format === "json") {
60
- process.stderr.write(JSON.stringify({ success: false, error: err.message || String(err) }, null, 2) + "\n");
61
- } else {
62
- process.stderr.write(`✗ 错误: ${err.message || err}\n`);
63
- }
64
- }
65
-
66
- /**
67
- * webpage publish 命令 — 统一发布 Markdown 或 HTML 为在线网页
68
- * @param {string} input - Markdown/HTML 代码或文件路径
69
- * @param {{ apiKey?: string, format?: string, title?: string, copyButton?: boolean, type?: string }} options
70
- */
71
- async function publishCommand(input, options = {}) {
72
- const format = options.format || "json";
73
- const resolvedKey = resolveApiKey(options.apiKey);
74
-
75
- if (!resolvedKey) {
76
- outputError(
77
- { message: "缺少 API Key。请通过 -k 参数、AICONDUCTOR_API_KEY 环境变量或 .env 文件提供。" },
78
- format
79
- );
80
- process.exit(1);
81
- }
82
-
83
- try {
84
- const { content, filePath } = resolveInput(input);
85
- // 确定类型:--type 显式指定优先,否则自动检测
86
- let type = options.type;
87
- if (!type) {
88
- type = detectType(content, filePath);
89
- } else if (type !== "md" && type !== "html") {
90
- outputError({ message: `无效的 --type 值: "${type}",可选: md 或 html` }, format);
91
- process.exit(1);
92
- }
93
-
94
- let result;
95
- if (type === "html") {
96
- result = await publishHtml(resolvedKey, content, options.title);
97
- } else {
98
- const addCopyButton = options.copyButton === true;
99
- result = await publishMdToHtml(resolvedKey, content, options.title, addCopyButton);
100
- }
101
- outputResult(result, format);
102
- process.exit(0);
103
- } catch (err) {
104
- if (err.type === "http_error") {
105
- outputError(
106
- { message: `API 返回错误 (HTTP ${err.statusCode}): ${err.body?.message || JSON.stringify(err.body)}` },
107
- format
108
- );
109
- } else if (err.type === "network_error") {
110
- outputError({ message: `网络错误: ${err.message}` }, format);
111
- } else {
112
- outputError({ message: `未知错误: ${err.message || err}` }, format);
113
- }
114
- process.exit(1);
115
- }
116
- }
117
-
118
- module.exports = { publishCommand };
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { publishHtml, publishMdToHtml } = require("../api");
4
+ const { outputResult, outputError, resolveKeyOrExit, handleApiError } = require("../lib/cli-utils");
5
+
6
+ /**
7
+ * 解析输入内容:如果是已存在的文件路径则读取,否则当作原始字符串
8
+ * @param {string} input
9
+ * @returns {{ content: string, filePath: string|null }}
10
+ */
11
+ function resolveInput(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 { content: fs.readFileSync(filePath, "utf-8"), filePath };
17
+ }
18
+ } catch (_) {}
19
+ return { content: trimmed, filePath: null };
20
+ }
21
+
22
+ /**
23
+ * 自动检测内容类型:Markdown 或 HTML
24
+ * @param {string} content
25
+ * @param {string|null} filePath
26
+ * @returns {"md"|"html"}
27
+ */
28
+ function detectType(content, filePath) {
29
+ if (filePath) {
30
+ const ext = path.extname(filePath).toLowerCase();
31
+ if (ext === ".md" || ext === ".markdown") return "md";
32
+ if (ext === ".html" || ext === ".htm") return "html";
33
+ }
34
+ const trimmed = content.trim();
35
+ if (trimmed.startsWith("<!DOCTYPE") || trimmed.startsWith("<html") || trimmed.startsWith("<head")) {
36
+ return "html";
37
+ }
38
+ if (trimmed.startsWith("<") && /<\w+[^>]*>/.test(trimmed.substring(0, 200))) {
39
+ return "html";
40
+ }
41
+ return "md";
42
+ }
43
+
44
+ /** text 模式的自定义格式化 */
45
+ function formatPublishText(data) {
46
+ let out = "✓ 发布成功\n";
47
+ out += ` URL: ${data.online_url}\n`;
48
+ if (data.message) {
49
+ out += ` 信息: ${data.message}\n`;
50
+ }
51
+ return out.trimEnd();
52
+ }
53
+
54
+ /**
55
+ * webpage publish 命令 — 统一发布 Markdown 或 HTML 为在线网页
56
+ * @param {string} input - Markdown/HTML 代码或文件路径
57
+ * @param {{ apiKey?: string, format?: string, title?: string, copyButton?: boolean, type?: string }} options
58
+ */
59
+ async function publishCommand(input, options = {}) {
60
+ const format = options.format || "json";
61
+ const apiKey = resolveKeyOrExit(options.apiKey, format);
62
+
63
+ try {
64
+ const { content, filePath } = resolveInput(input);
65
+ let type = options.type;
66
+ if (!type) {
67
+ type = detectType(content, filePath);
68
+ } else if (type !== "md" && type !== "html") {
69
+ outputError({ message: `无效的 --type 值: "${type}",可选: md 或 html` }, format);
70
+ process.exit(1);
71
+ }
72
+
73
+ let result;
74
+ if (type === "html") {
75
+ result = await publishHtml(apiKey, content, options.title);
76
+ } else {
77
+ result = await publishMdToHtml(apiKey, content, options.title, options.copyButton === true);
78
+ }
79
+ outputResult(result, format, formatPublishText);
80
+ process.exit(0);
81
+ } catch (err) {
82
+ handleApiError(err, format);
83
+ process.exit(1);
84
+ }
85
+ }
86
+
87
+ module.exports = { publishCommand };