Web Services and the Weather

Fetching weather information is a popular example when discussing web services. Examples of fetching weather data have probably been around since the first weather web site was posted on the internet way before XML or SOAP was even a gleam in anyones eye. I've already talked about how easy it is to use Gerald Lester's Web Services for Tcl to talk to Google, this article uses the same technique to fetch weather information.

The URL for this service is http://www.webservicex.net/WeatherForecast.asmx. This article uses the GetWeatherByZipCode operation. One of the nice features of this particular web service is that it provides small thumbnail images to represent the seven day forecast. To take advantage of that, this example will use wish rather than tclsh.

Prerequisites

Before starting with this example, you will need to have the following libraries installed: the aforementioned Web Services for Tcl, the base64 module from tcllib, the http package, and the Img extension. If you have ActiveTcl installed on your machine you should already have everything but the web services library. To get the web services library I'll refer you to a previous article where I explain how to obtain that library: Web Services and the Google API ([2]).

Also, this example assumes you have a preliminary version of tcl/tk 8.5, as the web services library relies on the new dict [3] command. If you only have version 8.4 available you can install a backward-compatible version of dict at [3]

The Basic Framework

The basic framework of the application follows the guidelines I wrote about in Your Second Tcl/Tk Program. Thus, to begin we'll need to define our packages and create a main procedure:

package require WS::Client
package require Tk
package require base64
package require http
package require Img

proc main {} {

}
main

To make sure all your package dependencies are in place, execute the above code with wish. If all goes well you'll get a blank window.

Creating the Main Window

For this example, we want an entry widget where you can enter a zip code. Even though we can bind the return key to fetch the data, we'll also include a button since people don't always know they can press return in a single-line entry field. The zipcode will be stored in a global variable named zipcode, which must be global.

Our main procedure, then, looks like this:


        

References

  1. Tcl and the weather on the Tcler's wiki, http://mini.net/tcl/3564
  2. Web Services and the Google API, http://www.tclscripting.com/articles/nov06/article1.html
  3. dict man page, http://www.tcl.tk/man/tcl8.5/TclCmd/dict.htm
  4. dict for 8.4, http://pascal.scheffers.net/news/item.tcl?scope=public&news_item_id=100091