-
Notifications
You must be signed in to change notification settings - Fork 221
Get All Followers Code
Muhammad Haseeb Asif edited this page Mar 30, 2016
·
6 revisions
Instead of retrieving all the followers in a single operation that could take ages; the following code demonstrates how to retrieve up to 75000
follower ids in a single operation and continue with the next batch of followers.
This give you the ability to not have to store millions of ids in a single collection.
Please note that this code uses Custom Queries through the TwitterAccessor
class. The reason is that this specific feature is not yet implemented in Tweetinvi. Thanks for your understanding.
The first part of the code demonstrates how to get up to 75000
follower ids safely.
private static IEnumerable<IIdsCursorQueryResultDTO> GetFollowerIds(string username, long cursor, out long nextCursor)
{
var query = string.Format("https://api.twitter.com/1.1/followers/ids.json?screen_name={0}", username);
// Ensure that we can get some information
RateLimit.AwaitForQueryRateLimit(query);
var results = TwitterAccessor.ExecuteCursorGETCursorQueryResult<IIdsCursorQueryResultDTO>(query, cursor : cursor).ToArray();
if (!results.Any())
{
// Something went wrong. The RateLimits operation tokens got used before we performed our query
RateLimit.ClearRateLimitCache();
RateLimit.AwaitForQueryRateLimit(query);
results = TwitterAccessor.ExecuteCursorGETCursorQueryResult<IIdsCursorQueryResultDTO>(query, cursor : cursor).ToArray();
}
if (results.Any())
{
nextCursor = results.Last().NextCursor;
}
else
{
nextCursor = -1;
}
return results;
}
The following code demonstrates how to use the previous method.
RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;
long nextCursor = -1;
do
{
var followerIds = GetFollowerIds("<user_screen_name>", nextCursor, out nextCursor);
// Your method to process the follower ids : ProcessFollowerIds(followerIds);
}
while (nextCursor != -1 && nextCursor != 0);