I find myself spending quite a bit of my spare time on Stackoverflow in recent weeks, and decided I wanted to display my current rep on my blog. A few days ago I built a dynamically generated (and cached for 2 hours for traffic's sake) user ranking report, since my request for this on Uservoice was not approved.
HtmlAgilityPack has been my weapon of choice in recent days. It was a good fit for this project and the end result looks like this:
And in case you might ask "Show me the code!" here you are. Feel free to use it for your own project if you like.
<%@ WebHandler Language="C#" Class="Badge" %> using System; using System.Web; public class Badge : IHttpHandler { public void ProcessRequest (HttpContext context) { string includeLogo = !string.IsNullOrEmpty(context.Request.QueryString["useLogo"]) ? context.Request.QueryString["useLogo"] : string.Empty; string userId = !string.IsNullOrEmpty(context.Request.QueryString["userid"]) ? context.Request.QueryString["userid"] : "1551"; string jsMode = !string.IsNullOrEmpty(context.Request.QueryString["jsMode"]) ? context.Request.QueryString["jsMode"] : string.Empty; PageRetriever pr = new PageRetriever("http://stackoverflow.com/users/" + userId + "/"); pr.GetPage(); User userBadges = pr.ExtractBadge(); if (!string.IsNullOrEmpty(jsMode)) { context.Response.ContentType = "text/javascript"; context.Response.Write("document.getElementById('stackoverflowRep').innerHTML = '"); } else { context.Response.ContentType = "text/html"; } if (!string.IsNullOrEmpty(includeLogo)) { context.Response.Write("<div style=\"height:40px\"><div style=\"float:left\"></div><div style=\"float:left;\">"); context.Response.Write(<img src=\"http://www.chrisballance.com/so/resources/stackoverflow-logo-250.png\" />"); } context.Response.Write(userBadges.Rep); if (!string.IsNullOrEmpty(includeLogo)) { context.Response.Write("<br /><a href=\"http://stackoverflow.com/users/"); context.Response.Write(userId); context.Response.Write("\">"); context.Response.Write(userBadges.Username); context.Response.Write("</a></div></div></div>"); } if (!string.IsNullOrEmpty(jsMode)) { context.Response.Write("';\n"); } } public bool IsReusable { get { return false; } } }
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.