From 2de2cb0dd805675c18738990cfaa3966ffbc1f0e Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sun, 2 Jul 2023 20:48:03 -0700 Subject: [PATCH] Remove media url from tweet --- app/bot/[username]/status/[id]/page.tsx | 9 ++++++++- lib/fetchTweet.ts | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/bot/[username]/status/[id]/page.tsx b/app/bot/[username]/status/[id]/page.tsx index bbfdacf..7cfd3ce 100644 --- a/app/bot/[username]/status/[id]/page.tsx +++ b/app/bot/[username]/status/[id]/page.tsx @@ -9,7 +9,14 @@ export async function generateMetadata({ params, }: PageProps): Promise { const { username, id } = params; - const { text, user, photos, video } = await fetchTweet(id); + let { text, user, photos, video, media } = await fetchTweet(id); + + if (media) { + media.map( + (item: any) => + (text = text.replace(new RegExp("\\s*" + item.url + "\\s*", "g"), "")) + ); + } const photoUrls: string[] = []; photos.map((photo) => photoUrls.push(photo.url)); diff --git a/lib/fetchTweet.ts b/lib/fetchTweet.ts index 6d62cec..492d269 100644 --- a/lib/fetchTweet.ts +++ b/lib/fetchTweet.ts @@ -3,11 +3,18 @@ export async function fetchTweet(id: string): Promise<{ user: any; photos: any[]; video: any | undefined; + media: any | undefined; }> { const baseUrl = "https://cdn.syndication.twimg.com/tweet-result"; const res = await fetch(`${baseUrl}?id=${id}`); const data = await res.json(); - const { text, user, photos, video } = data; - return { text, user, photos: photos ? photos : [], video }; + const { text, user, photos, video, entities } = data; + return { + text, + user, + photos: photos ? photos : [], + video, + media: entities.media, + }; }