Tinkering With Time

A few weeks ago now, I was looking for a context within which I could have a play with the deprecated BBC Web API. Now this isn’t the most useful of APIs, as far as I’m concerned, because rather than speaking in the language of iPLayer programme identifiers it users a different set of programme IDs (and I haven’t yet found a way of mapping the one onto the other). But as I’d found the API, I wanted to try to do something with it, and this is what I came up with: a service you can tweet to that will tell you what’s on a specified BBC TV or radio channel now (or sometime in the recent past).

Now I didn’t actually get round to building the tweetbot, and the time handling is a little ropey, but if I write it up it make spark some more ideas. So here goes…

The first part of the pipe parses a message of the form “#remindme BBCChannel time statement”. The BBCChannel needs to be in the correct format (e.g. BBCOne, BBCRFour) and only certain time constructs work (now, two hours ago, 3 hours later all seem to work).

The natural language-ish time expression time gets converted to an actual time by the Date Builder block, and is then written into a string format that the BBC Web API requires:

Then we construct the URI that references the BBC Web API, grab the data back from that URI and do a tiny bit of tidying up:

If you run the pipe, you get something like this:

Time expressions such as “last Friday” seem to calculate the correct date and use the current time of day. So you could use this service to remind yourself what was on at the current time last week, for example.

A second pipe grabs the programme data from the programme ID, by constructing the web service call:

then grabbing the programme data and constructing a description based on it:

It’s then easy enough to call this description getting pipe at the end of the original pipe, remembering to call the pipe with the appropriate programme ID:

So now we get the description too:

To see what’s on (or what was on) between two times, we need to to construct a URI to call the BBC Web API with the appropriate time arguments, suitably encoded:

and then call the web service with that URI.

It’s easy enough to embed this pipe in a variant of the original pipe that generates the two appropriately encoded time strings from two natural language time strings:

If we add the programme details fetching pipe to the end of the pipe, we can grab the details for each programme and get this sort of output from the whole pipeline:

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...