-
Notifications
You must be signed in to change notification settings - Fork 221
Get All User Tweets
linvi edited this page Dec 15, 2016
·
4 revisions
Twitter allow developers to retrieve up to 3200 tweets. Because this operation is not straightforward the following code allow you to very simply perform this operation.
RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;
RateLimit.QueryAwaitingForRateLimit += (sender, args) =>
{
Console.WriteLine($"Query : {args.Query} is awaiting for rate limits!");
};
long userId = <YOUR_USER_ID>;
var lastTweets = Timeline.GetUserTimeline(userId, 200).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 timelineRequestParameters = new UserTimelineParameters
{
// MaxId ensures that we only get tweets that have been posted
// BEFORE the oldest tweet we received
MaxId = idOfOldestTweet - 1,
MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve
};
lastTweets = Timeline.GetUserTimeline(userId, timelineRequestParameters).ToArray();
allTweets.AddRange(lastTweets);
}
// `allTweets` now contains all the tweets that Twitter can return