feat: 完善视频任务与 KOC 资源库
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
collectXhsMetricsFromMcp,
|
||||
collectMetricsFromMcp,
|
||||
type CollectionMcpConfig,
|
||||
} from "./mcp-collection-client";
|
||||
import { uid } from "./mvp-db";
|
||||
@@ -10,6 +10,7 @@ type DistributionForCollection = {
|
||||
task_id: string;
|
||||
publish_url: string | null;
|
||||
ocr_status: string;
|
||||
platform: string;
|
||||
};
|
||||
|
||||
type ScheduledTask = {
|
||||
@@ -165,11 +166,16 @@ export async function collectDistributionMetrics(
|
||||
mcpConfig: CollectionMcpConfig,
|
||||
) {
|
||||
const current = await db
|
||||
.prepare("SELECT * FROM distributions WHERE id = ?")
|
||||
.prepare(
|
||||
`SELECT d.*, t.platform
|
||||
FROM distributions d
|
||||
JOIN tasks t ON t.id = d.task_id
|
||||
WHERE d.id = ?`,
|
||||
)
|
||||
.bind(distributionId)
|
||||
.first<DistributionForCollection>();
|
||||
if (!current) throw new Error("分发记录不存在");
|
||||
if (!current.publish_url) throw new Error("笔记尚未回填发布链接");
|
||||
if (!current.publish_url) throw new Error("作品尚未回填发布链接");
|
||||
|
||||
const scheduledAt = `${scheduledDate}T09:00:00+08:00`;
|
||||
const runId = uid("run");
|
||||
@@ -203,7 +209,12 @@ export async function collectDistributionMetrics(
|
||||
.bind(distributionId, scheduledDate)
|
||||
.first<{ id: string; status: string }>();
|
||||
if (!run) throw new Error("采集任务创建失败");
|
||||
if (run.status === "success") return { skipped: true };
|
||||
// Scheduled jobs should remain idempotent, but an operator clicking
|
||||
// “立即采集” is explicitly asking for a fresh snapshot. Reusing the same
|
||||
// daily run lets us correct stale or previously mis-mapped platform data.
|
||||
if (run.status === "success" && source !== "manual") {
|
||||
return { skipped: true };
|
||||
}
|
||||
|
||||
const collectingDescription =
|
||||
source === "automatic"
|
||||
@@ -235,8 +246,12 @@ export async function collectDistributionMetrics(
|
||||
]);
|
||||
|
||||
try {
|
||||
const { likes, comments, collects } =
|
||||
await collectXhsMetricsFromMcp(current.publish_url, mcpConfig);
|
||||
const { likes, comments, collects, shares } =
|
||||
await collectMetricsFromMcp(
|
||||
current.publish_url,
|
||||
current.platform === "抖音" ? "抖音" : "小红书",
|
||||
mcpConfig,
|
||||
);
|
||||
const dayWeight = scheduleDay ?? 1;
|
||||
const successDescription =
|
||||
source === "automatic"
|
||||
@@ -253,6 +268,7 @@ export async function collectDistributionMetrics(
|
||||
likes = ?,
|
||||
comments = ?,
|
||||
collects = ?,
|
||||
shares = ?,
|
||||
status_description = ?,
|
||||
completed_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
@@ -261,6 +277,7 @@ export async function collectDistributionMetrics(
|
||||
likes,
|
||||
comments,
|
||||
collects,
|
||||
shares,
|
||||
successDescription,
|
||||
run.id,
|
||||
),
|
||||
@@ -270,6 +287,7 @@ export async function collectDistributionMetrics(
|
||||
SET latest_likes = ?,
|
||||
latest_comments = ?,
|
||||
latest_collects = ?,
|
||||
latest_shares = ?,
|
||||
collection_status = 'success',
|
||||
collection_status_description = ?,
|
||||
collection_updated_at = CURRENT_TIMESTAMP,
|
||||
@@ -285,12 +303,13 @@ export async function collectDistributionMetrics(
|
||||
likes,
|
||||
comments,
|
||||
collects,
|
||||
shares,
|
||||
successDescription,
|
||||
scheduleDay,
|
||||
distributionId,
|
||||
),
|
||||
]);
|
||||
return { skipped: false, likes, comments, collects };
|
||||
return { skipped: false, likes, comments, collects, shares };
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "公开数据采集失败";
|
||||
|
||||
Reference in New Issue
Block a user