#!/usr/bin/perl #(c) 2005 Christopher Uriarte # rssblogspot.cgi - parses a blogspot atom feed to show the most recent MAXENTRIES, links and date added use LWP::Simple; use XML::Parser; #### #Static Values #### $MAXENTRIES = 4; $url = 'http://chrisjur.blogspot.com/atom.xml'; #Tag Tracker my $thistag; #Track if we're in an block my $entryflag; #count the number of entries my $count = 0; #### #Retrieve XML Date #### my $data; $SIG{ALRM} = \&timed_out; eval { alarm(15); #get the base URL $data = get($url); alarm(0); }; sub timed_out { my $time; $time = localtime(); die "$time: Timed out while accessing feed at $url\n"; } my $parser = new XML::Parser(ErrorContext => 2); $parser->setHandlers(Start => \&start_handler, End => \&end_handler, Char => \&char_handler); $parser->parse($data); sub start_handler { my $expat = $_[0]; my $element = $_[1]; if ($element eq "entry") { $count++; #print "Count is now $count\n"; $entryflag = 1; } #Grab the title and href element of the second "link" tag #exclude service.edit links if ( ($element eq "link") and ($entryflag == 1) and ($_[5] eq "alternate") ) { $ENTRIES{$count}->{title} = $_[7]; #print "Title: $_[7]\n"; $ENTRIES{$count}->{link} = $_[3]; #print "Link: $_[3]\n"; } if ($element eq "issued" && $entryflag == 1) { $thistag = "issued"; #print "Added: "; } } sub char_handler { my ($p, $data) = @_; #print the modified date if ($thistag eq "issued" && $entryflag == 1) { $date = substr($data,0,10); #print "$date\n"; $ENTRIES{$count}->{date} = $date; $thistag = ""; } 1; } sub end_handler { my $expat = shift; my $element = shift; if ($element eq "entry") { $entryflag = 0; #print "\n\n"; } 1; } #Determine how many entries to display if ($MAXENTRIES < $count) { $MAX = $MAXENTRIES; } else { $MAX = $count; } for ($c=1; $c<=$MAX; $c++) { #print "Loop is $c, count is $count\n"; $title = $ENTRIES{$c}->{title}; $link = $ENTRIES{$c}->{link}; $date = $ENTRIES{$c}->{date}; print "-$title (added: $date)
\n"; }