Script to look up and stream today's podcasts

This commit is contained in:
John Mertz 2020-12-01 04:59:29 -05:00 committed by John Mertz
parent a4f2b3310c
commit 8f6199cd3d
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 83 additions and 0 deletions

83
audio/stream-podcasts.pl Executable file
View File

@ -0,0 +1,83 @@
#!/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 -";
# OMXPlayer is the only one capable of playing without stutter on the pi
# It requires FIFO to stream. For a player that does support STDIN, uncomment
# above and delete the following 4 lines
unless (-e "/tmp/fifo.mp3") {
system("mkfifo /tmp/fifo.mp3");
}
my $player = "> /tmp/fifo.mp3 & omxplayer /tmp/fifo.mp3";
# 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");
}
unlink("/tmp/fifo.mp3");
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);
}