Uptonian Thoughts

Yahoo! Weather Feeds With Python

· ·

Download the weather script from GitHub.

UPDATE: Chris Lasher updated my script. It now utilizes optparse and docstrings. Some of the options have been changed, too, and I have licensed the code under a by-nc-sa CC license. I have updated this post to reflect these changes. Thanks, Chris!

I believe that everyone likes to know what the weather is like outside. In the good ol’ days, people used to stick a hand out of the window and know everything they needed to know about the weather; now we have myriad options to check the weather.

A few years ago, I was running Konfabulator on Windows. The best widget for this application was the weather widget. I had a large weather widget that showed the current weather in the town in which I was currently residing, and some smaller weather widgets allowed me to keep tabs on the weather elsewhere.

When I got my Powerbook in 2004, I continued to use Konfabulator. With Tiger came Dashboard in 2005; I promptly started using the weather widgets provided by Dashboard. However, I realized that the requirement that I switch contexts to Dashboard just to quickly check small tidbits of information greatly distracted me from my workflow. Even the ability to display widgets directly on the desktop was no help; the pretty images take up too much screen real estate.

As I have written about in the past, I utilize Geek Tool on my desktop. One of the scripts I run scrapes the Weather Underground XML feed for Blacksburg, VA and displays the current conditions. This works fine, but it isn’t very extensible. Weather Underground does not appear to have a feed API, so each city’s XML feed must be found manually and utilized in the script. Last night I decided that this was unacceptable.

Yahoo offers a great RSS-based API for its weather services. Python has a built-in XML parser. It doesn’t take a rocket surgeon to put those two together. With a little help from Yahoo’s Developer Network, I was able to come up with a more robust script with which the weather can be displayed.

I learned a few things regarding Python while writing this script. The getopt() method is a very useful command-line options and arguments parser. It’s very simple to define options for your script using this method.

1
2
3
4
import getopt
import sys

opts, args = getopt(sys.argv[1:], "cflv")

Now the options that were passed on the command line and that are part of cflv are in the opts array; command line arguments are listed in args.

UPDATE: My script now uses optparse instead of getopt.

minidom is a simple but complete DOM implementation. Using the getElementsByTagName and the getAttribute methods makes for easy and stress-free XML parsing.

1
2
3
4
5
6
7
8
from xml.dom.minidom import parse

dom = parse("file.xml")

attrs = []

for node in dom.getElementsByTagName('tag'):
    attrs.append(node.getAttribute('text'))

attrs contains the text attribute of each tag node in the XML file file.xml.

I am currently looking into building this script on top of a Twitter model. Keep checking in for more information.

In order to run the script, pass it a zip code to find the weather.

1
$ weather.py 24060

Running the script without any arguments will show the usage and the available options.

  • -c Suppress the current weather output. Use this if you only want the forecast data.
  • -f DAYS Show the forecast for the next DAYS days.
  • -l Show the city and region name of the forecast that was retrieved. Use this to verify the zip code that you are using is in fact your area.
  • -v Print headers above all output.

Here is some example output from the script:

Default options:

1
2
$ weather.py 24060
55F | Drizzle

Forecast and location options:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ weather.py -lf2 24060
Blacksburg, VA

55F | Drizzle

22 Apr 2008
  Low: 50F
  High: 60F
  Condition: Few Showers

23 Apr 2008
  Low: 49F
  High: 71F
  Condition: AM Clouds/PM Sun