Category Archives: Scripts

Scripts I’ve written

Postcodes freed

On 1 April the Ordnance Survey finally opened access to data relating to the location of Britain’s 1.7 million postcodes. It’s long been a gripe of many that these data were not publicly available, and various projects have attempted to collate openly available postcode data, such as Free The Postcode and

A while ago I wrote a PHP class for manipulating postcodes, and in particular for calculating the distance between two postcodes. I wrote it for use in a utility to help people find their nearest reputable tree surgeon by searching on their postcode, which is how it came to be named FindMyNearest. Until now I’ve relied on data gleaned from open sources, often incomplete and of unknown accuracy. I used data only down to postcode district level (eg SW1A, CV32) and stored that in a delimited text file. With the release of Code Point Open I now have the opportunity to use full postcodes of reliable provenance.

The problem was that there are about 1.7 million postcodes in Great Britain. It was fairly clear that code which used a text file for storage and slurped the entire contents into memory wasn’t going to cope well with this. So I went back to the drawing board.

I’ve rewritten FindMyNearest to provide a kind of data abstraction layer. It now supports ‘drivers’ for different types of data store. The text file option is still there, but I added the ability to use a MySQL server to store the data.

Then I came across UK Postcodes, a system based on Code Point Open put together by Pezholio. UK Postcodes provides a simple API for fetching a range of data about a postcode in XML, JSON, CSV or RDF formats. I figured it wouldn’t be too hard to add a driver that utilised this, so I did just that.

And I finally got round to working out how to use Git so I’ve just put FindMyNearest on Git-hub


Pifimon: NetStumbler for Linux?

Well no, not really. But Pifimon does do a little of what NetStumbler does.

Pifimon is a small programme to monitor wireless networks under Linux. It is written in Perl and works by presenting the output of iwlist in a more friendly, and constantly updating, way.

The initial screen presents a list of visible access point with a summary of information about them in table format. The list is updated as fast as iwlist can rescan. You can select one access point to monitor and display that access point’s signal strength as a constantly updating histogram. Here are some screenshots:

List of access points

Initial screen: list of visible access points

Signal strength histogram

Monitoring the signal strength of one access point

Pifimon supports several wireless card drivers, and provides a way to extend support to other drivers. You can download the latest version (pifimon-0.4rc2.tar.gz) or get Pifimon from Github.

Why did I write it?

For various reasons I’ve become interested in building antennae to boost wifi coverage. I needed some way to get a constantly updating representation of signal strength to help test and align antennae. The thing most people seem to use is NetStumbler, but NetStumbler only runs on Windows. Most of my machines, and in particular the netbook I plan to use for testing, run Ubuntu.

After a bit of Googling I came across scanmeter, a bash script that processes the output of the Linux `iwlist scan` command to produce a histogram. Just what I needed! Well nearly, but the great thing about open source is that if it’s not quite right, you can change it.

I started playing about with scanmeter. I didn’t want to have to enter a whole MAC ID to select the cell I wanted to monitor. I thought it would be nice to colour code with a third colour for ‘moderate’. I put colours in the signal strength column of the cell list. Then I thought it would be nice if the cell list updated regularly…

By now I was really stretching the limits of my bash scripting. So I decided to rewrite it in Perl. A bit more googling threw up this blog on using Perl to neaten up iwlist’s output which provided some great ideas on parsing iwlist output with Perl. I rewrote that script as a Perl package, largely to allow for easy extending to cope with different wireless drivers producing different iwlist output. A few hours later I had my first stab at Pifimon…



Call popups with Asterisk and Thunderbird

There are several applications around for Windows users that offer popup notification of incoming calls to an Asterisk server, looking up the caller’s name in various Personal Information Managers (PIMs). Since I don’t use Windows, they’re not much use to me, so I wondered if there was anything available for Linux.

A Google a while back threw up this script by Olivier H. Beauchesne which looked promising. It does part of what I wanted—it generates a popup notification of incoming calls, but it makes no attempt to look up the name of the caller.

I considered writing something as a Thunderbird extension, but there seemed to be a couple of downsides to this approach:

  • From recent experience of working with Thunderbird’s address book for a click2dial extension I knew that it doesn’t lend itself well to look ups keyed on phone numbers
  • I wanted something that would always be running, irrespective of whether or not I’d fired up Thunderbird

I decided to have a go at hacking Olivier’s script, quite ambitious considering I’d never written anything in Python before.

It didn’t actually take much to get it to look up incoming numbers in a SQLite database. I then wrote a dirty Perl script to pull all the phone numbers out of my Thunderbird address book and populate the database. The Perl script was way to ugly to share and I always intended to tidy it up, but just never found the time. Until now.

callPopPy in action

callPopPy in action

A prolonged period off work sick has driven me to looking for things to keep my mind occupied, and so I’ve returned to this project. The Perl script has been scrapped. In it’s place I’ve written a Thunderbird extension named Squalit, a much neater solution. Squalit can export a single contact or an entire address book to the database, and can be configured to update the database periodically, ensuring that call popups and Thunderbird are always in sync.

I then turned my attention to the popup script itself. The original relied on libnotify and its Pyhon binding, pynotify. These are only available on Gnome based Linux distributions such as Ubuntu. I thought it would be useful to make everything more portable, so rewrote it to use a library I stumbled across by Daniel Woodhouse, gtkPopupNotify. After a fair few other changes I was left with a distant descendant of Olivier’s original script, which I’ve called callPopPy. callPopPy is portable enough that I’ve had it running on a Windows XP machine, and is doing just what I wanted on Ubuntu. The advantage of the two stage approach is that other utilities could be written to integrate callPopPy with other PIMs.

The combination of TBDialOut for click2dial with Squalit and callPopPy for call popup notification provides me with great integration between Thunderbird and my Asterisk server—I guess this is what is meant by CTI.