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; }

In reply to A podcatcher in Perl by jimhenry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.