Geektool + Python = Desktop Weather (& moonphase)

Jan. 1, 2019: Some tools you might find useful. Weather API – free 1000 hits per day. Moon phase library.

Aug. 2016 update: Be aware that Yahoo no longer offers weather data, at least not free.

Here is the weather service I’m using:

Python library to make consumption of the data fairly simple.

I had some free time today and was playing with Geektool, which is like the desktop toys I used to play with years ago on Linux – SuperKaramba comes to mind. I wrote a Python script as an example to do some weather stuff, which can be edited to work behind the scenes or to directly display weather in Geektool. It also will download an animated radar image and gets the weather icon file that you can map to your own icons or, with a minor code edit, download directly from Yahoo. Read the comments to setup in Geektool.

#! /usr/bin/python
# -*- coding: utf-8 -*-
Author: C. Nichols 

Weather script to be used with Geektool,
or whatever you like...
import subprocess,StringIO
import urllib
from xml.dom import minidom
import re
import shutil

def get_attribute(html, attr):
    res ='%s="(.*?)"' % attr, html)

def curl(url):
    c = subprocess.Popen(["curl", url], stdout=subprocess.PIPE)
    (out,err) = c.communicate()
    return out

def get_radar(image_out):
    image_url = ""
    result = curl(image_url)
    if result:
        print 'unable to retrieve image file.'

def weather_for_zip(zip_code):
    WEATHER_URL = ''
    WEATHER_NS  = ''
    wurl = WEATHER_URL % zip_code

    results = curl(wurl)
    weather_icon = get_attribute(results, 'src')
    icon = weather_icon.split('/')[-1]

    dom = minidom.parseString(results)
    forecasts = []
    for node in dom.getElementsByTagNameNS(WEATHER_NS, 'forecast'):
            'date': node.getAttribute('date'),
            'low': node.getAttribute('low'),
            'high': node.getAttribute('high'),
            'condition': node.getAttribute('text')
    ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
    return {
        'current_condition': ycondition.getAttribute('text'),
        'current_temp': ycondition.getAttribute('temp'),
        'forecasts': forecasts,
        'title': dom.getElementsByTagName('title')[0],
        'icon': icon
You can now simply build your output from a simple key/val dictionary
including the weather icon buy pointing to your icon set or change
to download from Yahoo. Also, grab the radar if you want one.  

To run in Geektool, open shell and add: python /path/to/your/ 

Don't forget the change the paths to the image files below.
    myWeather = weather_for_zip("43230")
    forcasts = []
    for i in myWeather['forecasts']:
        forcasts.append(i['date']+': '+i['condition']+'\n')

    message = u"%s°F %s\n%s" %(myWeather['current_temp'],
    print message.encode('utf-8')

    # Copy yahoo weather icon to weather...
    localIcon = "/Users/mohawke/Geektool/Symbols/%s.png" %myWeather['icon'].split('.')[0]
    replaceIcon = "/Users/mohawke/Geektool/weather.png"


except Exception, error:
    print "Unable to get weather data"
    #print error

So how about reading the system log? Even easier.

#! /usr/bin/python
# python /path/to/your/

sysLogPath = "/var/log/system.log"
limit = 5  # How manyu lines to display.

lineCount = 0
for line in sorted(open(sysLogPath, "r"), reverse=1):
    if lineCount == limit: break

    print line

    lineCount += 1

Pretty easy stuff, but if you need assistance give me a shout. I’ve included some cool icons that I found in the download, but I could not find who made them. If you made them let me know and I’ll gladly give you credit.

Download Weather

I’ve been made aware that some countries cannot use a zip code so here is an example to get the weather by longitude and latitude, automated so it will change as you move your laptop.

Download Weather by location example.

and you simply can’t have the weather without the moon phase, right?

Download Moonphase

~ by mohawke on May 20, 2012.

2 Responses to “Geektool + Python = Desktop Weather (& moonphase)”

  1. Nice!! I like the moonphase script. Just getting into python and finding it be a very clean and easy to work with language.

  2. I tried setting up the moonphase script on GeekTool, but it seems to be refusing to display an image. All I’ve done is copy the python /path/to/your/ to a shell script and changed the path to my Do I need to add an image glet and format that, too? And if so, how? The error I’m getting is that it displays the file title (textual moon phase), but there is no image displayed at all.

Comments are closed.

%d bloggers like this: