CharBotGreen – a Twitter Radio 4 announcement bot

Update – it’s now charbotgreen2, as twitter never unsuspended charbotgreen.

I wanted to try out the Twitter API and since I was finding myself repeatedly going through the tedium of flipping browser tabs to see what was on Radio 4, I figured I’d make a bot that tweeted what was on Radio 4 instead. This had the added advantage that I could use some half-written code I’d started for a more complex event bot that was turning out to be too hard. I neglected to do a twitter search, however, which would have shown me that there were at least two similar services already working. Ah well. Here’s CharBotGreen

Thanks to: Damian for the name and technology suggestions, @psd for the picture, and Charlotte Green for being a great Radio 4 announcer (as are they all!)

Be warned – do not use my Ruby code as an example of good practice, as it most certainly is not.

What it does

Once a day – pulls down the Radio 4 programmes json (details – what an excellent service that is – beeb++) – and stores it in an H2 database like this, having wiped the database over night (sometime between 1am and 5.20am, when it’s on the world service and no detailed schedule is available anyway):


CREATE TABLE if not exists beeb(DT TIMESTAMP, PID VARCHAR(8), D DATE, T TIME, NAME VARCHAR(255));

So basically I start the Radio 4 day with an SQL representation of today’s schedule page. I started with PID as UNIQUE but then realised that the same PID could be broadcast twice a day.

Every 5 minutes – checks in the database for anything starting in the next 5 minutes and sends a tweet, either ‘starting now’ or ‘starting in a few minutes’ depending on the exactness of the match

SELECT * FROM beeb WHERE D = '#{d}' AND T >= '#{t}' AND T < '#{t1}';

where t is the current time and t1 is the time in 5 minutes (d is today’s date).

Technology

I use ruby and H2 over JDBC. You can see the every 5 minutes and daily scripts and the readme.txt. Why these technologies? Well, I wanted to learn Ruby and using Jruby means that you can use many ruby libraries but you can also access Java classes which is handy for using the H2 database. Why H2? well it’s a self contained, in-memory, SQL-compatible database written in pure Java, so I could keep everything in one directory. For something this lightweight there’s almost no point in using SQL but I wanted it for something a little more complex as well so it made sense (and makes it nice and easy). I use Json pure for the json parsing (it has to be pure to use it with Jruby). If you want to use Ruby rather than Jruby the SQL bit will take some fiddling with; the rest should be ok as is.

Hashtags

I jumped into a little chat on twitter about what hashtags to use and settled on #pid: and then the PID (such as b00h4r7x). I’m still not sure about this; I put the URL in as well.

It’s all super-simple

But good fun to do. Psd suggested that some Charlotte Green-style amusing incidents would be fun to put in there, though I’ve not worked out how to do that. Another improvement would be if it gave you a little more notice about what’s coming up as @bbcradio4live does.

3 thoughts on “CharBotGreen – a Twitter Radio 4 announcement bot

Comments are closed.