Skip to content

Instantly share code, notes, and snippets.

@HowardvanRooijen
Created August 21, 2020 20:23
Show Gist options
  • Save HowardvanRooijen/2231aff3f9e74679908bb209df0222a3 to your computer and use it in GitHub Desktop.
Save HowardvanRooijen/2231aff3f9e74679908bb209df0222a3 to your computer and use it in GitHub Desktop.
public Task GenerateRssFeedAsync(SiteContext siteContext, IEnumerable<StructuredDataItem> items, string outputFilePath)
{
var feed = new RssFeed
{
Channel =
{
Link = new Uri(Flurl.Url.Combine(siteContext.SiteTemplateMetaData.Url, "feed", "rss.xml")),
Title = siteContext.SiteTemplateMetaData.Title,
Description = siteContext.SiteTemplateMetaData.Description,
LastBuildDate = DateTime.Now,
Language = CultureInfo.CurrentCulture,
Copyright = $"Endjin Limited {DateTime.Now.Year}"
}
};
foreach(var item in items.OrderByDescending(x => x.Date))
{
var rssItem = new RssItem
{
Author = item.Author.DisplayName,
Description = item.Excerpt,
Guid = new RssGuid(Flurl.Url.Combine(siteContext.SiteTemplateMetaData.Url, item.Url), true),
Link = new Uri(Flurl.Url.Combine(siteContext.SiteTemplateMetaData.Url, item.Url)),
PublicationDate = item.Date,
Title = item.Title,
};
var contentExtension = new SiteSummaryContentSyndicationExtension();
contentExtension.Context.Encoded = item.Content;
rssItem.AddExtension(contentExtension);
feed.Channel.AddItem(rssItem);
}
feed.Channel.Items = feed.Channel.Items.Take(50);
using (FileStream filestream = new FileStream(Path.Combine(outputFilePath, "rss.xml"), FileMode.Create))
{
var settings = new SyndicationResourceSaveSettings { CharacterEncoding = Encoding.UTF8 };
feed.Save(filestream, settings);
}
return Task.CompletedTask;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment