Skip to content

Commit

Permalink
Remove media url from tweet
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewStanciu committed Jul 3, 2023
1 parent 297528d commit 2de2cb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/bot/[username]/status/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ export async function generateMetadata({
params,
}: PageProps): Promise<Metadata> {
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));
Expand Down
11 changes: 9 additions & 2 deletions lib/fetchTweet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

0 comments on commit 2de2cb0

Please sign in to comment.