2

I'm reading tweets using the following code, it works fine but the media object is always empty even if the tweet has a picture but it works fine if the tweet has a video instead of picture!

            var stream = userClient.Streams.CreateFilteredStream();
            stream.TweetMode = Tweetinvi.TweetMode.Extended;
            var twitterUser = await userClient.Users.GetUserAsync(username);
            stream.AddFollow(twitterUser);
            stream.MatchingTweetReceived += (sender, eventReceived) =>
            {
                
                if(!eventReceived.Tweet.Retweeted)
                    Console.WriteLine(eventReceived.Tweet);
            };

            await stream.StartMatchingAllConditionsAsync();

I was debugging every tweets and verify that each one has a picture in twitter website.

This is the media object

Cassini
  • 119
  • 1
  • 13
  • What is the media object for you? 0 items? I am able to get this to work without issues, the only difference being I used `stream.AddTrack(string)` instead. – Timothy G. May 21 '21 at 23:05
  • @TimothyG. It's empty when the tweet has a picture, if the tweet has a video it works fine – Cassini May 23 '21 at 08:41

1 Answers1

1

The Media member of ITweet is a List<IMediaEntity>. It's possible for this list to be empty. Using the below code, I was able to receive tweets with their Media member containing IMediaEntity objects:

static async Task Main(string[] args)
{
    Task task = Task.Run(() => BeginTweetStream());
    await task;
}

public static async Task BeginTweetStream()
{
    string userName = "SomeName";
    int i = 0;
    TwitterClient userClient = new TwitterClient("string", "string", "string", "string"); //where the strings are credentials
    var stream = userClient.Streams.CreateFilteredStream();
    stream.TweetMode = Tweetinvi.TweetMode.Extended;
    var twitterUser = await userClient.Users.GetUserAsync(userName);
    stream.AddTrack("SomeTrack");
    stream.MatchingTweetReceived += (sender, eventReceived) =>
    {
        if (eventReceived.Tweet.Media != null && eventReceived.Tweet.Media.Any() && !eventReceived.Tweet.Retweeted)
        {
            Console.WriteLine($"Tweet with {eventReceived.Tweet.Media.Count()} media found!");

            foreach (Tweetinvi.Models.Entities.IMediaEntity media in eventReceived.Tweet.Media)
            {
                Console.WriteLine($"Media type: {media.MediaType} Link: {media.URL}");
            }

            ++i;
        }

        if (i == 99) //stop after 100 tweets with media
        {
            stream.Stop();
            Console.WriteLine("Complete! Press any key to exit.");
            Console.Read(); 
        }
    };

    await stream.StartMatchingAllConditionsAsync();
}

Console Output:

Output

You can check the IMediaEntity.MediaType member to see what kind of media it is. I haven't been able to find documentation of what values this can be set to, but so far, I've seen:

  • photo
  • video
  • animated_gif

Note that the URL member of the IMediaEntity object is the link to the tweet itself. If you are after the picture itself, MediaURL or MediaURLHttps will contain the link to only the picture (for videos and animated GIFs, these members are the link to the thumbnail picture instead).

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • I used your code exactly but I used "Khaberni" for AddTrack instead of "SomeTrack" I got the tweets but without their pictures! – Cassini May 23 '21 at 07:20
  • @Cassini I'm at a loss. So you aren't getting any links in the `IMediaEntity.MedialURL` for example? – Timothy G. May 23 '21 at 13:40
  • No, I'm not getting any link. – Cassini May 23 '21 at 16:59
  • @Cassini Can you post screenshots of your empty media object and tweet objects? – Timothy G. May 23 '21 at 17:12
  • I added the screenshot to the question. – Cassini May 23 '21 at 17:57
  • @Cassini I looked up the tweet using the ID in the screenshot, and it does not contain an image, but rather a [link to a website article](https://i.stack.imgur.com/DMf5D.png) that Twitter renders with a thumbnail, but that isn't technically a photo, at least not in the sense that it gets returned as part of the array of `IMediaEntity`. Here is a tweet from that account that **does** contain an image: https://twitter.com/khaberni/status/1396480461447249925 - [and Tweetinvi indicates this too](https://i.stack.imgur.com/ard0R.png) – Timothy G. May 23 '21 at 18:13
  • In other words, I think you've been examining tweets that you thought contained photos, but really they just contain a link to a news article/external website where Twitter renders a image from the new article itself into the thumbnail. And looking at what Tweetinvi provides, you can't get that photo out of the tweet. – Timothy G. May 23 '21 at 18:18
  • Here is an example of the tweet that I was reading https://twitter.com/khaberni/status/1396519075111739396 is that image is not a part of this tweet! – Cassini May 23 '21 at 18:34
  • @Cassini Again, that is not a "photo" in the tweet, not in the sense that it will be in the Media entity array. That is a thumbnail to a *link* that twitter renders, and the Twitter API does not provide those images back to users. In my previous comment, I [linked](https://twitter.com/khaberni/status/1396480461447249925) to a tweet that contains an actual photo that does get returned by the API. Your code as well as mine works, but the tweets you are looking at do not contain photos that are returned as part of the `IMediaEntity` array, hence why it has 0 items. – Timothy G. May 23 '21 at 18:38