feat: add MCP creator screenshot metrics recognition
This commit is contained in:
@@ -254,6 +254,7 @@ async function invokeMcpTool(
|
||||
timeoutMs: number,
|
||||
name: string,
|
||||
args: Record<string, unknown>,
|
||||
allowText = false,
|
||||
): Promise<ToolResult> {
|
||||
const result = await postMcp(
|
||||
fetchImpl,
|
||||
@@ -277,6 +278,12 @@ async function invokeMcpTool(
|
||||
try {
|
||||
payload = JSON.parse(text);
|
||||
} catch {
|
||||
if (allowText) {
|
||||
return {
|
||||
isError: result.envelope?.result?.isError === true,
|
||||
payload: { text },
|
||||
};
|
||||
}
|
||||
if (result.envelope?.result?.isError === true) {
|
||||
return {
|
||||
isError: true,
|
||||
@@ -305,6 +312,7 @@ async function callMcpTool(
|
||||
timeoutMs: number,
|
||||
name: string,
|
||||
args: Record<string, unknown>,
|
||||
allowText = false,
|
||||
): Promise<ToolResult> {
|
||||
const nested = await invokeMcpTool(
|
||||
fetchImpl,
|
||||
@@ -313,6 +321,7 @@ async function callMcpTool(
|
||||
timeoutMs,
|
||||
name,
|
||||
{ request: args },
|
||||
allowText,
|
||||
);
|
||||
if (!isToolArgumentShapeError(nested)) return nested;
|
||||
return invokeMcpTool(
|
||||
@@ -322,10 +331,86 @@ async function callMcpTool(
|
||||
timeoutMs,
|
||||
name,
|
||||
args,
|
||||
allowText,
|
||||
);
|
||||
}
|
||||
|
||||
function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
||||
function imageToolText(value: unknown) {
|
||||
if (typeof value === "string") return value;
|
||||
const root = asRecord(value);
|
||||
if (!root) return "";
|
||||
return [root.text, root.description, root.content, root.message, root.error]
|
||||
.flatMap((item) => (Array.isArray(item) ? item : [item]))
|
||||
.map((item) => {
|
||||
if (typeof item === "string") return item;
|
||||
const record = asRecord(item);
|
||||
return String(record?.text ?? record?.description ?? "");
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function metricFromImageText(text: string, labels: string[], label: string) {
|
||||
const pattern = labels.join("|");
|
||||
const match = text.match(
|
||||
new RegExp(`(?:${pattern})\\s*[::]?\\s*([\\d,.]+(?:万|w|千|k)?)`, "i"),
|
||||
);
|
||||
if (!match) return null;
|
||||
try {
|
||||
return metricValue(match[1], label);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function creatorMetricsFromImageResult(result: ToolResult) {
|
||||
if (result.isError) {
|
||||
const detail = imageToolText(result.payload);
|
||||
throw new Error(
|
||||
`图片识别 MCP 调用失败${detail ? `:${safeMessage(detail, "")}` : ""}`,
|
||||
);
|
||||
}
|
||||
const text = imageToolText(result.payload);
|
||||
const exposure = metricFromImageText(
|
||||
text,
|
||||
["曝光量", "曝光", "impressions", "exposure"],
|
||||
"曝光量",
|
||||
);
|
||||
const views = metricFromImageText(
|
||||
text,
|
||||
["阅读量", "阅读", "views", "view_count", "view count"],
|
||||
"阅读量",
|
||||
);
|
||||
if (exposure === null || views === null) {
|
||||
throw new Error("图片识别未找到曝光量和阅读量");
|
||||
}
|
||||
return { exposure, views };
|
||||
}
|
||||
|
||||
export async function extractCreatorMetricsFromMcp(
|
||||
imageUrl: string,
|
||||
config: CollectionMcpConfig,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
) {
|
||||
const endpoint = buildMcpUrl(config);
|
||||
const timeoutMs = Math.max(5_000, config.timeoutMs ?? 30_000);
|
||||
const sessionId = await createMcpSession(fetchImpl, endpoint, timeoutMs);
|
||||
const result = await callMcpTool(
|
||||
fetchImpl,
|
||||
endpoint,
|
||||
sessionId,
|
||||
timeoutMs,
|
||||
"analyze_image",
|
||||
{ image_url: imageUrl },
|
||||
true,
|
||||
);
|
||||
return creatorMetricsFromImageResult(result);
|
||||
}
|
||||
|
||||
function metricsFromToolResult(
|
||||
result: ToolResult,
|
||||
toolName = "fetch_content_detail",
|
||||
): XhsPublicMetrics {
|
||||
const root = asRecord(result.payload);
|
||||
const response = asRecord(root?.response) ?? root;
|
||||
const data = asRecord(response?.data) ?? asRecord(root?.data);
|
||||
@@ -337,14 +422,18 @@ function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
||||
success === false ||
|
||||
(Number.isFinite(code) && code >= 400)
|
||||
) {
|
||||
const providerCode = Number(response?.code);
|
||||
const codeLabel = Number.isFinite(providerCode)
|
||||
? `(code ${providerCode})`
|
||||
: "";
|
||||
throw new Error(
|
||||
safeMessage(
|
||||
`MCP工具 ${toolName} 返回失败${codeLabel}:${safeMessage(
|
||||
response?.msg ?? response?.message ?? root?.message,
|
||||
"公开数据采集失败",
|
||||
),
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
if (!data) throw new Error("采集结果缺少互动数据");
|
||||
if (!data) throw new Error(`MCP工具 ${toolName} 未返回互动数据`);
|
||||
|
||||
const count = (value: unknown, label: string) =>
|
||||
value === null || value === undefined || value === ""
|
||||
@@ -1156,7 +1245,7 @@ async function collectInSession(
|
||||
auto_cookie: true,
|
||||
},
|
||||
);
|
||||
return metricsFromToolResult(primary);
|
||||
return metricsFromToolResult(primary, "fetch_content_detail");
|
||||
}
|
||||
|
||||
export function resolveCollectionMcpConfig(
|
||||
|
||||
Reference in New Issue
Block a user