Tag Archives: API

Wunderground API via .Net

Intro

For those using WunderGround for weather, you probably are pulling your hair out. Not because it is a terrible service. But more likely because there is so little documentation. It is definitely one service that could use a technical writer to organise and tidy things up. For example, there is a  complete lack of documentation on errors. And there is no sample to show how to access WunderGround from .Net

The Sample

I was doing some work for a client and they had this need to download historic code from Wunderground for the town their clients have operations in. The weather relates to retail sales and footfall and can obviously be used to determine is a slow day was caused by weather.

On the WunderGround API website there are a few samples. One in Ruby, and even in (gasp) ColdFusion. But nothing in .Net. Seemed a bit odd given the popularity of .Net.

After a bit of rooting around I managed to bolt together this code – excuse the VB.Net my client prefers that I use VB.

Imports System.Xml.Linq
Imports System.Xml

Dim sWunderGroundURL As String = "http://api.wunderground.com/api/{0}/history_{1:yyyyMMdd}/q/{2}.xml"
Dim MyApiKey as String = "anAPIKeyHere"
Dim MyDate as DateTime = #8/1/2012#
Dim MyTown as String = "UK/London"

Dim root As XElement = XElement.Load(String.Format(sWunderGroundURL, MyApiKey, MyDate, MyTown))

'Check for an error
Dim errors As IEnumerable(Of XElement) = _
 root...<error>

If errors.Count > 0 Then
 nErrorCount += 1
Else
'Do your processing here
End If

The code is fairly straight forward but note that I am using the xml interface and not JSON. I like to use Linq to XML so rather than use xmlDocument, you use XElement. You load the URI into the XML then check for an error element. If there is one I log or print out an error otherwise do my further processing.

You could use the JSON interface if you wished. You would have the serialize in the JSON and then process it. And extra step I didn’t feel was worth it.

Conclusion

There you have it a very simple sample on connecting to WunderGround using .Net. I may submit this to the nice fellows at WunderGround – but as I like hits to this site..maybe not.