I'm writing a Stack Overflow API wrapper, currently at http://soapidotnet.googlecode.com/. I have a few questions about parsing SO RSS feeds.
I've chosen to use RSS.NET to parse the RSS, but I have a few questions about my code (which I have provided further down in this post).
My Questions:
First of all, am I parsing those attributes correctly? I have a class named Question, which has those properties.
Next, how can I parse the <re:rank>
RSS property (used for # of votes)? I'm not sure how RSS.NET lets us do that. As far as I understand, it's a element with a custom namespace.
Finally, do I have to add all the properties manually, like currently in my code? Is their some sort of deserialization that I can use?
Code:
Below is my current code for parsing recent question feeds:
/// <summary>
/// Utilises recent question feeds to obtain recently updated questions on a certain site.
/// </summary>
/// <param name="site">Trilogy site in question.</param>
/// <returns>A list of objects of type Question, which represents the recent questions on a trilogy site.</returns>
public static List<Question> GetRecentQuestions(TrilogySite site)
{
List<Question> RecentQuestions = new List<Question>();
RssFeed feed = RssFeed.Load(string.Format("http://{0}.com/feeds",GetSiteUrl(site)));
RssChannel channel = (RssChannel)feed.Channels[0];
foreach (RssItem item in channel.Items)
{
Question toadd = new Question();
foreach(RssCategory cat in item.Categories)
{
toadd.Categories.Add(cat.Name);
}
toadd.Author = item.Author;
toadd.CreatedDate = ConvertToUnixTimestamp(item.PubDate).ToString();
toadd.Id = item.Link.Url.ToString();
toadd.Link = item.Link.Url.ToString();
toadd.Summary = item.Description;
//TODO: OTHER PROPERTIES
RecentQuestions.Add(toadd);
}
return RecentQuestions;
}
Here is the code of that SO RSS feed:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
<title type="text">Top Questions - Stack Overflow</title>
<link rel="self" href="http://stackoverflow.com/feeds" type="application/atom+xml" />
<link rel="alternate" href="http://stackoverflow.com/questions" type="text/html" />
<subtitle>most recent 30 from stackoverflow.com</subtitle>
<updated>2009-11-28T19:26:49Z</updated>
<id>http://stackoverflow.com/feeds</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>
<entry>
<id>http://stackoverflow.com/questions/1813483/averaging-angles-again</id>
<re:rank scheme="http://stackoverflow.com">0</re:rank>
<title type="text">Averaging angles... Again</title>
<category scheme="http://stackoverflow.com/feeds/tags" term="algorithm"/><category scheme="http://stackoverflow.com/feeds/tags" term="math"/><category scheme="http://stackoverflow.com/feeds/tags" term="geometry"/><category scheme="http://stackoverflow.com/feeds/tags" term="calculation"/>
<author><name>Lior Kogan</name></author>
<link rel="alternate" href="http://stackoverflow.com/questions/1813483/averaging-angles-again" />
<published>2009-11-28T19:19:13Z</published>
<updated>2009-11-28T19:26:39Z</updated>
<summary type="html">
<p>I want to calculate the average of a set of angles.</p>
<p>I know it has been discussed before (several times). The accepted answer was <strong>Compute unit vectors from the angles and take the angle of their average</strong>.</p>
<p>However this answer defines the average in a non intuitive way. The average of 0, 0 and 90 will be <strong>atan( (sin(0)+sin(0)+sin(90)) / (cos(0)+cos(0)+cos(90)) ) = atan(1/2)= 26.56 deg</strong> </p>
<p>I would expect the average of 0, 0 and 90 to be 30 degrees.</p>
<p>So I think it is fair to ask the question again: How would you calculate the average, so such examples will give the intuitive expected answer.</p>
</summary>
</entry>
etc.
Here is my Question class, if it will help:
/// <summary>
/// Represents a question.
/// </summary>
public class Question : Post //TODO: Have Question and Answer derive from Post
{
/// <summary>
/// # of favorites.
/// </summary>
public double FavCount { get; set; }
/// <summary>
/// # of answers.
/// </summary>
public double AnswerCount { get; set; }
/// <summary>
/// Tags.
/// </summary>
public string Tags { get; set; }
}
/// <summary>
/// Represents a post on Stack Overflow (question, answer, or comment).
/// </summary>
public class Post
{
/// <summary>
/// Id (link)
/// </summary>
public string Id { get; set; }
/// <summary>
/// Number of votes.
/// </summary>
public double VoteCount { get; set; }
/// <summary>
/// Number of views.
/// </summary>
public double ViewCount { get; set; }
/// <summary>
/// Title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Created date of the post (expressed as a Unix timestamp)
/// </summary>
public string CreatedDate
{
get
{
return CreatedDate;
}
set
{
CreatedDate = value;
dtCreatedDate = StackOverflow.ConvertFromUnixTimestamp(StackOverflow.ExtractTimestampFromJsonTime(value));
}
}
/// <summary>
/// Created date of the post (expressed as a DateTime)
/// </summary>
public DateTime dtCreatedDate { get; set; }
/// <summary>
/// Last edit date of the post (expressed as a Unix timestamp)
/// </summary>
public string LastEditDate
{
get
{
return LastEditDate;
}
set
{
LastEditDate = value;
dtLastEditDate = StackOverflow.ConvertFromUnixTimestamp(StackOverflow.ExtractTimestampFromJsonTime(value));
}
}
/// <summary>
/// Last edit date of the post (expressed as a DateTime)
/// </summary>
public DateTime dtLastEditDate { get; set; }
/// <summary>
/// Author of the post.
/// </summary>
public string Author { get; set; }
/// <summary>
/// HTML of the post.
/// </summary>
public string Summary { get; set; }
/// <summary>
/// URL of the post.
/// </summary>
public string Link { get; set; }
/// <summary>
/// RSS Categories (or tags) of the post.
/// </summary>
public List<string> Categories { get; set; }
}
Thanks in advance! Btw, please contribute to the library project! :)