Navigation Menu

Skip to content

Commit

Permalink
Add RSS feed handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulaBean committed Oct 16, 2014
1 parent 493a63f commit 134af1b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
6 changes: 6 additions & 0 deletions TheDailyWtf/App_Start/RouteConfig.cs
Expand Up @@ -27,6 +27,12 @@ public static void RegisterRoutes(RouteCollection routes)
defaults: new { controller = "Admin", action = "EditSeries", slug = UrlParameter.Optional }
);

routes.MapRoute(
name: "Rss",
url: "rss",
defaults: new { controller = "Home", action = "Rss" }
);

routes.MapRoute(
name: "Contact",
url: "contact",
Expand Down
65 changes: 65 additions & 0 deletions TheDailyWtf/Common/ActionResults/RssArticlesResult.cs
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using TheDailyWtf.Models;

namespace TheDailyWtf
{
public sealed class RssArticlesResult : ActionResult
{
private readonly IEnumerable<ArticleModel> articles;

public RssArticlesResult(IEnumerable<ArticleModel> articles)
{
if (articles == null)
throw new ArgumentNullException("articles");

this.articles = articles;
}

public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "application/rss+xml";

var dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
var slash = XNamespace.Get("http://purl.org/rss/1.0/modules/slash/");
var wfw = XNamespace.Get("http://wellformedweb.org/CommentAPI/");

var xdoc = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "dc", dc),
new XAttribute(XNamespace.Xmlns + "slash", slash),
new XAttribute(XNamespace.Xmlns + "wfw", wfw),
new XElement("channel",
new XElement("title", "The Daily WTF"),
new XElement("link", "http://thedailywtf.com/"),
new XElement("description", "Curious Perversions in Information Technology"),
this.articles.Select(a => new XElement("item",
new XElement("author", a.Author.Name),
new XElement("title", a.RssTitle),
new XElement("link", a.Url),
new XElement("category", a.Series.Title),
new XElement("pubDate", a.PublishedDate.Value.ToUniversalTime().ToString("r")),
new XElement("guid", a.Id),
new XElement("description", ""),
new XElement(slash + "comments", a.CoalescedCommentCount),
new XElement("comments", a.CommentsUrl)
))
)
)
);

using (var writer = XmlWriter.Create(response.OutputStream, new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = false, NewLineChars = "" }))
{
xdoc.WriteTo(writer);
}
}
}
}
9 changes: 9 additions & 0 deletions TheDailyWtf/Controllers/HomeController.cs
Expand Up @@ -36,6 +36,15 @@ public ActionResult Sponsors()
return View(new HomeIndexViewModel());
}

[OutputCache(CacheProfile = CacheProfile.Timed5Minutes, VaryByParam = "*")]
public ActionResult Rss()
{
if (Request.QueryString["fbsrc"] != "Y" && Request.QueryString["sneak"] != "Y")
return new RedirectResult("http://syndication.thedailywtf.com/TheDailyWtf", false);

return new RssArticlesResult(ArticleModel.GetRecentArticles(15));
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Contact(ContactFormViewModel model)
Expand Down
22 changes: 19 additions & 3 deletions TheDailyWtf/Models/ArticleModel.cs
Expand Up @@ -24,8 +24,19 @@ public ArticleModel()
[AllowHtml]
public string BodyHtml { get; set; }
public string Title { get; set; }
public string RssTitle
{
get
{
if (this.Series.Title.Equals("Feature Articles", StringComparison.OrdinalIgnoreCase))
return HttpUtility.HtmlEncode(this.Title);
else
return HttpUtility.HtmlEncode(string.Format("{0}: {1}", this.Series.Title, this.Title));
}
}
public int DiscourseCommentCount { get; set; }
public int CachedCommentCount { get; set; }
public int CoalescedCommentCount { get { return Math.Max(this.DiscourseCommentCount, this.CachedCommentCount); } }
public DateTime? LastCommentDate { get; set; }
public string LastCommentDateDescription
{
Expand All @@ -43,8 +54,8 @@ public string LastCommentDateDescription
public string DiscourseThreadUrl { get { return string.Format("http://what.thedailywtf.com/t/{0}/{1}", this.DiscourseTopicSlug, this.DiscourseTopicId); } }
public DateTime? PublishedDate { get; set; }
public SeriesModel Series { get; set; }
public string Url { get { return string.Format("//{0}/articles/{1}", Config.Wtf.Host, this.Slug); } }
public string CommentsUrl { get { return string.Format("//{0}/articles/comments/{1}", Config.Wtf.Host, this.Slug); } }
public string Url { get { return string.Format("http://{0}/articles/{1}", Config.Wtf.Host, this.Slug); } }
public string CommentsUrl { get { return string.Format("http://{0}/articles/comments/{1}", Config.Wtf.Host, this.Slug); } }
public string Slug { get; set; }
public string TwitterUrl { get { return string.Format("//www.twitter.com/home?status=http:{0}+-+{1}+-+The+Daily+WTF", HttpUtility.UrlEncode(this.Url), HttpUtility.UrlEncode(this.Title)); } }
public string FacebookUrl { get { return string.Format("//www.facebook.com/sharer.php?u=http:{0}&t={1}+-+The+Daily+WTF", HttpUtility.UrlEncode(this.Url), HttpUtility.UrlEncode(this.Title)); } }
Expand Down Expand Up @@ -97,7 +108,12 @@ public static IEnumerable<ArticleModel> GetSeriesArticlesByMonth(string series,

public static IEnumerable<ArticleModel> GetRecentArticles()
{
var articles = StoredProcs.Articles_GetRecentArticles(Domains.PublishedStatus.Published, Article_Count: 8).Execute();
return GetRecentArticles(8);
}

public static IEnumerable<ArticleModel> GetRecentArticles(int count)
{
var articles = StoredProcs.Articles_GetRecentArticles(Domains.PublishedStatus.Published, Article_Count: count).Execute();
return articles.Select(a => ArticleModel.FromTable(a));
}

Expand Down
4 changes: 3 additions & 1 deletion TheDailyWtf/TheDailyWtf.csproj
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -106,6 +106,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Common\ActionResults\RssArticlesResult.cs" />
<Compile Include="Common\Ads\AdRotator.cs" />
<Compile Include="Common\BaseViewPage.cs" />
<Compile Include="Common\CacheProfile.cs" />
Expand Down Expand Up @@ -213,6 +214,7 @@
<Content Include="Content\Images\wtf-logo.png" />
<Content Include="favicon.ico" />
<Content Include="Global.asax" />
<Content Include="robots.txt" />
<Content Include="Scripts\bootstrap-datepicker.js" />
<Content Include="Scripts\custom.js" />
<Content Include="Scripts\gumby.min.js" />
Expand Down
6 changes: 6 additions & 0 deletions TheDailyWtf/robots.txt
@@ -0,0 +1,6 @@
User-agent: *
Disallow: /Resources/
Disallow: /Admin/
Disallow: /Comments/AddComment.aspx
Disallow: /Comments/EditComment.aspx
Disallow: /Comments/DeleteComment.aspx

0 comments on commit 134af1b

Please sign in to comment.