#!/usr/bin/perl -w use XML::FeedPP; use XML::LibXML; use XML::Rules; use LWP::Simple; use Time::Piece; use strict; use warnings; # ----------------------------------------------- # open the quake marker file for writing # ----------------------------------------------- open (quake_marker, '>/Users/coblem/xplanet/markers/quake.txt') or die $!; # ----------------------------------------------- # get the Atom feed from the USGS # ----------------------------------------------- #my $source = '/Users/coblem/xplanet/quake/quake.xml'; #use this line only for local testing my $source = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.atom'; #use this line for feed testing my $feed = XML::FeedPP->new( $source ); # get the date and time. While not needed right now, this will help later with managing expired # data. # ----------------------------------------------- # get date and time, day of the year # ----------------------------------------------- my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; my $cur_time = "$wday $mday $mon $year, $hour:$min:$sec"; # ----------------------------------------------- # update the output file header # ----------------------------------------------- my $dayofYear = localtime[7]; print (quake_marker "# This Quake file created by quake_parsing_9", "\n"); print (quake_marker "# Matt Coblentz; Perl version unknown", "\n"); print (quake_marker "# For more information, see the USGS website", "\n"); print (quake_marker "# Last Updated: ", $cur_time, "\n", "\# \n"); # 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. # PerlMonk Jenda pointed out that I should use the standard rules previously defined. There were # other helpful comments about the author and update rules as well, which are now deprecated. my @rules = ( _default => 'content', dd => 'content trim', ); my $parser = XML::Rules->new(rules => \@rules); # Need to convert the feed object to a string. Thanks to PerlMonk Frozenwithjoy 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); # Thanks to PerlMonk Jakeease for fixing these extractions: my $id = $quake->get( "id"); my $update = $quake->get( "updated" ); my $location = $quake->get( "georss:point"); # *********************************************** # *********************************************** # THIS IS THE PROBLEM AREA! # *********************************************** # *********************************************** my $summary = $quake->get( "summary"); print $summary; while ($summary = read_chunk_of_data()) { $parser->parse_chunk($summary); } my $data = $parser->last_chunk(); my $dd = $data->get( "dd"); print $dd, "\n"; # print (quake_marker $location, "\"\" color=Yellow symbolsize=65", "\n"); # print (quake_marker $location, "\"", $magnitude, "\" color=Yellow align=Above", "\n"); # print (quake_marker "\n"); print (quake_marker "\n", "Magnitude ", $magnitude, " ", $place, "\n"); print (quake_marker $location, " \"\" color=Yellow symbolsize=65", "\n"); print (quake_marker $location, " \"", $magnitude, "\" color=Yellow align=Above", "\n"); print "\n"; } close (quake_marker);