我是一个新的C#开发人员,我正在尝试开发一个使用Tweetinvi库来搜索Twitter的ASP.NET WebForms应用程序。我能够按用户和短语搜索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;
}
}
在过去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;
}