Show today's branch opening hours, taken from Google Calendars
Pull request Compare This branch is 9 commits behind asadaqain:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


= branchhours: Show today's branch opening hours, taken from Google Calendars


York University Libraries:

There are eight libraries at York University and we want to show
today's openings hours for all of them on our home page.  

I'd read Andrew Darby's article in Code4Lib Journal issue 2, "Using
Google Calendar to Manage Library Website Hours"
(, which gave me the idea for
using Google Calendar.  It's a great article, and includes code and
also lets you show who's on the reference desk, so have a look at it
first.  To suit local needs, I decided to write something slightly
different and do it in Ruby.

Because our calendars are public, you should be able to run this
program without any configuration.  It will show some output on STDERR
and create ten small HTML files with out opening hours.

branchhours generates files named YYYYMMDD.html.  You can include them
directly in a web page, or, for simplicity, you can just include
today.html.  Run branchhourslink every night to update the symlink
that makes today.html point to the right file.

== License

Copyright (c) 2009 William Denton <>.  Licensed under
the MIT License.  See the included LICENSE file.

== Google Calendar

The API for Google Calendar is here:

This program doesn't do any writing to calendars, just reading, so
it's dead simple.

The XML feed of the Bronfman Business Library's calendar (in the
"full" projection, with all the details) is here:

I set the calendar to be public.  If you make the calendar private
you'll have to use the private URL or authenticate, which is explained
in the docs.

Let's add some parameters to customize the calendar request:

      singleevents=true                    # expand recurring events
      ctz=America/Toronto                  # our time zone
      start-min=2009-06-22T00:00:00-04:00  # start date
      start-max=2009-07-02T00:00:00-04:00  # end date
      orderby=starttime			   # for readability
      sortorder=ascending                  # for readability

Pipe the output to xmllint so it's more readable:

curl "" | xmllint --format -

All I care about in the XML are these fields:

  <title type="text">Bronfman 10 - 6</title>
  <gd:when startTime="2009-06-26T09:00:00.000-04:00" endTime="2009-06-26T17:00:00.000-04:00"/>

If the branch is closed for the day, I make an all-day event called

  <title type="text">Closed</title>
  <gd:when startTime="2009-07-01" endTime="2009-07-02"/>

== The configuration file: branchhours.yml

googleURL: URL of a calendar feed, which will be edited to give
an exact URL for each branch's calendar.

daysInAdvance: number of days of calendar information to grab at once.

outputDir: where to put the generated HTML.

libraries: list of libraries (with short names)

For each library (eg. "bronfman") in the library array, there should
be a corresponding entry that looks like this:

    name: Bronfman Business Library
    id: m7c4318hko4aum2kmucq0g7kts

The ID is the Google Calendar ID that is the unique part of the URL
used to get the calendar feed.

template: the HTML template that will be used when files are generated
for inclusion in the home page.  If there is a library named "bronfman"
in the libaries list, then its hours will go where ::BRONFMAN:: is in
the template.

== Usage

After editing the configuration file and making sure outputDir exists,
you can probably run the program without any arguments and it will


Make it verbose to see what it's doing:

./branchhours --verbose

When you have it in production you'll probably want to run it from cron:

# Get ten (by default) days of opening hours once a week
30 7 * * Mon /path/to/branchhours --config /path/to/branchhours.yml

branchhourslink will handle symlinking so that you only ever need to
inlcude today.html in a web page.

# Update the symlink nightly to point to the right hours
55 23 * * * /path/to/branchhourslink --config /path/to/branchhours.yml --date tomorrow
# or since using today's date is the default you can just run
@midnight * * * /path/to/branchhourslink --config /path/to/branchhours.yml

[Check on redirection and add e-mail to get errors.]

[How to include in a web page]