微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TweetInvi 不检索推文的图片

如何解决TweetInvi 不检索推文的图片

我正在使用以下代码阅读推文,它工作正常,但即使推文有图片,媒体对象也始终为空,但如果推文有视频而不是图片,则效果很好!

            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();

我正在调试每条推文,并确认每条推文在 Twitter 网站上都有一张图片

This is the media object

解决方法

MediaITweet 成员是 List<IMediaEntity>。此列表可能为空。使用以下代码,我能够接收包含 IMediaEntity 对象的 Media 成员的推文:

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"); //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();
}

控制台输出:

Output

您可以查看 IMediaEntity.MediaType 成员以了解它是什么类型的媒体。我一直无法找到有关可以将其设置为哪些值的文档,但到目前为止,我已经看到:

  • photo
  • video
  • animated_gif

请注意,URL 对象的 IMediaEntity 成员是推文本身的链接。如果您要查找图片本身,MediaURLMediaURLHttps 将仅包含指向图片的链接(对于视频和动画 GIF,这些成员是指向缩略图的链接)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。