In the previous article we started implementing the geo.getEvents Last.FM API call. In this article we will complete the implementation of the Event class and create a small application to display events for a specific location in a compact format.

Implementing the <event/> XML chunk extractor

So let's move on to the WWW::LastFM::Response::Event class. This is one of the core classes for our particular problem domain. Everything up to here has just been stuff needed to build an extensible framework for sending our queries to the Last.FM API and extract values from the response data. Let's get on with it. Create lib/WWW/LastFM/Response/Event.pm with this content:

package WWW::LastFM::Response::Event; use XML::Rabbit; use HTML::FormatText; use DateTime::Format::HTTP; # Last.FM-specific data has_xpath_value 'id' => './id'; has_xpath_value 'tag' => './tag'; has_xpath_value 'url' => './url'; # The actual event-related data has_xpath_value 'title' => './title'; has_xpath_value 'description_html' => './description'; has_xpath_value 'website' => './website'; has_xpath_value 'start_date' => './startDate'; has_xpath_value 'headliner' => './artists/headliner'; has_xpath_value_list '_artists' => './artists/artist', handles => { 'artists' => 'elements', } ; has_xpath_object 'venue' => './venue' => 'WWW::LastFM::Response::Venue'; has_xpath_value_list '_tags' => './tags/tag', handles => { 'tags' => 'elements', }, ; # Some various attendance-related data has_xpath_value 'ticket_count' => './tickets'; has_xpath_value 'attendance' => './attendance'; has_xpath_value 'cancelled' => './cancelled'; has_xpath_value 'review_count' => './reviews'; # Event image URLs has_xpath_value 'image_s' => './image[@size="small"]'; has_xpath_value 'image_m' => './image[@size="medium"]'; has_xpath_value 'image_l' => './image[@size="large"]'; has_xpath_value 'image_xl' => './image[@size="extralarge"]'; # The description value, converted to plain text has 'description' => ( is => 'ro', isa => 'Str', lazy_build => 1, ); sub _build_description { my ($self) = @_; return HTML::FormatText->format_string( $self->description_html, leftmargin => 0, rightmargin => 79, ); } # Convert the start_date to a DateTime class which is much more useful has 'date' => ( is => 'ro', isa => 'DateTime', lazy_build => 1, ); sub _build_date { my ($self) = @_; return DateTime::Format::HTTP->parse_datetime( $self->start_date ); } finalize_class();

There shouldn't be anything surprising in this piece of code. The only thing you haven't seen before is the use of XPath attribute values as selectors. We've already covered all the various declarations, so let's just continue with WWW::LastFM::Response::Venue, so that we can have a look at how to deal with the namespaced latitude/longtitude information. Create lib/WWW/LastFM/Response/Venue.pm with this content:

package WWW::LastFM::Response::Venue; use XML::Rabbit; # Last.FM-specific data has_xpath_value 'id' => './id'; has_xpath_value 'url' => './url'; # The actual venue-related data has_xpath_value 'name' => './name'; has_xpath_value 'website' => './website'; has_xpath_value 'phone_number' => './phonenumber'; # Location-related data has_xpath_value 'city' => './location/city'; has_xpath_value 'country' => './location/country'; has_xpath_value 'street' => './location/street'; has_xpath_value 'postal_code' => './location/postalcode'; has_xpath_value 'latitude' => './location/geo:point/geo:lat'; has_xpath_value 'longitude' => './location/geo:point/geo:long'; # Venue image URLs has_xpath_value 'image_s' => './image[@size="small"]'; has_xpath_value 'image_m' => './image[@size="medium"]'; has_xpath_value 'image_l' => './image[@size="large"]'; has_xpath_value 'image_xl' => './image[@size="extralarge"]'; # Convenience attribute for combined address has 'address' => ( is => 'ro', isa => 'Str', lazy_build => 1, ); sub _build_address { my ($self) = @_; return $self->name . ', ' . ( $self->street ? $self->street . ', ' : "" ) . ( $self->postal_code ? $self->postal_code . ' ' : "" ) . $self->city . ', ' . $self->country; } finalize_class();

This is just more of the same things we've been doing. Notice the simplicity in dealing with namespaces in the XPath queries for latitude and longitude? It's really that simple.

lastfm_events.pl, a simple WWW::LastFM client

Let's finish it up by making a fairly simple command line app to search for events. Create bin/lastfm_events.pl with the following content:

#!/usr/bin/env perl use strict; use warnings; use rlib; use feature qw(say); use WWW::LastFM; use Getopt::Long; # PODNAME: lastfm_eventss.pl # ABSTRACT: Search the Last.FM API for events/concerts STDOUT->binmode(":utf8"); # Read command line flags my $limit; my $distance; my $help; GetOptions( "limit=i" => \$limit, "distance=i" => \$distance, "help" => \$help, ); die <<"EOM" if $help; Usage: $0 [<options>] [<location>] Options: --limit <num> ; Number of events to return --distance <km> ; How large area to search EOM my $location = shift; # Perform query my $event_list = WWW::LastFM->new->geo->get_events( ( defined $limit ? ( limit => $limit ) : () ), ( defined $distance ? ( distance => $distance ) : () ), ( defined $location ? ( location => $location ) : () ), )->events; my @events = $event_list->all; # Format and output results if ( @events > 0 ) { foreach my $event ( sort { $a->date cmp $b->date } @events ) { my $formatted_date = DateTime::Format::HTTP->format_datetime($event->date); my $headliner = $event->title ne $event->headliner ? " with " . $event->headliner : ""; my @additional_artists = grep { ! $event->headliner } $event->artists; say $formatted_date . ": " . $event->title . $headliner . ( scalar @additional_artists ? " and " . join(", ", @additional_artists ) : "" ) . " @ " . $event->venue->address; } } else { say STDERR "No events found for location '" . $event_list->location . "'"; } 1;

Let's have some fun with it!

$ bin/lastfm_events.pl --distance 50 --limit 50 Tønsberg Fri, 04 Nov 2011 19:30:00 GMT: Melissa Horn @ Kulturhuset Bølgen, Larvik, Norway Fri, 18 Nov 2011 19:00:00 GMT: Konsert, Bjørn Eidsvåg med band with Bjørn Eidsvåg @ Parkteatret, Moss, Norway Fri, 16 Dec 2011 19:00:00 GMT: Helene Bøksle på Domkirkefestivalen i Tønsberg Domkirke with Helene Bøksle @ Tønsberg Domkirke, Tønsberg, Norway Fri, 16 Dec 2011 19:31:01 GMT: Ljungblut @ Total, Stoltenbergsgata 46, 3110 Tønsberg, Norway Sat, 17 Dec 2011 19:34:01 GMT: Ljungblut @ Total, Stoltenbergsgata 46, 3110 Tønsberg, Norway

This shows the events happening around where I live. Not really much to be cheerful about. Hopefully you'll get some more interesting results for where you live.

Conclusion

I wrote this article mostly to showcase how easy it is extract useful information from XML documents with XML::Rabbit and augment it with additional Moose magic. I don't really go to concerts very often, but I figured it was a useful example that some of you would find interesting (instead of just using some hand-crafted example XML).

If you want to see additional API calls implemented I urge you to get in touch with me and help me to improve it. I've shown you how I've done it this far. With some help from various Perl learning resources I'm sure you'd be able to whip up a patch with some additional features (or tests). Please follow me on Twitter or GitHub or subscribe to my blog if you want to stay informed with what I'm doing.