PyRadio 0.1 Released

October 23, 2008

PyRadio 0.1 is now available! PyRadio is a curses based Internet radio player that makes it really simple to play music via the console. It’s written in Python, and uses mplayer for the media playback. The full source code is available via GitHub.

I use Amarok for managing my media collection, and it comes with excellent support for Internet radio. However, Amarok can be fairly resource intensive. That’s why I developed this extremely lightweight console based radio player to make it easy to listen to music without slowing my system down.

For the next release I’m planning to support easy addition and removal of radio stations. Currently you have to edit the actual code if you want to do that. If anyone finds this app useful and would like to request a feature then feel free to leave a comment, either here or on the PyRadio project page.

Javascript Boids

October 17, 2008

Boids are a way to simulate swarm behaviour using 3 simple rules. I’d previously implemented Boids in Python, and was really impressed with the results. When I heard about the javascript vector graphics library Raphaël I thought it would be a good opportunity to port my original code to javascript. If you just want to see the boids in action you can jump right to the boids demonstration.

At the heart of the boids implementation are the boid objects. The constructor function is shown below, and basically just sets up a starting position, size, and velocity, and creates an Raphaël circle:

var Boid = function(x, y, size) {
	this.x = x;
	this.y = y;

	this.xVelocity = 1;
	this.yVelocity = -1;

	this.circle = paper.circle(x, y, size).attr({fill: '#FF0000'});
}

Here’s the boids move away function, which makes sure that the boids don’t crowd each other:

Boid.prototype.moveAway = function(boids, minDistance) {
	var distanceX = 0;
	var distanceY = 0;
	var numClose = 0;

	for(var i = 0; i < boids.length; i++) {
		var boid = boids[i];

		if(boid.x == this.x && boid.y == this.y) continue;

		var distance = this.distance(boid)
		if(distance < minDistance) {
			numClose++;
			var xdiff = (this.x - boid.x);
			var ydiff = (this.y - boid.y);

			if(xdiff >= 0) xdiff = Math.sqrt(minDistance) - xdiff;
			else if(xdiff < 0) xdiff = -Math.sqrt(minDistance) - xdiff;

			if(ydiff >= 0) ydiff = Math.sqrt(minDistance) - ydiff;
			else if(ydiff < 0) ydiff = -Math.sqrt(minDistance) - ydiff;

			distanceX += xdiff;
			distanceY += ydiff;
		}
	}

	if(numClose == 0) return;

	this.xVelocity -= distanceX / 5;
	this.yVelocity -= distanceY / 5;
}

The move closer function, which causes the boids to attract one another:

Boid.prototype.moveCloser = function(boids, distance) {
    if(boids.length < 1) return				

	var avgX = 0;

	var avgY = 0;

	for(var i = 0; i < boids.length; i++) {

		var boid = boids[i];
		if(boid.x == this.x && boid.y == this.y) continue;
		if(this.distance(boid) > distance) continue;

		avgX += (this.x - boid.x);

		avgY += (this.y - boid.y);
	}

	avgX /= boids.length;
	avgY /= boids.length;

	distance = Math.sqrt((avgX * avgX) + (avgY * avgY)) * -1.0
	if(distance == 0) return;

	this.xVelocity= Math.min(this.xVelocity + (avgX / distance) * 0.15, maxVelocity)

	this.yVelocity = Math.min(this.yVelocity + (avgY / distance) * 0.15, maxVelocity)
}

And the move with function, which causes all the boids to generally move in the same direction:

Boid.prototype.moveWith = function(boids, distance) {
    if(boids.length < 1) return
	// calculate the average velocity of the other boids
	var avgX = 0;
	var avgY = 0;
	for(var i = 0; i < boids.length; i++) {
		var boid = boids[i];
		if(boid.x == this.x && boid.y == this.y) continue;
		if(this.distance(boid) > distance) continue;

		avgX += boid.xVelocity;
		avgY += boid.yVelocity;
	}

	avgX /= boids.length;
	avgY /= boids.length;

	distance = Math.sqrt((avgX * avgX) + (avgY * avgY)) * 1.0
	if(distance == 0) return;

	this.xVelocity= Math.min(this.xVelocity + (avgX / distance) * 0.05, maxVelocity);
	this.yVelocity = Math.min(this.yVelocity + (avgY / distance) * 0.05, maxVelocity);

}

With just those simple three rules some quite complex and realistic looking swarm behaviour emerges!

View the boids in action (tested on Firefox 3).

Download the source code: boids.js.

Server status checker shell script

October 3, 2008

It can be absolute nightmare if you find out on Monday morning that your web server was down all weekend, and all your potential visitors would have been greeted with an error message! This has happened to me a couple of times recently (fortunately not on this site), so I came up with an automated checker. The shell script is below:

#!/bin/bash

# Query a supplied URL and output an error message if
# the status code was not 200. Can be used as a cron
# task to check if a server is up or not.

# Ben Dowling - http://www.coderholic.com

# The URL to query
if [ $# -ne 1 ]
then
	echo "Usage: $0 <URL>";
	exit;
fi

url=$1;
response=$(curl -s -I -L $url | grep HTTP); 

status=${response#* }; # Strip off characters up to the first space
status=${status:0:3}; # Just use the 3 digit status code

if [ "$status" != "200" ]
then
	echo "Error fetching $url. Status code '$status'";
fi

I then set this up as a cron task to run every 15 minutes, and to email me if the site doesn’t respond with a HTTP 200 OK status:

*/15 * * * * /path/to/checkserver.sh "http://www.coderholic.com" | mail ben@coderholic.com