FixTweet / FxTwitter

Fix broken Twitter/X embeds! Use multiple images, videos, polls, translations and more on Discord, Telegram and others
https://fxtwitter.com
MIT License
2.62k stars 77 forks source link

how do I know the numbers of images in one tweet #222

Closed CurssedCoffin closed 1 year ago

CurssedCoffin commented 1 year ago

If the tweet has multiple images, how do I know exactly the number of images to parse?

dangeredwolf commented 1 year ago

In the FixTweet Status Fetch API, the photos are in the tweet.media.photos? object. If the Tweet has photos, this will be an array of the Tweet's photos, otherwise it will be undefined. The API does not explicitly have a photo count, as the length of the photos array indicates the number of photos.

In JavaScript for instance, tweet.media.photos?.length will return the photo count or undefined if no photos. To correct for this behavior, add || 0 to ensure it always returns a number. Here's an example in JS:

const data = await fetch("https://api.fxtwitter.com/TwitterDev/status/1561833980286173186").json();
const photoCount = data.tweet.media.photos?.length || 0;

console.log(photoCount)
> 2

I hope this helps!

CurssedCoffin commented 1 year ago

yes thanks!