#!/usr/bin/perl #(c) 2005 Christopher Uriarte # rssflckr.cgi - parses a flckr atom feed to show the most recent MAXENTRIES, links and date added use LWP::Simple; use XML::Parser; #### #Static Values #### $MAXENTRIES = 5; $url = 'http://www.flickr.com/services/feeds/photos_public.gne?id=64426228@N00&format=atom_03'; #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(30); #get the base URL $data = get($url) || die "Can't retrieve feed at $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); #We start here when we encounter an HTML Tag sub start_handler { my $expat = $_[0]; my $element = $_[1]; #print "Encountered element $element\n"; #If we enter an tag, we have a new element #Increase the county by 1 and set the entry flag to 1 if ($element eq "entry") { $count++; $entryflag = 1; #print "Count is now $count and entryflag is set to $entryflag.\n"; } if ($element eq "title" && $entryflag == 1) { $thistag = "title"; } #Grab the title and href element of the second "link" tag #exclude service.edit links, we want the "alternate" tag link #print "Encountering Element=$element,entryflag=$entryflag,dollar_1,2,3=$_[1],$_[2],$_[3] \n"; if ( ($element eq "link") and ($entryflag == 1) and ($_[3] eq "alternate") ) { $ENTRIES{$count}->{link} = $_[7]; #print "Link: $_[7]\n"; } if ($element eq "issued" && $entryflag == 1) { $thistag = "issued"; #print "Added: $_"; } } #This is where we handle the values within the tag sub char_handler { my ($p, $data) = @_; #print the modified date if ($thistag eq "issued" && $entryflag == 1) { #Get the first 11 Chars of the date, that's all we care about $date = substr($data,0,10); #print "$date\n"; $ENTRIES{$count}->{date} = $date; $thistag = ""; } if ( ($thistag eq "title") and ($entryflag == 1) ) { $ENTRIES{$count}->{title} = $data; $thistag = ""; } 1; } sub end_handler { my $expat = shift; my $element = shift; #If we're at the end of an block, clear the entry flag if ($element eq "entry") { $entryflag = 0; #print "\n\n"; } 1; } #Determine how many entries to display #If our set maximum amt of entries is less than what we encountered #we only display up to $MAXENTRIES if ($MAXENTRIES < $count) { $MAX = $MAXENTRIES; } #Otherwise, we display what we encountered else { $MAX = $count; } #Map through the %ENTRIES hash from 1 to $MAX to display the links 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"; }