Skip to content

Instantly share code, notes, and snippets.

@m0sa
Created September 27, 2012 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0sa/3795672 to your computer and use it in GitHub Desktop.
Save m0sa/3795672 to your computer and use it in GitHub Desktop.
Syndication of Google+ articles and notes to an RSS feed
// the controler:
public class RssController : Controller
{
private readonly IGoogleSettings _settings;
public RssController(IGoogleSettings settings)
{
_settings = settings;
}
[OutputCache(Duration = 120)]
[HttpGet]
public ActionResult Index()
{
var client = new RestClient("https://www.googleapis.com/plus/v1");
var activityList = new RestRequest("/people/{userId}/activities/{collection}", Method.GET);
activityList.AddUrlSegment("collection", "public");
activityList.AddUrlSegment("userId", _settings.UserId);
activityList.AddParameter("key", _settings.ApiKey);
var response = client.Execute(activityList);
var content = JObject.Parse(response.Content);
var items = content["items"];
return new RssResult("m0sa.net Google+ RSS Feed", "m0sa.net Google+ RSS Feed", GetSyndicationItems(items));
}
private static IEnumerable<SyndicationItem> GetSyndicationItems(IEnumerable<JToken> items)
{
foreach (var item in items ?? new JToken[0])
{
var id = item["id"];
var published = item["published"];
var updated = item["updated"];
var @object = item["object"];
if (@object == null) continue;
var attachments = @object["attachments"];
if (attachments == null) continue;
var note = attachments.FirstOrDefault(x => x["objectType"].ToString() == "note");
var article = attachments.FirstOrDefault(x => x["objectType"].ToString() == "article");
var source = note ?? article;
if(source == null) continue;
var displayName = source["displayName"];
var content = source["content"];
var url = source["url"];
if(displayName == null && content == null) continue;
var rssItem = new SyndicationItem(displayName + "", content + "", new Uri(url + ""));
rssItem.PublishDate = (DateTime)(updated ?? published);
rssItem.Id = id + "";
yield return rssItem;
}
}
}
// utilizing System.ServiceModel.Syndication to create a feed:
public class RssResult : FileResult
{
private readonly SyndicationFeed _feed;
public RssResult(string title, string description, IEnumerable<SyndicationItem> feedItems)
: base("application/rss+xml")
{
_feed = new SyndicationFeed(title, description, HttpContext.Current.Request.Url) { Items = feedItems };
}
protected override void WriteFile(HttpResponseBase response)
{
using (var writer = XmlWriter.Create(response.OutputStream))
{
_feed.GetRss20Formatter().WriteTo(writer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment