Created
August 21, 2020 20:23
-
-
Save HowardvanRooijen/2231aff3f9e74679908bb209df0222a3 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
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