Where would you like to go, Toady? RSS 2.0
# Sunday, February 22, 2009

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:

stack overflow badge

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; } } }

 

Sunday, February 22, 2009 11:10:06 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
Comments are closed.
Stackoverflow.com Rep
Flickr stream
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Chris Ballance
Sign In
Statistics
Total Posts: 46
This Year: 11
This Month: 2
This Week: 0
Comments: 11
All Content © 2010, Chris Ballance

Valid XHTML 1.0 Transitional