John and Ian, the nice chaps behind the Five Pound App meetings are holding a competition to build the best application or website you can in 5K of code. The prize? £51.20, which is 5K (5120 bytes) in pennies.
I was initially dubious that I’d have time to make any apps, partly because I have a lot of client work on, and partly because I couldn’t think of anything to make. 5K is small and making something useful in it is a tricky. However, I was inspired by John’s demo applications to at least do something. Recently I was fed up with the work I was doing and went for a walk down to the sea and finally, inspiration!
My first app, and oddly the last one completed, uses Twitter. I wanted to build something relatively useful to an audience, and that would run itself. I built a Twitter joke poster a while ago at the Farm Hack Day event I organised, and the problem with it is I haven’t had time to compile jokes for it for ages (although it’s still on my to-do list, which is unfortunately just a little longer than my arm right now, and I have very long arms.)
I started thinking about information that updates often, and that the non-geeks who are joining Twitter now might like to have. I thought of horoscopes, but quickly realised it would be difficult to compress these down to 140 characters without needing human intervention (and have since discovered a couple of people doing the same thing.) I then remembered biorhythms. These are supposed cycles of Physical, Intellectual and Emotional states that you go through, related to when you were born.
Biorhythms were popular in the mid-’80s, when my sister had a biorhythm calculator on her BBC Micro, and they’ve drifted in and out of fashion since then, and probably way before. This means they’re likely to be in fashion for some people, or will be at some point. Another major plus is that they’re quite small pieces of information, which means they can be crammed in to a tweet without too much trouble.
So, my first 5K app (finished today) is…
The 5K Twitter Biorhythm Bot
Twittering as @byobot if you send a message to the bot in the format @byobot yyyy-m-d (e.g. “@byobot 1942-1-8″ if you were Stephen Hawking) then it will reply back with your biorhythm for today.
There’s not a lot of error checking, so if you don’t get the date in the right format, you just get a friendly error message sent back to you.
The code is written in PHP and uses a greatly cut down version of the biorhythm class by Iztok Strzinar for the main calculation. Twitter is handled by a combination of Curl and SimpleXML.
Once I’d found the class, the main things I had to work on was compiling the message so it was short enough to go in a tweet (including leaving space for the @ reply to the person wanting the biorhythm.) And also a way of storing the last tweet read, so the bot wouldn’t keep replying to messages it had replied to before. By using the XML version of the ‘mentions’ stream from the Twitter API I was able to store ID of the last tweet replied to and use the ’since_id=’ variable in the URL when getting the stream so it receives the 20 tweets after the last tweet that the bot read.
The code, plus a file to store the last tweet ID in, is 4,605 bytes. I managed to make a very light logo for Twitter by reducing the colours used in the GIF down a long way – this was a little problematic as Twitter wouldn’t accept a GIF with only 16 colours, I had to go to 32. Altogether, code and image come to 5,036 bytes, which means I have 84 bytes left to play with.
I can think of various things to do with the Biorhythm Bot, the first one to be adding direct messaging to it can give private results to people, but that’s outside the scope of a 5K project and won’t happen for a while anyway as I have too many other client and personal projects on the boil.
Why ‘byobot’? When I first started thinking about this project, I wanted to use ‘biorhythm’ as the account name on Twitter, but both that and ‘biorhythms’ are already in use. I had thought of using ‘bior’, but that got registered while I was messing around with the code. Let this be a lesson to you: if you’re going to do a project, register the damn account / domain name while you still can!