2026-04-20 09:52:48 +08:00
|
|
|
import {
|
|
|
|
|
publishToutiaoArticle,
|
|
|
|
|
} from "../../../../../packages/publisher-platforms/src/toutiao";
|
2026-04-19 14:18:20 +08:00
|
|
|
import type { JsonValue } from "@geo/shared-types";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
fetchImageBlob,
|
|
|
|
|
normalizeArticleHtml,
|
|
|
|
|
sessionFetchJson,
|
|
|
|
|
uploadHtmlImages,
|
|
|
|
|
} from "./common";
|
|
|
|
|
import type { PublishAdapter } from "./base";
|
|
|
|
|
|
|
|
|
|
function buildResultPayload(
|
|
|
|
|
articleId: string,
|
|
|
|
|
mediaName: string,
|
2026-04-20 09:52:48 +08:00
|
|
|
externalManageUrl: string,
|
|
|
|
|
externalArticleUrl: string | null,
|
2026-04-19 14:18:20 +08:00
|
|
|
): Record<string, JsonValue> {
|
|
|
|
|
return {
|
|
|
|
|
platform: "toutiaohao",
|
|
|
|
|
media_name: mediaName,
|
|
|
|
|
external_article_id: articleId,
|
2026-04-20 09:52:48 +08:00
|
|
|
external_manage_url: externalManageUrl,
|
|
|
|
|
external_article_url: externalArticleUrl,
|
|
|
|
|
publish_type: "publish",
|
2026-04-19 14:18:20 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const toutiaoAdapter: PublishAdapter = {
|
|
|
|
|
platform: "toutiaohao",
|
2026-04-20 09:52:48 +08:00
|
|
|
executionMode: "session",
|
2026-04-19 14:18:20 +08:00
|
|
|
async publish(context) {
|
|
|
|
|
context.reportProgress("toutiao.normalize_html");
|
|
|
|
|
const html = normalizeArticleHtml(context.article);
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
const result = await publishToutiaoArticle(
|
|
|
|
|
{
|
|
|
|
|
title: context.article.title,
|
|
|
|
|
html,
|
|
|
|
|
coverAssetUrl: context.article.cover_asset_url,
|
|
|
|
|
publishType: "publish",
|
|
|
|
|
},
|
2026-04-19 14:18:20 +08:00
|
|
|
{
|
2026-04-20 09:52:48 +08:00
|
|
|
fetchJson: (input, init) => sessionFetchJson(context.session, input, init),
|
|
|
|
|
fetchImageBlob,
|
|
|
|
|
uploadHtmlImages,
|
|
|
|
|
reportProgress(stage) {
|
|
|
|
|
context.reportProgress(`toutiao.${stage}`);
|
2026-04-19 14:18:20 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
if (!result.success) {
|
|
|
|
|
const summary =
|
|
|
|
|
result.code === "toutiaohao_not_logged_in"
|
|
|
|
|
? "头条号登录态失效,无法执行发布。"
|
|
|
|
|
: result.code === "article_content_empty"
|
|
|
|
|
? "文章内容为空,无法推送到头条号。"
|
|
|
|
|
: result.message || "头条号发布失败。";
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
return {
|
|
|
|
|
status: "failed",
|
2026-04-20 09:52:48 +08:00
|
|
|
summary,
|
2026-04-19 14:18:20 +08:00
|
|
|
error: {
|
2026-04-20 09:52:48 +08:00
|
|
|
code: result.code,
|
|
|
|
|
message: result.message,
|
2026-04-19 14:18:20 +08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
const payload = buildResultPayload(
|
|
|
|
|
result.articleId,
|
|
|
|
|
result.mediaName,
|
|
|
|
|
result.externalManageUrl,
|
|
|
|
|
result.externalArticleUrl,
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
return {
|
|
|
|
|
status: "succeeded",
|
|
|
|
|
payload,
|
2026-04-20 09:52:48 +08:00
|
|
|
summary: "头条号发布成功。",
|
2026-04-19 14:18:20 +08:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|