3

我是一名新的 C# 开发人员,我正在尝试开发一个 ASP.NET Webforms 应用程序来使用 Tweetinvi 库搜索 Twitter。我能够按用户和短语搜索 Twitter。但是,我现在正在努力按短语搜索推文,因为它只返回少量低于 150 条推文的推文,我不知道为什么。

我能够按用户检索超过 1700 条推文,但是当我在按短语搜索时应用相同的代码概念时我只能获得 150 条推文,我不知道为什么。

请注意,按短语搜索推文中的短语可以是任何关键字、主题标签或 Twitter 帐户名称

这是按短语搜索推文的 C# 代码:

private List<ITweet> GetFilteredTweets(string keyword)
        {
            try
            {
                RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;

                RateLimit.QueryAwaitingForRateLimit += (se, ar) =>
                {
                    Console.WriteLine($"Query : {ar.Query} is awaiting for rate limits!");
                };

                // Create a parameter for queries with specific parameters
                var searchParameters = new SearchTweetsParameters(keyword)
                {
                    Lang = LanguageFilter.English,
                    SearchType = SearchResultType.Mixed,
                    //MaximumNumberOfResults = 100,
                    MaximumNumberOfResults = 200,
                    TweetSearchType = TweetSearchType.OriginalTweetsOnly
                };

                var lastTweets = Tweetinvi.Search.SearchTweets(searchParameters).ToArray();

                var allTweets = new List<ITweet>(lastTweets);
                var beforeLast = allTweets;

                while (lastTweets.Length > 0 && allTweets.Count <= 3200)
                {
                    var idOfOldestTweet = lastTweets.Select(x => x.Id).Min();
                    Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}");

                    var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200;
                    var searchRequestParameters = new SearchTweetsParameters(keyword)
                    {
                        Lang = LanguageFilter.English,
                        SearchType = SearchResultType.Mixed,
                        // MaxId ensures that we only get tweets that have been posted 
                        // BEFORE the oldest tweet we received
                        MaxId = idOfOldestTweet - 1,
                        MaximumNumberOfResults = numberOfTweetsToRetrieve,
                        TweetSearchType = TweetSearchType.OriginalTweetsOnly
                    };

                    lastTweets = Tweetinvi.Search.SearchTweets(searchRequestParameters).ToArray();
                    allTweets.AddRange(lastTweets);
                }
                // `allTweets` now contains all the tweets that Twitter can return
                tweetList = allTweets;
                return allTweets;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
4

1 回答 1

0

获取过去 7 天内的所有推文。

    static public List<ITweet> GetTweets(string keyword)
    {
        RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;
        var searchParam = new Tweetinvi.Parameters.SearchTweetsParameters(keyword)
        {
            MaximumNumberOfResults = 1000
        };

        var tweets = Tweetinvi.Search.SearchTweets(searchParam).ToList();
        var tweetList = new List<ITweet>(tweets);

        while (tweets.Count > 0)
        {
            searchParam.MaxId = tweets.Select(x => x.Id).Min() - 1;
            searchParam.MaximumNumberOfResults = 5000;
            tweets = Tweetinvi.Search.SearchTweets(searchParam).ToList();
            tweetList.AddRange(tweets);

            Console.WriteLine($"Find : {tweets.Count}");
            Console.WriteLine($"All : {tweetList.Count}");
        }
        return tweetList;
    }
于 2018-10-01T12:50:34.917 回答