Created
May 20, 2012 22:51
-
-
Save gerardpaapu/2759830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Globalization; | |
using Facebook; | |
namespace PhosphorUsercontrols | |
{ | |
public class FacebookPostLink { | |
public string URL { get; set; } | |
public string Name { get; set; } | |
public string Caption { get; set; } | |
public string Description { get; set; } | |
public string HTML | |
{ | |
get | |
{ | |
return String.Format("<a href=\"{0}\" title=\"{1}\">{2}</a>\n<p>{3}</p>", URL, Caption, Name, Description); | |
} | |
} | |
} | |
public class FacebookPost | |
{ | |
// This maps very loosely to the schema described in: https://developers.facebook.com/docs/reference/api/post/ | |
// except I'm only keeping exactly what I need | |
public string GraphID { get; set; } | |
public string FromName { get; set; } | |
public string FromID { get; set; } | |
public string URL | |
{ | |
get | |
{ | |
var components = GraphID.Split(new char[] { '_' }); | |
var streamID = components[0]; | |
var itemID = components[1]; | |
return String.Format("http://facebook.com/{0}/posts/{1}", FromID, itemID); | |
} | |
} | |
public string StreamURL | |
{ | |
get | |
{ | |
return String.Format("http://facebook.com/{0}", FromID); | |
} | |
} | |
public string Message { get; set; } | |
public FacebookPostLink Link { get; set; } | |
public string Picture { get; set; } | |
public string Type { get; set; } | |
public DateTimeOffset CreatedTime { get; set; } | |
public static FacebookPost FromJSON(dynamic json) | |
{ | |
FacebookPostLink link = null; | |
if (json.link != null) | |
{ | |
link = new FacebookPostLink | |
{ | |
URL = json.link, | |
Name = json.name, | |
Caption = json.caption, | |
Description = json.description | |
}; | |
} | |
return new FacebookPost | |
{ | |
GraphID = json.id, | |
FromName = json.from != null ? json.from.name : "anonymous", | |
FromID = json.from != null ? json.from.id : null, | |
Message = json.message ?? "", | |
Link = link, | |
Picture = json.picture, | |
Type = json.type, | |
CreatedTime = DateTimeOffset.Parse(json.created_time) | |
}; | |
} | |
public static IEnumerable<FacebookPost> ParseFeed(dynamic json) | |
{ | |
var list = json.data as List<dynamic>; | |
return list.Select<dynamic, FacebookPost>(item => FacebookPost.FromJSON(item)); | |
} | |
public static IEnumerable<FacebookPost> FetchFeed(string url, string accessToken) | |
{ | |
var client = new FacebookClient(accessToken); | |
var json = client.Get(url); | |
return FacebookPost.ParseFeed(json); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment