Geeklets: weather and forecast

[Tweet : ADN : nvALT]

Update: I worked with a couple of readers to make a localizable version of this. I haven’t had time to fully polish and post it, but if you want to use this geeklet in a non-US English setup, contact me and I’ll help you get it running. Currently only German is complete, but the strings file is easy enough to fill out.

GeekTool is on the App Store now, which has renewed some interest in the desktop customization utility. I’ve been asked many times to share some of my GeekTool scripts, so I’m going to start doing them in installments. This first one is one of the more involved scripts, and probably my current favorite. The goal is to put the current weather, an abbreviated 5-day forecast and a large icon for the current conditions onto the desktop. The scripts below work just as well with NerdTool as they do with GeekTool. For the purposes of this post, I’m assuming you know how to create a Geeklet and customize it1, I’ll just provide the scripts and commands.

Most of the weather scripts I’ve seen scrape Google or Yahoo weather for current conditions. That’s a bit of a waste given that Weather Underground (among some others) offers a very complete REST API. Using a bit of Ruby, I just parse the feeds for current conditions and forecast and output the results.

Current conditions

At it’s most basic, the current conditions script looks like this:

response = Net::HTTP.get_response(URI.parse(URI.escape("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#{mycity}")))
doc = REXML::Document.new(response.body)
obs = doc.elements['current_observation'].elements
temp = obs['temp_f'].text
text = obs['weather'].text
return "#{text}, #{temp}"

We just parse out the temperature and the textual representation of the current condition and return them as a short, formatted string. The script download below includes a configuration section for international city and Celsius or Fahrenheit temperatures.

The same XML feed also contains an item with a standard icon name for the condition. This can be applied to any of the icon sets that Weather Underground provides, but I like to use my own by creating a set with the same names and referencing them locally. The download includes my custom set in white and a layered PSD with vector shapes for changing colors and rescaling2. I honestly don’t recall where I found the original set, so if it looks familiar to you and you want to get credit and/or sue me for distributing it, do let me know.

Forecast

The forecast section of the script is similar in concept, just a different feed. We take that one and recurse through the 5 upcoming days and return a formatted forecast similar to:

Forecast for Winona, MN
    Sunday: 65/43, Partly Cloudy
    Monday: 65/43, Clear
   Tuesday: 70/45, Clear
 Wednesday: 74/50, Clear
  Thursday: 76/54, Clear
    Friday: 77/56, Clear

The script is set up to take one main argument which determines the type of information to return: “time,” “current” or “forecast”. If you run it without an argument, it returns current and forecast together. Separating them is nice because you can create separate “geeklets” with different font and size characteristics. When “current” is run, the icon with the specified name is copied over “weathericon.png” in the same directory, so you can just point an image geeklet at that file location and it will update when the current conditions geeklet refreshes.

Installation

  1. Download the zip file below
  2. Place the icon folder somewhere in your user folder (default location is ~/Dropbox/WeatherIcons)
  3. Edit the weatherparser.rb script to set configuration variables at the top
  4. Add a shell geeklet for the current time:
    • Command: /path/to/script/weatherparser.rb time
    • Refresh: 30s
  5. Add a geeklet for current conditions:
    • Command: /path/to/script/weatherparser.rb current
    • Refresh: 1800s (30 minutes, suggested)
  6. Add a geeklet for forecast (optional, of course)
    • Command: /path/to/script/weatherparser.rb forecast
    • Refresh: 1800s
  7. Add an image geeklet to point to /path/to/WeatherIcons/weathericon.png
    • Refresh: 1800s
  8. Profit? I don’t know. Nerd out.

There are exported geeklets in the zip file as well, which you can just drag to GeekTool if you want to use my default settings.

Additional notes

For icon designers/manipulators, there is a second argument you can pass along with “current”: the name of an icon to force it to display. If you run /path/to/WeatherIcons/weatherparser.rb current cloudy it will force the cloudy.png icon to write out, which is useful for testing out new icons if you’re modifying them.

At some point, Weather Underground stopped returning “nighttime” icon names, so I forced that between 8pm and 8am, local time. The current conditions feed doesn’t include sunrise and sunset and I didn’t want to make another call just for that info, so I cheated. If the lack of accuracy is a concern, I’m sure you’ll find a way to fix it.

Hopefully I covered the bases here. If you play with this one, leave a link to a screenshot in the comments, I’d love to see it.

Updated download: This geeklet has been updated to handle locations. See this post for more details.

Localized GeekTool Weather and Forecast v1.5

Get the weather and forecast on your desktop in multiple languages. Includes French, German, Dutch, Italian, Norwegian, Swedish, Polish, Spanish, Russian, Catalan and Japanese with easy extensibility.

Updated Sat Nov 16 2013.

DonateMore info…

  1. A “module” in GeekTool is called a geeklet, and they’re created by dragging one of “file,” “image,” or “shell” to the desktop from the GeekTool window and adjusting parameters and styling in the inspector. If I get enough queries, I’ll do a “basics” post, but there are plenty of them on the Googles.

  2. There’s an adjustment layer at the top which you can use to change the color of all icons at once. Transparency and size can be set in the exported PNG, but GeekTool lets you control the scale and opacity of a geeklet on the fly, so it’s unnecessary.