22 October 2009 ~ Comments

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

  • Boy I'm glad I found this! I too am demoing Tweetsharp in one of my sessions for SharePoint Connections in Las Vegas next week. I built a simple sample app months ago and am just now prepping it again. Needless to say I was puzzled why it wasn't working, now I know! Thankfully it didn't require *any* code changes.
  • Hi Eric,

    Glad you found the post useful, and good luck at SharePoint Connections! Preview 17 is using the versioning API, which you might want to switch over to if you're worried about spontaneous API changes.

    Daniel
  • Thanks, I corrected the code.
  • Ron
    I found the Problem.
    In the second code, the last line in the 'while' should be:

    nextCursor = pagedResponse.AsNextCursor();


    Ron
  • Ron
    Your general work is amazing, thanks !

    There is one thing that I cannot understand..
    I want to receive the followers list of an account that has 50,000 followers.

    I'm using the seconds option (5,000 Id's on every call),
    and now (on debugging) I have 130,000 ID's and still running..

    It looks like running the loop (via F10 - debugging) could continue forever and generate unique & different positive ID's to add.
    (I checked the unique, different and positive ID's after writing them to a text file)

    I cannot understand this 130,000 ID's that I have, continue debugging will add more ID's (the loop continues).
    What is that ID's List ?

    This 130,000 ID's could be some of the account's followers ?
    (with only 50,000 followers)
    Maybe not at all ?

    Any suggestions ?


    Thanks,
    Ron
blog comments powered by Disqus