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

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