A few weeks ago I blogged about writing a little program to make my life easier. (The entries are here and here.) In summary, this program automates the messy but easy administrative task of editing links to the sound files of my radio programs into their respective blog entries.
I’ve been experimenting with this prgoram for a few weeks, but I can only really tell if it’s working properly on a Thursday after my radio slot. And, I’m happy to say, it all went swimmingly last Thursday. After my radio program, I kept checking the Radio NZ site manually to see when the sound files went up on it, and then when I went to my blog the links had been added automagically. Wonderful – just one less thing to worry about.
I’ve posted the listing of the program, which is a Python script, here. Given the messiness of what it’s doing there seem to be indecently few lines of actual code. I’ll explain below how they work.
The first few lines are just there to set up pointers to the resources we need, like the Radio NZ website and the blog. The first interesting line is the one that begins while – this sets a up a loop so we can keep checking whether the links to the sound files are on the RNZ page yet.
The next lines get the contents of the RNZ page and look for the links. See my previous blog entries on this for details. And you can see a sleep statement which makes the loop wait for one minute before checking again. It would be rude to hammer on RNZ’s door too often.
You can see how there is no statement in Python ending the loop. All we do is set the indent back to the same level as where the loop started and Python just knows. Some people hate this aspect of the language because it means that white space is under some circumstances significant and can alter the path of your program, something not seen since the heyday of IBM’s supremely rude Job Control Language. I don’t mind this, in fact I find it quite a natural way of showing program structure. Python forces you to show the program’s structure as you write it, rather than leaving it to the computer to “pretty print” it for you. And one other wrinkle – you’ll see that the sleep statement – based on the time module, but that’s another story – is further indented, because it depends on the if before it. Going right back to the left margin closes both the if and the loop.
The line beginning linktext is building the HTML we need to insert into the blog. Then, we get the last blog entry, and split it at the string “download the audio”. Typically this is the last line of the blog entry, with only a full stop afterwards. The if statement following that checks that we did indeed find “download the audio” – if we did, we paste in the links and save it to the blog.
This program will run on pretty much any Internet-connected computer which has a Python interpreter on it. That’s any Mac or *nix box, and Windows boxes where the owner has installed Python.
In practice, because I don’t want to have to trigger this program manually, I am running it on a GNU/Linux server I have semi-employed at home as a file and media server. I set a cron job to run it at 11:15am every Thursday. Then, it loops until the sound files are posted at RNZ, then edits my last blog entry.
The program needs a loop limit so that it doesn’t keep trying if the sound files don’t get posted. I’ll add that later, before I go away in a couple of weeks and miss two weeks on the radio.
But it’s good enough for now, and I’m pleased to have managed to get it working.