Created
November 24, 2020 09:37
-
-
Save JohnMertz/0ede3c2ec1a15336eb2d64fd35f0a981 to your computer and use it in GitHub Desktop.
Daily podcast player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# Dependencies | |
use LWP::UserAgent; # Usually pre-installed, available on CPAN | |
use XML::Hash::XS; # CPAN or libxml-hash-xs-perl on Debian | |
# Feeds that should be used if no arguments are provided | |
my @default_feeds = ( | |
# CBC FrontBurner | |
"https://www.cbc.ca/podcasting/includes/frontburner.xml", | |
# Majority Report AM Quickie | |
"https://feeds.fans.fm/5883fe04-e11e-4578-a018-3b93ddfb5723.xml" | |
); | |
# Player command (must accept stream as STDIN) | |
my $player = "mpv -"; | |
# Multiple feeds can be given as CLI arguments instead | |
my @feeds; | |
if (scalar @ARGV) { | |
foreach (@ARGV) { | |
push @feeds, $_; | |
} | |
} | |
# Only use default feeds if no CLI feeds were given | |
unless (scalar @feeds) { | |
@feeds = @default_feeds; | |
} | |
# Local function to get date pattern provided as 'pubDate' | |
my $today = get_date(); | |
# Setup Webpage agent and XML parser | |
my $ua = LWP::UserAgent->new(); | |
my $xml = XML::Hash::XS->new(utf8 => 0, encoding => 'utf-8'); | |
my @playlist; | |
# Go through each feed | |
foreach my $url (@feeds) { | |
# Ensure that it's actual an XML link | |
next unless $url =~ m#^https?://.*\.xml$#; | |
# Ensure it is fetched okay | |
my $raw = $ua->get("$url")->content() || next; | |
# Ensure it is parsed okay | |
my $xml_hash = $xml->xml2hash($raw) || next; | |
# Collect all episodes published today | |
foreach my $item (@{$xml_hash->{'channel'}->{'item'}}) { | |
if ($item->{'pubDate'} =~ m/$today/) { | |
# Add any found to playlist | |
push @playlist, $item->{'enclosure'}->{'url'}; | |
} | |
} | |
} | |
# Fetch each item and pass it to player via STDIN | |
foreach (@playlist) { | |
# Uncomment the following to print the media URL | |
#print $_ . "\n"; | |
system("curl -NL $_ | $player"); | |
} | |
sub get_date | |
{ | |
my ($second, $minute, $hour, $day, $month, $year) = localtime(time); | |
my @months = ( | |
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' | |
); | |
return $day . ' ' . $months[$month] . ' ' . ($year+1900); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment