Events API Example: Publishing Events to Eventful.com - Perl Script

Options

Thes are the general steps and the perl script written to publish events created in Convio to eventful.com. This is just an example of what can be done with the Events API.

#!/usr/bin/perl

# This gets events from a Convio-powered site using the REST API and then puts

# them on the eventful.com site.

# It selects events with a certain interest category (initially hard-coded to

# "eventful") for publishing to eventful.com.

# It uses a DBM database (id_map.pag and id_map.dir files are created) to keep

# track of the mapping of Convio event IDs to eventful event IDs.

#

# It does not currently handle removing events from eventful if they are removed

# from the Convio-powered site.

# I tried organizing all eventful events in an eventful "calendar" so that I

# could get the list of all events in that calendar to identify ones that were

# no longer in the Convio-powered site's listing. However, the "calendar"

# functionality of eventful.com does not seem to function correctly.

# It might be possible to create an eventful "venue" for the organization and

# associate all events with it. Then the venue ID could be used in the event

# search API to get all eventful events for the organization and look for ones

# that need to be removed.

#

# Before using this:

# 1) Create an account at eventful.com and request an API key. Store that in eventful_api_key below.

# 2) Store your user name and MD5 hash of your password in eventful_user_name and eventful_password_md5 below.

# 3) Create a calendar at eventful.com and store its ID in cal_id below.

# 4) Update the url_path and convio_api_key variables below for invoking the Convio Events API

# 5) Create a Convio interest category to assign to Convio events and store in eventful_interest_cat below.

# Only events with this interest are sent to the eventful site.

# 6) Install required perl libraries:

# perl -MCPAN -e 'install XML::XPath'

# perl -MCPAN -e 'install XML::XPath::XMLParser'

# perl -MCPAN -e 'install WebService::Eventful'

use XML::XPath;

use XML::XPath::XMLParser;

use WebService::Eventful;

require LWP::UserAgent;

# Base file name for Convio -> Eventful event ID mapping

my $id_map_file = "id_map";

my $debug = 0;

# Convio API parameters

my $url_path = "http://your_org_domain/site"; # Using SSL requires package Crypt::smileyfrustrated:SLeay

my $convio_api_key = "your_site_api_key";

my $eventful_interest_cat = "eventful";

# eventful API parameters

my $eventful_api_key = "alphabet_soup_from_eventful";

my $eventful_user_name = "XXXXX";

my $eventful_password_md5 = "md5_hash_of_eventful_password";

my $cal_id = "C#-###-#########-#"; # ID of calendar created manually

# Handle to eventful.com API.

# Call eventfulLogin() to establish.

my $eventfulAPI;

# id_map maps Convio event ID to eventful event ID

my %id_map = ();

dbmopen (%id_map, $id_map_file, 0666) or die "Can't open ID DB file";

# Loop through all months this year

for $mon (1..12) {

postConvioEventsToEventful ("2009", $mon);

}

# Get Convio events for given year and month and add or update them in eventful.

sub postConvioEventsToEventful {

local ($year, $month) = @_;

my $uri = URI->new($url_path . "/CROrgEventAPI");

%get_events_args = (

"v", "1.0",

"api_key", $convio_api_key,

"method", "getMonthEvents",

"month", $month,

"year", $year,

"interests", $eventful_interest_cat,

);

$uri->query_form(\\%get_events_args);

$debug && print $uri . "\\n";

# Get events

my $ua = LWP::UserAgent->new;

my $response = $ua->get($uri);

if ($response->is_success) {

$debug && print $response->decoded_content . "\\n\\n";

my $xp = XML::XPath->new (xml => $response->decoded_content);

my $evList = $xp->find("/getMonthEventsResponse/event");

foreach my $event ($evList->get_nodelist) {

my $convio_id = $xp->findvalue("id", $event);

my %args = makeEventArgs ($xp->findvalue("name", $event),

$xp->findvalue("description", $event),

$xp->findvalue("eventUrl", $event),

$xp->findvalue("eventDate/startDate", $event),

$xp->findvalue("eventDate/startTime", $event),

$xp->findvalue("eventDate/endDate", $event),

$xp->findvalue("eventDate/endTime", $event),

$xp->findvalue("attendanceOption", $event));

syncEventfulEvent ($convio_id, \\%args);

}

}

else {

die $response->status_line;

}

}

# Log in to eventful.com

sub eventfulLogin {

if (!$eventfulAPI) {

$eventfulAPI = WebService::Eventful->new(app_key => $eventful_api_key);

$eventfulAPI->login(user => $eventful_user_name, password_md5 => $eventful_password_md5)

or die "Can't log in: $WebService::Eventful::errstr";

}

return $eventfulAPI;

}

# Given an eventful event ID, return the eventful event

sub getEventfulEvent {

local($eventful_id) = @_;

my $evAPI = eventfulLogin();

my $event = $evAPI->call("events/get", {id => $eventful_id})

or die "Can't get event: $WebService::Eventful::errstr";

return $event;

}

# Given a Convio event ID, find corresponding eventful event

sub findEventfulEvent {

local($id) = @_;

my $eventful_id = $id_map{$id};

print "findEventfulEvent " . $eventful_id . "\\n";

return $eventful_id && getEventfulEvent($eventful_id);

}

# Given an eventful event ID, add the event to the eventful calendar

sub addEventfulEventToCalendar {

local($eventful_id) = @_;

my $evAPI = eventfulLogin();

my $response = $evAPI->call("calendars/events/add", {calendar_id => $cal_id, event_id => $eventful_id})

or die "Can't add event to calendar: $WebService::Eventful::errstr";

}

# Given an eventful event ID and URL, add link to the event

sub addUrlToEventfulEvent {

local($eventful_id, $url) = @_;

my $evAPI = eventfulLogin();

my %args = (

"link", $url,

"id", $eventful_id,

"link_type_id", "17" # 17 = Official Site

);

my $response = $evAPI->call("events/links/new", \\%args)

or die "Can't add link to event: $WebService::Eventful::errstr";

}

# Synchronize a Convio event with an eventful event.

# If event exists in eventful, then update it, otherwise create it.

sub syncEventfulEvent {

my $convio_id = shift(@_);

my $args = shift(@_);

my %args_hash = %$args;

my $event = findEventfulEvent ($convio_id);

# Add it if not found, else update it

if (!$event) {

$event = addEventfulEvent ($convio_id, \\%args_hash);

}

else {

my %event_hash = %$event;

$args_hash{'id'} = $event_hash{'id'};

updateEventfulEvent ($convio_id, \\%args_hash);

if (!$event_hash{'calendars'}) {

addEventfulEventToCalendar ($event_hash{'id'});

}

}

}

# Builds the arguments for adding or modifying an event.

sub makeEventArgs {

local($title, $description, $url, $start_day, $start_time, $end_day, $end_time, $attendance) = @_;

$start = substr($start_day, 0, 10) . "T" . $start_time;

if ($end_day eq "") {

$end_day = $start_day;

}

$end = substr($end_day, 0, 10) . "T" . $end_time;

my $freeness = ($attendance eq 2) ? "0" : "1";

%event_args = (

"calendar_id", $cal_id,

"title", $title,

"description", $description,

"start_time", $start,

"stop_time", $end,

"all_day", "0", # not all-day

"privacy", "1", # public

"free", $freeness,

"url", $url,

);

return %event_args;

}

# Adds an event to eventful

sub addEventfulEvent {

my $convio_id = shift(@_);

my $args = shift(@_);

my %args_hash = %$args;

my $evAPI = eventfulLogin();

my $event = $evAPI->call("events/new", \\%args_hash)

or die "Can't create event: $WebService::Eventful::errstr";

my %event_hash = %$event;

my $eventful_id = $event_hash{'id'};

print "Added eventful event " . $eventful_id . " for Convio event " . $convio_id . ".\\n";

$id_map{$args_hash{'id'}} = $eventful_id;

addEventfulEventToCalendar ($eventful_id);

addUrlToEventfulEvent ($eventful_id, $args_hash{'url'});

}

# Updates existing eventful event

sub updateEventfulEvent {

my $convio_id = shift(@_);

my $args = shift(@_);

my %args_hash = %$args;

my $evAPI = eventfulLogin();

my $response = $evAPI->call("events/modify", \\%args_hash)

or die "Can't update event: $WebService::Eventful::errstr";

print "Updated eventful event " . $args_hash{'id'} . " for Convio event " . $convio_id . ".\\n";

}

sub dumpObject {

local($obj) = @_;

dumpHash(0, %$obj);

}

sub dumpHash {

local($indent_num, %hash) = @_;

$debug || return;

while (my ($k, $v) = each %hash) {

if (ref($v) eq "HASH") {

printf ("%${indent_num}skey: %s, hash:\\n", "", $k);

dumpHash($indent_num + 4, %$v);

}

else {

printf ("%${indent_num}skey: %s, value: %s.\\n", "", $k, $v);

}

}

}

Tagged:

Categories