Generating Football Fixture Calendars in iCal (ICS) format

Chris Gibson  —  April 11, 2012 — 24 Comments

When the BBC recently redesigned the BBC Sport website, a screen-scraping script that I wrote a year or so ago suddenly stopped working. While I was rewriting it, I thought I’d take the opportunity to improve it a little, release the code via GitHub, and write a blog about it.

A little history

Feel free to skip this section if you just want to get to the code.

Up until not so long ago, it was impossible to find a decent online football fixture calendar — that is, something that you could point your iPhone at, or add to your Google Calendar, and track all the upcoming fixtures for your chosen team.

This is because the “data” in question — the actual fixture list — has been deemed copyright by the Premier League (and the Football League). Many small websites have fallen foul of this ruling, and while the legality of the case has been questioned, it was nonetheless true that any websites publishing this information found themselves in potentially hot water with the authorities.

(Of course, an official channel could have made this information available in the necessary format, but large media agencies are not noted for their forward-thinking approach to technology. Either way, attempting to copyright and license this information — factual, widely published data about which football team is playing which, and when — clearly is ludicrous. However, these are blog posts for another time).

It occurred to me that it wouldn’t be too difficult to convert the fixture lists published on the BBC Sport website into iCal format, suitable for most calendar clients. This information is highly accurate, and updated promptly and automatically (in the event of a fixture being rescheduled, for example). The code discussed below does just that.

Addendum: Google have since published fixture lists for pretty much all worldwide sporting leagues; quite how these are generated, and what their legal position is, I don’t know. (Any insight into this would be appreciated). They’re not ideal, though; you can only subscribe on a competition level, such as “All Manchester United’s Premier League matches”, and you need to add FA Cup games (etc) separately. Also, the format of the fixture is listed as “Away Team @ Home Team”, which is counterintuitive to British readers. So, the following code does still have some purpose.

The code

The code itself is actually straight-forward. (In fact, compared to the long background piece above, this part of the blog will be very brief).

Initially, I used cURL to load the contents of a fixture list from the BBC site (such as, http://www.bbc.co.uk/sport/football/teams/manchester-united/fixtures), then wrote a regular expression to strip out relevant data from the HTML source (teams, dates and so on).

I then compiled this into an iCal data export manually, writing out code along the lines of:

BEGIN:VEVENT DTSTART;TZID=Europe/London:20110101T150000
DTEND;TZID=Europe/London:20110101T164500

… and so on.

This worked well, but when the layout of the BBC site changed (thus changing the HTML source, and thus breaking the regular expression) I took the opportunity to make everything more robust. This meant:

The DOM parser is more flexible than a simple regular expression (and less likely to break if the HTML changes slightly), and iCalCreator is more reliable way of building the iCal files. Any slight overhead is of no consequence, given the relatively low frequency at which the script will be called (see below).

Usage

Obviously, it would be trivial to host this script, allow users to specify their favourite team, and thus publish calendars to thousands of individuals. However, this could fall foul of the legal situation described above. Screen-scraping itself is also a legal grey area, and chances are that the BBC website’s T&Cs forbid it.

However, given that the code is publicly available, there is nothing stopping you from hosting a copy of this script yourself, if you so wish. Simply download all files, modify the main PHP script to include the URL of your team’s fixtures, and upload everything to a web server of your choosing.

You can then point your iPhone (or other calendar client) to your server, and everything should work nicely. For example, if you have uploaded everything to a folder on yoursite.com called fixtures, your calendar URL will be:

http://www.yoursite.com/fixtures

Simple.

Download

All code can be downloaded from GitHub. Please note that the PHP Simple DOM HTML Parser and iCalCreator are entirely the work of two separate third parties, and they retain full copyright of and credit for their work.

This is the first time we’ve actually “released” code, and although it is relatively tidy, it could perhaps be better documented, or better follow accepted standards. Any feedback or suggestions, on how any aspect of the code could be improved, will be gratefully received.

Chris Gibson

Posts

My Google Profile

24 responses to Generating Football Fixture Calendars in iCal (ICS) format

  1. Hi.
    I think this is just what I’m looking for.

    I have a pc and an iPhone and I would like to be able to automaticly update my iPhone calendar with my girlfriends irregular shift patten (rather than copying it in every few weeks) I her shift patten is published on the Internet.

    For this code to work do I need to set up a website first?

    • Chris Gibson June 13, 2012 at 13:18

      Yes. You’ll need to set up a website that hosts this PHP script. You’ll also need to heavily modify the code, so that it reads in your girlfriend’s shift pattern, rather than a fixture list from the BBC website.

      Unfortunately, unless you are pretty comfortable writing PHP and reading HTML, you will not be able to get this to work.

  2. Thanks for this! Now got my fixtures in ical format and into my calendars!

  3. Thanks this is perfect for a customer of mine – incidently they are a man utd group so i didnt have to change anything!

    Thanks

    Dave

  4. Firstly, thanks for the work that’s gone into this.

    I did have this working for a couple of weeks but it appears to have broken in the last few days, I think BBC may have changed the structure of the webpage again recently?

    As far as I can tell, the iCal is still being generated but all matches have the following start & end date/times.

    DTSTART:19700101T010000
    DTEND:19700101T024500

    I’ll have a poke around in the code & website to see if I can narrow it down a bit more but I’m no expert.

    • Chris Gibson July 14, 2012 at 13:47

      You’re absolutely right; I’ll look into this and see what the problem is. As you say, it’s almost certainly due to a change in the HTML structure on the BBC site. Thanks for the heads up.

    • Chris Gibson July 14, 2012 at 14:55

      This has now been addressed, thanks again for flagging this. The BBC now list the full date of each fixture, including the year, and the script wasn’t handling it properly.

      The updated version is now on GitHub. It also uses the DateTime class for date-parsing, which is more reliable (although PHP5 only).

  5. Thanks for looking at it so quickly but unfortunately I still can’t get it working again.

    I uploaded the new file but now the original webpage I set up does not generate an ics file at all, no errors are displayed.

    I thought it might be to do with php versions (although the original site claims to run the latest version of php), so I set up a site with different webhoster (Zymic, who say php version 5.2.12).

    This time the ics file is generated but it just contains the “Cannot load data\; check for updates” message.

    • Chris Gibson July 15, 2012 at 23:17

      Looks like the DateTime class needs PHP 5.3 or higher. I’ll take a look at this if I get chance; DateTime isn’t essential, it’s just more robust. There may be a perfectly good workaround for earlier versions of PHP, I’ll upload a fix if I can find one.

  6. Thanks Chris for your help on this. I have just noticed this issue myself after I updated my site with the latest TV Fixtures and found that the ICS file wasnt working.

    Much Appreciated

    Dave Large

  7. Chris Gibson July 16, 2012 at 21:46

    I’ve now updated the code on GitHub to support versions of PHP lower than 5.3. Thanks again to everyone who provided feedback and bug reports.

  8. Me again, it seems they’ve got it in for us and have changed the date format again! :-(

    It’s now some day date month format which has broken the generation of the calendar file.

    Not sure if Chris has got the time (or patience) to faff around doing another fix so soon after last time? ;-)

    • Yes, I spotted this the other day! Very annoying, I wish they’d stop meddling. Either way, I’ve updated the GitHub repository with a newer version of the script that should work fine, including PHP 5.3 and lower.

      It’s never a difficult amend, and I’ll always fix this ASAP; I use the code myself, as does my wife! It’s just difficult to anticipate how the date format will change, and writing something that will handle *all* possible formats is tricky.

  9. Thanks for the prompt update Chris, all working fine again now.

    Hopefully they wont mess with it again this season!

  10. I am getting the message Cannot load data; check for updates
    Followed the instructions and didn’t need to change it from Man Utd.

    • That error indicates that you’re not loading any data at all, which is unusual.

      If you remove the “@” from the following line, that should display a more detailed error message, which may help you debug your installation?

      if ($html = @file_get_html($url)) {

  11. I have uploaded the entire contents of the directory, but it fails to load any fixtures, even when left with all default values. Any suggestions?

    I am getting the cannot load data check for updates message.

    • As per an earlier comment, this error indicates that you’re not loading any data at all, which is unusual.

      If you remove the “@” from the following line, that should display a more detailed error message, which may help you debug your installation?

      if ($html = @file_get_html($url)) {

      Let me know if this helps.

      • It is not displaying any further information despite removing the @ from said line.

        • I’m not sure why that would be the case; if you have error messages enabled on your server, you should see some form of error displayed without the ‘@’ sign. It might be worth checking that you have enabled fopen wrappers, this will be necessary to load content from the BBC URL.

  12. Hello!
    Just found this on a search . Does it work for bbc fixtures 13/14 before I look into how this all works?
    Thanks!

    • Chris Gibson June 24, 2013 at 11:44

      Yes, this works fine with the latest fixture list; within about ten minutes of the fixtures being released, my iPhone had imported them all automatically. This may change if the BBC modifies their site again, but for now it’s working very well.

      • Hi – Great Solution for Football fixtures

        I found by adding the following 2 lines before line 108 in the index.php file I can then subscribe to my calendar in Google Calendar (Other Calendars > downarrow > Add By URL)

        header(‘Content-type: text/calendar; charset=utf-8′);
        header(‘Content-Disposition: attachment; filename=’ . $filename);

        line 108 = $config = array(‘unique_id’ => ‘sevenpointsix’);

Leave a Reply to Chris Gibson Cancel reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>