Thursday, August 13, 2009

GeekTool Weathermap

Let's try a code block on this blog, since I write code from time to time. I modified Blogger's stylesheet to do these nice block quotes with shaded backgrounds.

I use GeekTool on my Macbook Pro to display useful stuff on my desktop. One such thing I put together is a weather map of wherever I am, geographically. To do this, assume you have a network connection. Get your current IP. Determine the geographical location of that IP. Get the weather map for that location.

First, a bash shell script to get the weather map:

#!/bin/sh

# Modify path to add portage stuff, which is where "links" and "wget" etc. live.
PATH="/sw/bin:$PATH"

# Location for a saved weather map
MAP=/var/tmp/map.gif

# Determine our current IP address
IP=`links -dump http://checkip.dyndns.org/ | awk '{print $NF}'`

# Determine latitude and longitude
COOR=`wget -q -O - http://api.hostip.info/get_xml.php?ip=${IP} | awk '/coordinates/{print}' | tr -d '[[:alpha:]<>/: ]'`

# Split into individual components
LAT=`echo $COOR | awk -F, '{print $2}'`
LON=`echo $COOR | awk -F, '{print $1}'`

# Now get the weather map for the latitude and longitude
wget -q -O $MAP "http://radblast.wunderground.com/cgi-bin/radar/WUNIDS_composite?centerlat=${LAT}&centerlon=${LON}&radius=120&type=N0R&frame=0&num=5&delay=15&width=230&height=230&newmaps=1&r=1250167046"

# Log that this is complete
logger -p daemon.err -t "$0[$$]" "retrieved: map for ip:${IP} lat:${LAT} lon:${LON}"


Now, this is an example weather map that the above script downloaded:



The script saves the map. Then use GeekTool to display the map on the desktop.

No comments:

Post a Comment