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 < ENTITY CONTENT; do
if [[ $ENTITY = $1 ]] ; then
break
fi
done < $2
}

###########################################################
# Returns path to corresponding icon by SkyCode
# $1: sky code
###########################################################
# Sky codes (source: http://stackoverflow.com/questions/12142094/msn-weather-api-list-of-conditions)
#       0, 1 ,2, 3 ,4, 17, 35 - Thunderstorm
#       5 - Rain/Snow mix
#       6 - Sleet/Snow mix
#       7 - Rain/Snow/Sleet mix
#       8,9 - Icy
#       10 - Rain/Sleet mix
#       11 - Light Rain
#       12 - Rain
#       13 - Light Snow
#       14,16,42,43 - Snow
#       15 - Blizzard
#       18,40 - Showers
#       19 - Dust
#       20 - Fog
#       21 - Haze
#       22 - Smoke
#       23,24 - Windy
#       25 - Frigid
#       26 - Cloudy
#       27,29,33 - Partly Cloudy (night)
#       28,30,34 - Partly Cloudy
#       31 - Clear (night)
#       32 - Clear
#       36 - Hot
#       37,38 - Scattered Thunderstorms
#       39 - Scattered Showers
#       41 - Scattered Snow Showers
#       44 - N/A
#       45 - Scattered Rain Showers (night)
#       46 - Scattered Snow Showers (night)
#       47 - Scattered Thunderstorms (night)

GetIconBySkyCode()
{
ICON_RES="scalable"
ICON_EXT=".svg"
path=/usr/share/icons/Tango/${ICON_RES}/status/
fname=""
case $1 in
9) fname="weather-showers-scattered${ICON_EXT}"
;;
11) fname="weather-showers-scattered${ICON_EXT}"
;;
20) fname="weather-fog${ICON_EXT}"
;;
26) fname="weather-overcast${ICON_EXT}"
;;
27) fname="weather-overcast${ICON_EXT}"
;;
28) fname="weather-overcast${ICON_EXT}"
;;
39) fname="weather-showers${ICON_EXT}"
;;
30) fname="weather-overcast${ICON_EXT}"
;;
31) fname="weather-clear-night${ICON_EXT}"
;;
32) fname="weather-clear${ICON_EXT}"
;;
33) fname="weather-few-clouds-night${ICON_EXT}"
;;
34) fname="weather-few-clouds${ICON_EXT}"
;;
45) fname="weather-showers-scattered${ICON_EXT}"
;;
5|6|7|1[346]|4[1236] ) fname="weather-snow${ICON_EXT}"
esac
echo "$path$fname"
}

###########################################################
# Read weather XML from web service, extract weather's fields
###########################################################
rm -f ${WEATHER_XML_PATH}
wget -q -O ${WEATHER_XML_PATH} ${WEATHER_WEBSVC}

ReadSectionContent "weather" ${WEATHER_XML_PATH}
Location=$(GetParamValueByName "weatherlocationname")
Lat=$(GetParamValueByName "lat")
Long=$(GetParamValueByName "long")
TZ=$(GetParamValueByName "timezone")

ReadSectionContent "current" ${WEATHER_XML_PATH}
Date=$(GetParamValueByName "date")
Temp=$(GetParamValueByName "temperature")
FeelsLike=$(GetParamValueByName "feelslike")
Humidity=$(GetParamValueByName "humidity")
Wind=$(GetParamValueByName "winddisplay")
SkyCode=$(GetParamValueByName "skycode")
Sky=$(GetParamValueByName "skytext")
Station=$(GetParamValueByName "observationpoint")
Icon=$(GetIconBySkyCode $SkyCode)

###########################################################
# Read sunrise XML from web service, extract sunrise's fields
###########################################################
rm -f ${SUNRISE_XML_PATH}

SUNRISE_WEBSVC="http://www.earthtools.org/sun/$Lat/$Long/$DAY/$MONTH/$TZ/$DAYSAVING"
wget -q -O ${SUNRISE_XML_PATH} ${SUNRISE_WEBSVC}
Sunrise=$(awk 'BEGIN { RS=">";FS="<" } /sunrise/ {if (length($1)==8) {print $1}}' ${SUNRISE_XML_PATH} )
Sunset=$(awk 'BEGIN { RS=">";FS="<" } /sunset/ {if (length($1)==8) {print $1}}' ${SUNRISE_XML_PATH} )

###########################################################
# Display a boolon containing weather/sunrise info
###########################################################
/usr/bin/notify-send -i $Icon "$Location $Date" "Temp : $Temp C (Feels like : $FeelsLike C; Humidity : $Humidity%)nSky : $Sky; Wind : $WindnMeteo station : $StationnSunrise: $SunrisenSunset: $Sunset"

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.
X11 weather Linux

Latest posts by Eugen Mihailescu (see all)

Tagged on: ,

Leave a Reply

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