Sep26

XML Mass Importer for Google Calendar

This ia a easy-to-use PHP class to import Google events from custom XML files.
Requirements

My wrapper uses parts of the Zend framework. If you use Gentoo just let portage do that task: emerge -av dev-php5/ZendFramework.
I’m sure that other distributions also maintain packages for that framework. If not, you’ve to install the Zend Framework by hand like desribed here.

You need at least a running PHP interpreter (here: dev-lang/php-5.2.13).

Import XML

In order to start, connect and login to the Google calendar service:

include "lib/Import.class.php";

$CalendarID = "id@group.calendar.google.com";
$Username = "username";
$Password = "password";

// Login to Google service
$Import = new Import($Username, $Password);

Before we create an event, we’ve to organize data. This is done by creating xpath mapping between your custom XML and the expected attributes:

// Define some templates (map event attributes to Google event attributes)
$Import->Title =</staffel/spiel/teamheim></staffel/spiel/teamgast> (</staffel/liganame>);
$Import->Location =</staffel/spiel/teamheim>, </staffel/spiel/halle>;
$Import->Start =</staffel/spiel/datum> </staffel/spiel/uhrzeit>;
$Import->Duration = 120; // 2 hours
$Import->UniqueKey =</staffel/spiel/datum> </staffel/spiel/uhrzeit> </staffel/spiel/halle>;

As you see, I try to import dates for a national volleyball league. Fortunately they provide some XML exports like the following:

<staffel>
<liganame>Bezirksklasse 1 Herren, Süd</liganame>
<spiel>
<datum>09.10.2010</datum>
<uhrzeit>15:00</uhrzeit>
<halle>Otto-Hahn-Gymnasium (OHG)</halle>
<teamheim>SSC Karlsruhe III</teamheim>
<teamgast>TSG Blankenloch II</teamgast>
<ergebnis>0:0</ergebnis>
<ballpunkte>0:0</ballpunkte>
</spiel>
</staffel>

If you are oblivious, just add some reminders:

$Import->AddReminder(Import::REMINDER_POPUP, Import::REMINDER_ONE_DAY * 3);

Finally we can start adding our stuff. This can take several time, so please make sure that you set PHP’s executable environment accordingly:

// Read XML file containing data for events
$Import->Read("http://www.volleyball-nordbaden.de/xml_export/spiele_1665.xml");

// Add events to Google calendar
$Number = $Import->Add("http://www.google.com/calendar/feeds/$CalendarID/private/full", "//spiel", Import::CONFLICT_SHIFT);

echo "Imported $Number events!";

Sources

You can checkout the latest version from Google code:
svn checkout http://matchimport.googlecode.com/svn/trunk/ matchimport-read-only.
There’s an example script run.php which contains the basic steps to initialize a mass-import.

The source is under Free BDS license, so feel free to modify the code.
Visit the Google code project for more details.

Leave a Reply

You must be logged in to post a comment.