/
Rss.ashx.cs
158 lines (136 loc) · 6.49 KB
/
Rss.ashx.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Xml;
using AspNetDating.Classes;
namespace AspNetDating
{
public class RssHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["action"] == null) return;
switch (context.Request.Params["action"].ToLower())
{
case "news":
GenerateNewsRSS(context);
break;
case "newusers":
GenerateNewUsersRSS(context);
break;
}
}
private static void GenerateNewsRSS(HttpContext context)
{
byte[] xmlResponse;
string cacheKey = String.Format("RssHandler_GenerateNewsRSS");
if (context.Cache[cacheKey] != null)
{
xmlResponse = context.Cache[cacheKey] as byte[];
}
else
{
News[] news = News.FetchAsArray(1);
MemoryStream ms = new MemoryStream();
XmlTextWriter objX = new XmlTextWriter(ms, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version", "2.0");
objX.WriteStartElement("channel");
objX.WriteElementString("title", Config.Misc.SiteTitle + " - " + Lang.Trans("News"));
objX.WriteElementString("link", Config.Urls.Home);
objX.WriteElementString("description", Lang.Trans("Latest news!"));
objX.WriteElementString("copyright", Lang.Trans("(c) 2003-2008, All rights reserved."));
objX.WriteElementString("ttl", "1440");
if (news != null)
{
foreach (News singleNews in news)
{
objX.WriteStartElement("item");
objX.WriteElementString("title", singleNews.Title);
objX.WriteElementString("description", singleNews.Text);
objX.WriteElementString("link", Config.Urls.Home + "/News.aspx?id=" + singleNews.ID);
objX.WriteElementString("pubDate", singleNews.PublishDate.ToString("R"));
objX.WriteEndElement();
}
}
objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
xmlResponse = ms.ToArray();
context.Cache.Insert(cacheKey, xmlResponse, null, DateTime.Now.AddHours(1),
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
}
context.Response.Clear();
context.Response.ContentType = "application/xml";
context.Response.BinaryWrite(xmlResponse);
context.Response.End();
}
private static void GenerateNewUsersRSS(HttpContext context)
{
byte[] xmlResponse;
string cacheKey = String.Format("RssHandler_GenerateNewUsersRSS");
if (context.Cache[cacheKey] != null)
{
xmlResponse = context.Cache[cacheKey] as byte[];
}
else
{
NewUsersSearch nuSearch = new NewUsersSearch();
nuSearch.PhotoReq = true;
nuSearch.UsersCount = 10;
UserSearchResults nuResults = nuSearch.GetResults();
MemoryStream ms = new MemoryStream();
XmlTextWriter objX = new XmlTextWriter(ms, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version", "2.0");
objX.WriteStartElement("channel");
objX.WriteElementString("title", Config.Misc.SiteTitle + " - " + Lang.Trans("New Users"));
objX.WriteElementString("link", Config.Urls.Home);
objX.WriteElementString("description", Lang.Trans("They've just joined us!"));
objX.WriteElementString("copyright", Lang.Trans("(c) 2003-2008, All rights reserved."));
objX.WriteElementString("ttl", "30");
if (nuResults != null && nuResults.Usernames != null)
{
foreach (string username in nuResults.Usernames)
{
User user = User.Load(username);
int age = (int) (DateTime.Now.Subtract(user.Birthdate).TotalDays/365.25);
string descr = String.Format(Lang.Trans("<img src=\"{2}\"><br>Gender: {0}<br>Age:{1}"),
user.Gender, age, Config.Urls.Home +
"/Image.ashx?id=" +
Photo.GetPrimary(username).Id +
"&width=90&height=90&cache=1");
objX.WriteStartElement("item");
objX.WriteElementString("title", username);
objX.WriteElementString("description", descr);
objX.WriteElementString("link", Config.Urls.Home + "/ShowUser.aspx?uid=" + username);
objX.WriteElementString("pubDate", user.UserSince.ToString("R"));
objX.WriteEndElement();
}
}
objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
xmlResponse = ms.ToArray();
context.Cache.Insert(cacheKey, xmlResponse, null, DateTime.Now.AddMinutes(10),
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
}
context.Response.Clear();
context.Response.ContentType = "application/xml";
context.Response.BinaryWrite(xmlResponse);
context.Response.End();
}
public bool IsReusable
{
get { return true; }
}
}
}