My *nix world

X11 weather Linux

I don't know why but everyday I check the weather report. Maybe it became a habit, I don't know. But I know that I need something capable to give me a short weather forecast report.

Well, if you are using Xfce desktop like me then you might want to take a look at xfce4-weather-plugin. The disadvantage of plug-ins (in general) is that they must to be load at the session start-up, they run along with all the session, so they consume RAM and CPU all the time (even if you need it just ten seconds per day).

I like to have a lightweight desktop so I came up with a simple solution:

  • create a simple bash script (eg. : weather) which will check out an weather web service and get a HTML/XML(preferable) report
  • extract all the info from the weather report
  • define a keyboard shortcut (such as Ctrl+Alt+M) that will launch that script every time you want to find out the new weather condition

We are going to use the msn.com weather web service (at the moment of writing this article this is available and fully functional) so we need to know exactly the weather code for our location.

www.weather.com will help us with that, just search for your city/country in the search box provided by weather.com and if it find it then take a look at the address bar of your browser. For Washington DC it will looks like this: http://www.weather.com/weather/today/USDC0001

The important part is the last code I have written with bold (USDC0001) because this is you weather code (of course, for different location you will got different code, so basically you will get anything else than USDC0001).

So edit a bash script called weather like the one bellow:

#!/bin/bash
#####################################################################
# Script for fetching and displaying weather information
# You can define a desktop keyboard shortcut and launch this at user
# request or you can autostart at session login. If you start at
# session login maybe it will be a good idea to adapt little bit
# to display the notification when information is changing
#
# Author               : Eugen Mihailescu
# Last change     : 13.Jan.2013
# E-mail               : eugenmihailescux at gmail dot com
#
# Depends on       : x11-libs/libnotify
# Tested on         : Linux 3.7.1-gentoo x86_64 GenuineIntel
#####################################################################

WEATHER_XML_PATH=/tmp/weather_data.xml
WEATHER_CODE="SWXX0017" #check them on http://www.weather.com
WEATHER_WEBSVC="http://weather.service.msn.com/data.aspx?weadegreetype=C"culture=en-US"wealocations=wc:${WEATHER_CODE}"

SUNRISE_XML_PATH=/tmp/sunrise_data.xml
DAY=$(date +"%d")
MONTH=$(date +"%m")
DAYSAVING=0

###########################################################
# Returns XML parameter's value by name
# $1 : parameter name
# $CONTEMT : global XML content
###########################################################
GetParamValueByName()
{
result=$(echo $CONTENT|awk -v param=$1"="" '{ i=index($0,param); if (i>0) {s=substr($0,i+length(param)); j=index(s,""");
if(j>0){print substr(s,1,j-1);}}}')
echo $result
}

###########################################################
# Return XML section by section's (tag) name
# $1: section name
# $2: source xml file
# $CONTENT : is the output global variable for ENTITY value
###########################################################
ReadSectionContent()
{
while read -d ";FS="";FS="

Note: Don't forget to set the script as executable so do it like this : chmod +x weather

If you had defined your keyboard shortcut (as stated above) then all you need to do is to press Ctrl+Alt+M (like I'm doing) any time you want to get the following meteo information about your location:

weather linux

Important : the icons I have used here are coming from the Xfce icon theme called Tango. If you are using other DM or other icon library then... you have to adapt the above script to your needs. Anyway this is simple because all you have to do is to check out the skycode returned by the meteo web service and also the icon that http://local.msn.com/worldweather.aspx will show for your location.

So just open the following link:

http://weather.service.msn.com/data.aspx?weadegreetype=C"culture=en-US"wealocations=wc:

replace with the code you already got it for your location (I have explained above how to do it).

Take a look at the line which start with "current temperature" and check the skycode field. You can also find a skytext field which is a common description for the respective sky-code. If you are curious to know how the icon should really looks like for that particular sky-code then just open http://local.msn.com/worldweather.aspx and check the current weather condition (you will be interested about the icon.

If you will right-click the icon and get the Image Info we will not be surprised to find that the icon file name is something like .gif where is actually a number (like "26" for "Cloudy").

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
X11 weather Linux

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: