Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

A podcatcher in Perl

by jimhenry (Acolyte)
on Sep 06, 2023 at 00:39 UTC ( [id://11154272]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: A podcatcher in Perl
by jwkrahn (Abbot) on Sep 06, 2023 at 04:36 UTC
    1078: writelog printf "wget exited with value %d\n", $true_rc;

    That looks like that should be writelog sprintf

    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
Re: A podcatcher in Perl
by jwkrahn (Abbot) on Sep 06, 2023 at 22:36 UTC
    454 # ===== basic utility routines 455 456 sub writelog; 640 sub writelog($) { 641 my $arg = shift;

    Your forward declaration of writelog does not include a prototype and therefore the effects of the prototype do not apply until after it is declared.

    No prototype:

    $ perl -le' use warnings; use strict; my @x = 14 .. 19; sub writelog; writelog @x; writelog "F" .. "J"; sub writelog ($) { my $arg = shift; print "\@_ = @_\t\t\$arg = $arg"; } writelog @x; writelog "F" .. "J"; ' Useless use of range (or flop) in void context at -e line 13. @_ = 15 16 17 18 19 $arg = 14 @_ = G H I J $arg = F @_ = $arg = 6 @_ = $arg = F Argument "J" isn't numeric in range (or flop) at -e line 13. Use of uninitialized value $. in range (or flop) at -e line 13.

    With Prototype:

    $ perl -le' use warnings; use strict; my @x = 14 .. 19; sub writelog ($); writelog @x; writelog "F" .. "J"; sub writelog ($) { my $arg = shift; print "\@_ = @_\t\t\$arg = $arg"; } writelog @x; writelog "F" .. "J"; ' Useless use of range (or flop) in void context at -e line 7. Useless use of range (or flop) in void context at -e line 13. @_ = $arg = 6 @_ = $arg = F Argument "J" isn't numeric in range (or flop) at -e line 7. Use of uninitialized value $. in range (or flop) at -e line 7. @_ = $arg = 6 @_ = $arg = F Argument "J" isn't numeric in range (or flop) at -e line 13. Use of uninitialized value $. in range (or flop) at -e line 13.
    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
Re: A podcatcher in Perl
by jwkrahn (Abbot) on Sep 06, 2023 at 18:31 UTC
    $ podchecker podcatcher.pl *** ERROR: Spurious text after =cut at line 1061 in file podcatcher.pl podcatcher.pl has 1 pod syntax error.
    =cut sub check_rss_feed_via_wget {

    You need a blank line after =cut

    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker

      Thanks for the feedback. The writelog printf ... would have been hard to catch in the normal course of things because that function check_rss_feed_via_wget() only gets called under specific circumstances where LWP::UserAgent::get fails to download an RSS feed, and that only ever happened with one podast that is no longer around (and if I remember right, ceased to be a problem when I changed the user agent string). (See this old thread.)

Re: A podcatcher in Perl
by jwkrahn (Abbot) on Sep 07, 2023 at 03:03 UTC
    1309: if ( $value =~ m/^([A-Za-z0-9!#$%&'*+-.^_`|~]+) \s* : +\s* (.*)$/x ) {

    Regular expressions, like double quoted strings, interpolate scalars and arrays so the $ and @ characters have to be escaped:

    if ( $value =~ m/^([A-Za-z0-9!#\$%&'*+-.^_`|~]+) \s* : \s* +(.*)$/x ) {
    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
Re: A podcatcher in Perl
by AnomalousMonk (Archbishop) on Sep 07, 2023 at 05:34 UTC
    if ( $value =~ m/^([A-Za-z0-9!#\$%&'*+-.^_`|~]+) \s* : \s* (.*)$/x ) {

    Note also that in the
        [A-Za-z0-9!#\$%&'*+-.^_`|~]
    character class, the sub-pattern +-. represents | matches the range of characters + , - . which includes the - (hyphen) character.

    This seems a bit sus; I wonder if it was intended by the OPer.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11154272]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-05-19 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found