Finally, A Schedule!

Ever since I've joined college and started living on my own, my daily schedule has gone for a toss. Though I'd written about what a typical Sunday is like, it was more of an attempt to show how chaotic life can be when you're living by yourself in a hostel. So I've never really had a schedule for the past 3 years, and I can never tell what I'm going to be doing tomorrow at this time. 

Except for the last month.


Getting My Feet Wet

Yesterday I finally got started with python scripting, by writing a script that gets weather data from accuweather.com. 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!

#!/usr/bin/python
import re
import urllib2 
import os
url=raw_input("Please enter the weather url from Accuweather.com : ")
xml=urllib2.urlopen(url)
f=xml.read()
temp=re.search(r'(\"temp\">)(\w+)',f)
cond=re.search(r'(\"cond\">)(\w+\s\w+)',f)
weather=cond.group(2)
print temp.group(2)
print weather
test1=re.search(r'un',weather)
test2=re.search(r'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/weather.py}

(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!