forked from oazabir/Droptiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RssTiles.ashx
87 lines (74 loc) · 3.07 KB
/
RssTiles.ashx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<%@ WebHandler Language="C#" Class="RssTiles" %>
using System;
using System.Web;
using System.Xml.Linq;
using System.Collections.Generic;
public class RssTiles : IHttpHandler {
const string TileJs = @"
window.TileBuilders = {
double: function (uniqueId, name, data) {
return {
uniqueId: uniqueId,
name: 'double',
size: data.size || 'tile-double',
appUrl: 'http://www.bbc.co.uk/news/world/',
slides: ['<h2><a href=""' + data.url + '"">' + data.title + '</a></h2>' + (data.image.length > 0 ? '<img src=""' + data.image + '"" />' : '') + '<p class=""body"">' + data.body + '</p>']
};
}
};
";
private static readonly string[] FeedUrls = {
"http://feeds.bbci.co.uk/news/world/rss.xml",
"http://news.yahoo.com/rss/"
};
public void ProcessRequest (HttpContext context) {
XNamespace media = "http://search.yahoo.com/mrss/";
var counter = 1;
var sections = new List<string>();
foreach (var feedUrl in FeedUrls)
{
var xml = XElement.Load(feedUrl);
var feedTitle = xml.Element("channel").Element("title").Value;
var tiles = new List<string>();
foreach (var item in System.Linq.Enumerable.Take(xml.Element("channel").Elements("item"), 6))
{
var mediaNode = item.Element(media + "thumbnail") ?? item.Element(media + "content");
var descriptionNode = item.Element("description") ?? item.Element(media + "text");
var description = HttpUtility.HtmlEncode(System.Text.RegularExpressions.Regex.Replace(descriptionNode.Value, @"</?[^>]*>", "").Replace("'", "\\'"));
tiles.Add(string.Format(@"{{
id: '{0}',
name:'double',
data: {{
title:'{1}',
body:'{2}',
image:'{3}',
url: '{4}'
}}
}}",
"tile" + counter++,
HttpUtility.HtmlEncode(item.Element("title").Value.Replace("'", "\\'")),
description,
mediaNode != null ? mediaNode.Attribute("url").Value : "",
item.Element("link").Value
));
}
sections.Add(string.Format(@"
{{
title: '{0}',
tiles : [{1}]
}}",
feedTitle,
string.Join(",", tiles.ToArray())));
}
context.Response.Write(TileJs);
context.Response.Write(Environment.NewLine);
context.Response.Write("window.RssTiles = [" + string.Join(",", sections.ToArray()) + "];");
context.Response.ContentType = "text/javascript";
context.Response.Write(TileJs);
}
public bool IsReusable {
get {
return false;
}
}
}