[Solution] Getting recent Posts

Oct 23, 2011 at 4:58 AM

The ApiCaller.GetPosts method allows for int? Offset  and int? Limit, but there are two other helpful options for paging.  You can get posts that have been created Since a given date/time, and only those created Until a date/time.  Here are changes that can be made which allow you to use these filters.

In ApiCaller, change the signature of GetPosts from this:

internal static IList<Post> GetPosts( string ID , string AccessToken , int? Offset , int? Limit )

to this:

internal static IList<Post> GetPosts( string ID , string AccessToken , int? Offset , int? Limit , DateTime? Since , DateTime? Until )

Now create a new method with the same signature as the original:

internal static IList<Post> GetPosts( string ID , string AccessToken , int? Offset , int? Limit )
{
   return GetPosts(ID , AccessToken , Offset , Limit , null , null);
}

 

So far the net result is that existing code won't break.  Now add this method which will allow for adding Since and/or Until to URLs for queries:

internal static string FormatConnectionURL( string Connection , string ID , string AccessToken , int? Offset , int? Limit , DateTime? Since , DateTime? Until )
{
    string URL = FormatConnectionURL(Connection , ID , AccessToken , Offset , Limit);
    if ( Since.HasValue )
        URL += "&since=" + Since.Value.ToUniversalTime().ToString("o");
    if ( Until.HasValue )
        URL += "&until=" + Until.Value.ToUniversalTime().ToString("o");
    return URL;
}

 

Finally, the new GetPosts with Since and Until need to pass those params to the new method above:

string URL = FormatConnectionURL("posts" , ID , AccessToken , Offset , Limit , Since , Until);

Now in your app code you can get all posts for some user in the last 8 hours:

IList<Post> posts = FB.User.GetPosts(theUser , Api.AccessToken , null , null , DateTime.Now.AddHours(-8) , null);

Or all posts between a period of days:

IList<Post> posts = FB.User.GetPosts(theUser , Api.AccessToken , null , null , DateTime.Now.AddDays(-7) , DateTime.Now.AddDays(-3));

I haven't checked all permutations.  If this works and people think it should go into the core, then I'll create a tracker item for it.

Note that I've found recent notes that said this functionality was broken in the Graph API, but it seems to be working for me tonight so I'm publishing this info.  Let's just be aware that this could be subject to breaking.