简体   繁体   English

TweetInvi 不检索推文的图片

[英]TweetInvi does not retrieve tweet's picture

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!我正在使用以下代码阅读推文,它工作正常,但媒体 object 始终为空,即使推文有图片,但如果推文有视频而不是图片,它工作正常!

            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.我正在调试每条推文,并验证每条推文在 twitter 网站上都有一张图片。

这是媒体对象

The Media member of ITweet is a List<IMediaEntity> . ITweetMedia成员是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:使用下面的代码,我能够接收到他们的Media成员包含IMediaEntity对象的推文:

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.您可以检查IMediaEntity.MediaType成员以查看它是哪种媒体。 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.请注意, URL的成员IMediaEntity是推文本身的链接。 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).如果您关注图片本身, MediaURLMediaURLHttps将只包含指向图片的链接(对于视频和动画 GIF,这些成员是指向缩略图的链接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM