From 2e592291ca105dab2a6c8477c35044212eebed2f Mon Sep 17 00:00:00 2001 From: Pakorn Nathong Date: Thu, 2 Jan 2025 01:05:50 +0700 Subject: [PATCH] feat: add parse article fn --- src/timeline-v2.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/timeline-v2.ts b/src/timeline-v2.ts index 61bf4b8..592b4ff 100644 --- a/src/timeline-v2.ts +++ b/src/timeline-v2.ts @@ -423,3 +423,41 @@ export function parseThreadedConversation( return tweets; } + +export interface TimelineArticle { + id: string; + articleId: string; + title: string; + previewText: string; + coverMediaUrl?: string; + text: string; +} + +export function parseArticle( + conversation: ThreadedConversation, +): TimelineArticle[] { + const articles: TimelineArticle[] = []; + for (const instruction of conversation.data + ?.threaded_conversation_with_injections_v2?.instructions ?? []) { + for (const entry of instruction.entries ?? []) { + const id = entry.content?.itemContent?.tweet_results?.result?.rest_id; + const article = + entry.content?.itemContent?.tweet_results?.result?.article + ?.article_results?.result; + if (!id || !article) continue; + const text = + article.content_state?.blocks + ?.map((block) => block.text) + .join('\n\n') ?? ''; + articles.push({ + id, + articleId: article.id || '', + coverMediaUrl: article.cover_media?.media_info?.original_img_url, + previewText: article.preview_text || '', + text, + title: article.title || '', + }); + } + } + return articles; +}