2026-04-20 09:52:48 +08:00
|
|
|
import {
|
|
|
|
|
fetchToutiaoMediaSnapshot,
|
|
|
|
|
publishToutiaoArticle,
|
|
|
|
|
} from "../../../../packages/publisher-platforms/src/toutiao";
|
|
|
|
|
|
2026-04-03 17:48:30 +08:00
|
|
|
import type { StoredPlatformState } from "../platforms";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
connectedPlatform,
|
|
|
|
|
disconnectedPlatform,
|
|
|
|
|
fetchImageBlob,
|
|
|
|
|
fetchJson,
|
|
|
|
|
manageUrlResult,
|
|
|
|
|
normalizeArticleHtml,
|
|
|
|
|
uploadHtmlImages,
|
|
|
|
|
} from "./common";
|
2026-04-20 09:52:48 +08:00
|
|
|
import type { AdapterContext, PlatformAdapter } from "./types";
|
2026-04-03 17:48:30 +08:00
|
|
|
|
|
|
|
|
async function detectToutiaohao(platform: StoredPlatformState): Promise<StoredPlatformState> {
|
|
|
|
|
try {
|
2026-04-20 09:52:48 +08:00
|
|
|
const media = await fetchToutiaoMediaSnapshot(fetchJson);
|
|
|
|
|
if (!media?.platformUid || !media.screenName) {
|
2026-04-03 17:48:30 +08:00
|
|
|
return disconnectedPlatform(platform, "未检测到头条号登录态");
|
|
|
|
|
}
|
2026-04-20 09:52:48 +08:00
|
|
|
return connectedPlatform(platform, media.platformUid, media.screenName, media.avatarUrl);
|
2026-04-03 17:48:30 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
return disconnectedPlatform(platform, error instanceof Error ? error.message : "头条号登录检测失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
async function publishToutiaohao(context: AdapterContext) {
|
|
|
|
|
const result = await publishToutiaoArticle(
|
2026-04-03 17:48:30 +08:00
|
|
|
{
|
2026-04-20 09:52:48 +08:00
|
|
|
title: context.article.title,
|
|
|
|
|
html: normalizeArticleHtml(context.article),
|
|
|
|
|
coverAssetUrl: context.article.cover_asset_url,
|
|
|
|
|
publishType: context.article.publish_type,
|
2026-04-03 17:48:30 +08:00
|
|
|
},
|
|
|
|
|
{
|
2026-04-20 09:52:48 +08:00
|
|
|
fetchJson,
|
|
|
|
|
fetchImageBlob,
|
|
|
|
|
uploadHtmlImages,
|
2026-04-03 17:48:30 +08:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
if (!result.success) {
|
|
|
|
|
throw new Error(result.message);
|
2026-04-03 17:48:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return manageUrlResult(
|
|
|
|
|
true,
|
2026-04-20 09:52:48 +08:00
|
|
|
result.status,
|
|
|
|
|
result.articleId,
|
|
|
|
|
result.externalManageUrl,
|
|
|
|
|
result.message,
|
|
|
|
|
result.externalArticleUrl,
|
2026-04-03 17:48:30 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const toutiaohaoAdapter: PlatformAdapter = {
|
|
|
|
|
platformId: "toutiaohao",
|
|
|
|
|
detect: detectToutiaohao,
|
|
|
|
|
publish: publishToutiaohao,
|
|
|
|
|
};
|