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

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