The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RSSController.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using MarkdownDeep;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.ServiceModel.Syndication;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Xml.Linq;
  9. using Teknik.Areas.Blog.Models;
  10. using Teknik.Controllers;
  11. using Teknik.Filters;
  12. using Teknik.Helpers;
  13. using Teknik.Models;
  14. namespace Teknik.Areas.RSS.Controllers
  15. {
  16. public class RSSController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. [TrackPageView]
  20. [AllowAnonymous]
  21. public ActionResult Index()
  22. {
  23. SyndicationFeed feed = new SyndicationFeed("Teknik RSS", "RSS feeds for the Teknik Services.", new Uri(Url.SubRouteUrl("help", "Help.RSS")));
  24. return new RssResult(feed);
  25. }
  26. [TrackPageView]
  27. [AllowAnonymous]
  28. public ActionResult Blog(string username)
  29. {
  30. // If empty, grab the main blog
  31. Blog.Models.Blog blog = null;
  32. string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  33. string title = string.Empty;
  34. string description = string.Empty;
  35. bool isSystem = string.IsNullOrEmpty(username);
  36. if (isSystem)
  37. {
  38. blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.BlogId == Config.BlogConfig.ServerBlogId).FirstOrDefault();
  39. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  40. }
  41. else
  42. {
  43. blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.User.Username == username).FirstOrDefault();
  44. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
  45. }
  46. if (blog != null)
  47. {
  48. if (isSystem)
  49. {
  50. title = Config.BlogConfig.Title;
  51. description = Config.BlogConfig.Description;
  52. }
  53. else
  54. {
  55. title = blog.User.BlogSettings.Title;
  56. description = blog.User.BlogSettings.Description;
  57. }
  58. List<SyndicationItem> items = new List<SyndicationItem>();
  59. foreach (BlogPost post in blog.BlogPosts)
  60. {
  61. if (post.Published && post.System == isSystem)
  62. {
  63. items.Add(new SyndicationItem(
  64. post.Title,
  65. MarkdownHelper.Markdown(post.Article).ToHtmlString(),
  66. new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId })),
  67. post.BlogPostId.ToString(),
  68. post.DateEdited
  69. ));
  70. }
  71. }
  72. SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(blogUrl), items);
  73. return new RssResult(feed);
  74. }
  75. SyndicationFeed badFeed = new SyndicationFeed("No Blog Available", "The specified blog does not exist", new Uri(blogUrl));
  76. return new RssResult(badFeed);
  77. }
  78. [TrackPageView]
  79. [AllowAnonymous]
  80. public ActionResult Podcast()
  81. {
  82. List<SyndicationItem> items = new List<SyndicationItem>();
  83. List<Podcast.Models.Podcast> podcasts = db.Podcasts.Include("Files").Where(p => p.Published).ToList();
  84. if (podcasts != null)
  85. {
  86. foreach (Podcast.Models.Podcast podcast in podcasts)
  87. {
  88. SyndicationItem item = new SyndicationItem(
  89. podcast.Title,
  90. MarkdownHelper.Markdown(podcast.Description).ToHtmlString(),
  91. new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode })),
  92. podcast.Episode.ToString(),
  93. podcast.DateEdited
  94. );
  95. foreach (Podcast.Models.PodcastFile file in podcast.Files)
  96. {
  97. SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })), file.ContentType, file.ContentLength);
  98. item.Links.Add(enclosure);
  99. }
  100. items.Add(item);
  101. }
  102. }
  103. SyndicationFeed feed = new SyndicationFeed(Config.PodcastConfig.Title, Config.PodcastConfig.Description, new Uri(Url.SubRouteUrl("podcast", "Podcast.Index")), items);
  104. return new RssResult(feed);
  105. }
  106. }
  107. }