Reputation:
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.
Upvotes: 2
Views: 345
Reputation: 9027
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:
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).
Upvotes: 0