A while ago I wrote a podcatcher in Perl. In the last few days I've finally gotten around to cleaning it up a bit, finishing the documentation, and getting it out where people can use it (on my website for now -- maybe I'll try to submit it to CPAN at some point).
The full code (and associated files) can be found at http://jimhenry.conlang.org/software/podcatcher.zip and the documentation (including per-function summaries) at http://jimhenry.conlang.org/software/podcatcher.html
Here, I'll just briefly discuss one of the functions that gave me some trouble, given the variety of podcast RSS feeds out there and how weirdly (sometimes invalidly) formatted some of them are.
This function is passed an RSS feed as a single string and attempts to extract the podcast episode URLs from it. First it tries to parse the RSS using XML::RSS::LibXML. Then, if that worked, it tries to find episodes in <enclosure> tags, then if that fails, it tries looking in <media:content> tags. If it failed to parse the RSS file, or if it parsed and failed to find any podcasts in the appropriate tags, it does a brute force regular expression match on the whole RSS file to find anything that starts with http and ends with one of the file extensions we're looking for (which is configurable).
sub get_mp3_links_from_string {
my $pagecontent = shift;
my @episodes;
my $parser = XML::RSS::LibXML->new;
# for some bizarre reason, putting curly brackets around this eval
+ generates
# syntax errors. use q// instead.
eval q/ $parser->parse($pagecontent) /;
if ( $@ ) {
writelog "Could not parse page as XML/RSS: $@\n";
$parser = undef;
}
if ( $parser ) {
foreach my $item (@{ $parser->{items} }) {
my $ep;
if ( defined $item->{enclosure} ) {
if ( $ep = $item->{enclosure}{url} and $ep =~ m!$extension_reg
+ex$! ) {
push @episodes, { url => $ep };
} elsif ( $ep = $item->{media}{content}{url} and $ep =~ m!$ext
+ension_regex$! ) {
push @episodes, { url => $ep };
}
next if not $ep;
} else {
next;
}
if ( $config{description} ) {
$episodes[ $#episodes ]->{title} = $item->{title};
$episodes[ $#episodes ]->{description} = $item->{description};
}
} # end for each <item>
} # end if we have a valid parse
unless ( @episodes ) {
writelog "Found no $config{extensions} files by parsing XML, check
+ing via regex for any $config{extensions} links in any context\n";
my @mp3s = uniq ( $pagecontent =~ m/(http[^\s>]+$extension_re
+gex)/gi );
return undef unless ( @mp3s );
foreach ( @mp3s ) {
push @episodes, { url => $_ };
}
}
return \@episodes; # @mp3s;
}