#!/usr/bin/perl -w use XML::FeedPP; use XML::LibXML; use XML::Rules; use LWP::Simple; use strict; use warnings; # get the Atom feed from the USGS my $source = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.atom'; my $feed = XML::FeedPP->new( $source ); # Incorporating a rules section so that we can extract the information from the CDATA section # and any other specific fields we might need. First attempt will be to pull the 'Updated' # field data from the Atom feed. I tried to follow the example in the CPAN summary but I'm # clearly lost. my @rules = ( _default => sub {$_[0] => $_[1]->{_content}}, id => sub {$_[0] => $_[1]->{_content}}, # author => undef, # ignoring the author because I already know it's the USGS author => sub {$_[0] => $_[1]->{_content}}, updated => sub {print "$_[1]->{updated}\n"; }, ); my $parser = XML::Rules->new(rules => \@rules); my $atom = $feed->to_string(); $parser->parse( $atom ); # This section extracts the title field, then performs string manipulations to extract the # long location data and the magnitude. Funny that USGS does not have a magnitude field # in this feed. foreach my $quake( $feed->get_item() ) { my $title = $quake->title(); my $place = substr($title, 8); my $magnitude = substr($title, 2,3); my $id = $quake->get( "id"); my $update = $quake->get( "updated" ); my $location = $quake->get( "georss:point"); print "Magnitude ", $magnitude, " about ", $place, "\n"; print "id is $id, updated at $update, georss:point is $location \n"; }