Getting My Feet Wet

Yesterday I finally got started with python scripting, by writing a script that gets weather data from I needed to display the weather conditions and temperature on my desktop using conky, so instead of copy-pasting someone else's script, I decided to use my own.

By downloading the webpage corresponding to the required city, I got an XML file containing all the required data in plain text. Using regular expressions, I parsed the XML file to get the weather conditions (such as 'Partly Sunny or ' Overcast') and the temperature. As an added exercise, I decided to  set the wallpaper to change automatically based on the weather. So if the weather is cloudy, the wallpaper will be a cloudy one, and if the weather's sunny outside, the wallpaper will change to a bright and cheerful one.

There's probably a more elegant way to do this, but since this is my first attempting at python scripting, I'm just glad that it works!

import re
import urllib2 
import os
url=raw_input("Please enter the weather url from : ")
print weather'un',weather)'oud',weather)
if test1:
  os.system("gsettings set org.gnome.desktop.background picture-uri file:///media/Media/Images/Wallpapers/sunny.jpg")
elif test2:
  os.system("gsettings set org.gnome.desktop.background picture-uri file:///media/Media/Images/Wallpapers/cloudy.jpg")

To show the output of a script (any script) on conky, you can use the exec command, which runs the script and displays the shell output on the screen. You can tweak it using execi (which repeats the command at a regular interval) or using execpi (which repeats and parses the output so you can apply text formatting like colours, fonts etc. to the output).

In my case, the command looked like this :

${exec /path/to/script/}

(Of course, you need to make the script executable first.)

I couldn't get it to repeat at a desired interval of 3600 seconds (1 hour) because of a bug in conky where it presents a segmentation fault for large intervals of time. An easier way to automatically run the script at a regular interval would be to use crontab. Meanwhile, I'm going to try to display unread email counts, and maybe a part of my Twitter stream on conky. Wish me luck!


Post a Comment