Tweetsharp Preview 16 – Cursors and Spam Reporting
TweetSharp Preview 16 is now released and includes support for Twitter’s new cursor-based pagination of friends and followers list, ID lists in the SocialGraph APIs, as well as the recently added spam reporting API. For details on the spam reporting API, you can see more on Jason’s blog here. The following code snippets will show you how to page through friends and followers using cursors.
var twitter = FluentTwitter.CreateRequest()
.AuthenticateAs(USERNAME, PASSWORD)
.Users()
.GetFriends()
.CreateCursor() //this will start paging and get the 1st page
.AsXml();
//make the call to twitter
var response = twitter.Request();
//get the first page of users from the response.
var friends = response.AsUsers();
//get the value of the next cursor page (a nullable 64-bit integer)
var nextCursor = response.AsNextCursor();
if ( nextCursor.HasValue && nextCursor.Value != 0 )
{
//get next page
twitter = FluentTwitter.CreateRequest()
.AuthenticateAs(USERNAME, PASSWORD)
.Users()
.GetFriends()
.GetCursor(nextCursor.Value)
.AsJson();
//make another request
var secondResponse = twitter.Request();
//get the users from the request
var secondCursorPage = secondResponse.AsUsers();
//get the id of the previous cursor page if you want to go back.
var previous = secondResponse.AsPreviousCursor();
}
Things to know:
- Currently the cursor pages for friends and followers are 100 users each (give or take – blocked users are eliminated on the fly, so you may get less than 100 on any given page).
- Cursor ids can be negative numbers. A return value of 0 from AsNextCursor() or AsPreviousCursor() indicates that there is no next or previous page, correspondingly.
- The SocialGraph methods that return IDs only return pages of 5000 ids at a time (or slightly less, if because of blocks)
- Passing an ID of -1 to the GetCursor() method has the same effect as calling CreateCursor()
- The old method of paging through users has been deprecated and will eventually stop working. The extension methods that use these APIs have been marked as obsolete, so if your code is using them you will see warnings when you compile against this preview.
The code for paging through IDs in your social graph is similar:
//Using a user with more than 5000 followers
var twitter = FluentTwitter.CreateRequest()
.AuthenticateAs(USERNAME, PASSWORD)
.SocialGraph().Ids().ForFollowersOf("hodgman")
.CreateCursor()
.AsXml();
var response = twitter.Request();
var nextCursor = response.AsNextCursor();
var ids = response.AsIds();
while (nextCursor.HasValue && nextCursor.Value != 0 )
{
var pagedTwitter = FluentTwitter.CreateRequest()
.AuthenticateAs(USERNAME, PASSWORD)
.SocialGraph().Ids().ForFollowersOf("hodgman")
.GetCursor(nextCursor.Value)
.AsJson();
var pagedResponse = pagedTwitter.Request();
ids.AddRange(pagedResponse.AsIds());
nextCursor = pagedResponse.AsNextCursor();
}
See also the Twitter API documentation:
Statuses/Friends
Statuses/Followers
SocialGraph/Friends/IDs
SocialGraph/Followers/IDs
