Most people think of Google as 'just' a search company, but in recent years it's started to provide free versions of applications that were previously only available as standalone desktop programs.
The number of people using Google for email, scheduling, word processing and creating spreadsheets is ever-increasing, but websites are just one way of accessing all these applications.
Behind the scenes, most of what's available from Google through web pages is also available via programming.
What are web services?
Web services are akin to very simple websites – sites that are designed to be used by computers instead of humans.
Imagine that you write a script that calls a web service. The script makes a 'request' to the service by sending information (usually in XML) to a web address. The service usually responds with some XML text instead of returning a HTML web page.
For example, a search engine might allow you to send it some search terms and respond to those with an XML file that lists the names and locations of web pages matching your criteria.
That's a simple case, but as websites have grown more powerful and similar to desktop applications, web services have begun to offer a longer list of features. They've grown to the point that many of them are now serving a similar function to the role macro languages such as VBA once served in Microsoft Office and other products.
Connecting services
The example application we're going to build here connects two different sets of web services together. One is from the BBC, the other from Google.
The BBC offers a lot of its listing information for free via web services. If you have a TV card in your PC that is able to automatically pick up programme details, it may well be using one of these BBC services.
Google exposes almost everything it does as web services. It's part of its plan to become the container for all information on the planet, and Google has shrewdly given users as much access as possible to the data and applications that it hosts.
By connecting these two web services, we're able to solve a hypothetical problem: you want to watch documentaries about the countryside, but whenever you turn on the TV, you're unable to find what you want and end up watching something else. This TV scheduling project stops this from happening.
You can filter the BBC's listings for just the things you want, then post an event for each programme on Google Calendar to remind you when to tune in for the shows.
Backstage pass
The first set of web services we'll look at is run on the BBC's Backstage website. This is an experimental system that was launched three years ago, and it makes a lot of BBC data available in the hope that people come up with interesting and innovative ways of using it. All of the documentation for the Backstage web services can be found here: www.tinyurl.com/2af4vw.
The script that handles the BBC service is called 'bbc_progs.rb'. Although there are plenty of services on offer, we're only going to use three of them. First of all, we'll call 'bbc.genre.getMembers'. If you send this service the code for a particular genre of programme (such as documentaries), it returns an XML description of all programmes of that type currently listed on its database.
Next, we're going to call 'bbc.programme.getLocations'. This returns a set of addresses where you're able to watch or listen to the programme on the web. In reality, the addresses returned don't always work – most of the radio links are fine, but the majority of the TV links are wrong. It seems likely that, over time, more TV will be streamed on the web, so we'll record them here just in case.
Finally, we're going to call 'bbc.programme.getInfo'. For a given programme, the getInfo service returns a lot of extra information, such as a summary of the content and the channel that it's aired on. We'll use this information to fill out the detailed description that we're posting to Google Calendar.
The Google code
Google's web services are known as the Google Data Application Programming Interfaces (APIs). That's a mouthful, so they're often called the GData services for short. Nearly any programming language can make use of them, but for this project we've chosen to use Ruby.
The code in 'google_calendar. rb' provides the basics for posting new events to Google Calendar. But you don't need to understand the Ruby language in order to use it – most of the complexity comes from the security that's needed for a system such as Google Calendar. We're using a mechanism called ClientLogin, which is the standard way for desktop applications and scripts to connect to the GData services.
If you have software on your PC that synchronises your local calendar information with Google, it's almost certainly using ClientLogin. It works by generating a unique session reference that needs to be included in each data request that's sent to Google.
Data entry
Google provides many services for Calendar, but we'll only be using event-posting here. An event is another name for a calendar appointment. You post events by sending a long XML string to a given address. For example, you might send out this string:
<entry>
<category></category>
<title>
Life in the goldfish bowl
</title>
<content>
Reality TV show about fish
</content>
<when
startTime="2009-01-01T19:00"
endTime="2009-01-01T19:30">
<reminder minutes='5'/>
</when>
</entry>
The XML gives the event a title, which is the information you'll see in the week, day and month view of the calendar. It's also providing a longer content string to give the details of the event (displayed when you click on the event in Google Calender). Finally, the '<when>' tags give the date and time of the event, and enable you to set an alarm. In this example, our reminder is set for five minutes before the event.
With strings attached
The script that brings all of this together is called 'post_progs.rb'.
This script reads all of the programmes from the BBC that you're interested in (see the 'Choosing your programmes' box) and uses the information from each one to generate an event on Google Calendar. Most of the work involves creating a HTML string with links to each of the broadcast streams.
The great thing about publishing to Google Calendar is that you're able to get at the data wherever you are. Running the Google Notifier tool on your machine also means that your alarms consistently remind you at the right time.
If you use Outlook as a desktop calendar, then Google Calendar Sync is worth a look, because everything that gets posted to Google Calendar automatically gets synced with Outlook. It can even sync with your mobile phone if you want it to. If you run OS X, then Spanning Sync can perform a similar role on a Mac.
Going further
This has been just a taster of the things that you can do with web services and Google. If TV isn't your thing, tweak the scripts to do something you'll find more useful. Why not modify a version of the open-source MPlayer to record the radio that RealMedia streams into iTunes, for example? Or you could use the calendar interface to deploy a regular batch file that performs a backup of your machine.
With a simple scripting language and the wealth of services that are available on Google, you may find that you never have a free Sunday again.
-------------------------------------------------------------------------------------------------------
First published in PC Plus, Issue 277
Now read 101 Google tips, tricks and hacks
Sign up for the free weekly TechRadar newsletter
Get tech news delivered straight to your inbox. Register for the free TechRadar newsletter and stay on top of the week's biggest stories and product releases. Sign up at http://www.techradar.com/register
No comments