The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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

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