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.7KB

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