Wednesday 6 June 2007

Ical subscriptions with orage

Orage looks to be an exciting program for my new Xfce desktop, but what good's a calendar if it can't subscribe to ical feeds from the internet?! Fortunately, I'm supposed to be revising at the moment, so have had an urge to hack code together...

And so we present a simple shell script to download .ics files periodically from the internet to your filesystem. The one nifty feature is that if the internet's down, this won't blat the current copies as using wget blindly would, which should be useful with my laptop.


There are probably more sophisticated systems, but I was going for the 25 minute job option. Firstly, we need a list of feeds - mine looked something like this:

## Lines must be either urls, blank, or start with a #.

# UK holidays
http://ical.mac.com/ical/UK32Holidays.ics

# NASA Space Missions
http://ical.mac.com/tonyfarley/SpaceMissions.ics

# Astronomical Events
http://hewgill.com/astrocal/astrocal.ics


The lines beginning with hashes really are comments - they get stripped by the downloading script, which uses wget:

#!/bin/sh

dir=/home/rupert/.icals/

for f in `cat $dir/feeds.list | sed -e '/^$/d' -e '/^\#/d'`
do
fout=$dir/`echo $f | md5sum | cut -f 1 -d ' '`.ics
tmp=`mktemp $dir/getfile.XXXXXX`
wget -q --tries=3 -O $tmp $f
if [ -s $tmp ]; then
mv $tmp $fout
else
rm $tmp
fi
done


To make it work, you need to call the first file feeds.list and set dir to a directory containing feeds.list and in which you'd like your downloaded .ics files to end up. The quickest way I could think of to give the files names was the md5sum of the url, so once you've got everything in place, set the executable bit on the script (which I called get_feeds.sh) and run it.

If all goes to plan, you should get some files called things like "5b7ed8afdd4a8f71525d0df0e47231e5.ics" appearing in $dir. Now we need to automate it: I used cron, so call crontab -e and add the following line to your crontab:

*/15 * * * * /home/rupert/.icals/get_feeds.sh

(well clearly, you'll need to make the directory right!) This calls the get_feeds.sh script every 15 minutes. Maybe we should slow that down, but I was testing!

Finally, use the new "Exchange Data" item on Orage's File menu to add a foreign file corresponding to the .ics you downloaded and you're away! Wahey!

Incidentally, the SVN orage that I downloaded this evening doesn't appear to be getting multiple foreign files quite right - if I can work out what's going on, I'll file a bug tomorrow!

No comments: