Last active
August 29, 2015 13:59
-
-
Save enakai00/10447697 to your computer and use it in GitHub Desktop.
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 | |
# | |
# podgetter.pl | |
# | |
# Podcast downloader exclusively for BBC Radio | |
# | |
# 2014/04/11 v1.0 | |
# 2014/06/10 v1.1 | |
# | |
use strict; | |
#use warnings; | |
my $Download = "./download"; | |
my $Keepdays = 60; | |
my $Maxfiles = 3; | |
my ($mday, $mon, $year) = (localtime(time-60*60*24*$Keepdays))[3..5]; | |
my $Keepdate = sprintf ("%04d%02d%02d", $year+1900, $mon+1, $mday); | |
sub list_urls { | |
my @url_list; | |
my @rss_list = @_; | |
my ($rss, $url, $count); | |
foreach $rss (@rss_list) { | |
$count = 1; | |
open (IN, "curl $rss 2>/dev/null|"); | |
while (<IN>) { | |
if ($_ =~ m!<media:content url="(http://[^>]+\.mp3)"!) { | |
$url = $1; | |
$url =~ m!/[^/]+_(\d{8})-.+\.mp3$!; | |
if ($1 >= $Keepdate && $count <= $Maxfiles) { | |
$count += 1; | |
push @url_list, $url; | |
print $url. "\n"; | |
} | |
} | |
} | |
close IN; | |
} | |
return @url_list; | |
} | |
sub get_rss { | |
my @rss_list; | |
open (IN, $_[0]); | |
while (<IN>) { | |
next if ($_ =~ m/^\s*#/); | |
if ($_ =~ m!(http://.*/rss.xml)!) { | |
push @rss_list, $1; | |
} | |
} | |
close IN; | |
return @rss_list; | |
} | |
sub clean_files { | |
my ($date, $count, $title, $file); | |
my @files; | |
$title = ""; | |
$count = 0; | |
opendir(IN, $Download); | |
@files = readdir(IN); | |
closedir IN; | |
foreach $file (reverse(sort @files)) { | |
next unless ($file =~ m/(.*)_(\d{8})-.+\.mp3$/); | |
if ($1 ne $title) { | |
$count = 0; | |
$title = $1; | |
} | |
$count += 1; | |
if ($2 < $Keepdate || $count > $Maxfiles ) { | |
print "Delete: $file\n"; | |
unlink "$Download/$file"; | |
} | |
} | |
} | |
sub get_files { | |
my @url_list = @_; | |
my ($url, $file); | |
for $url (@url_list) { | |
$url =~ m!/([^/]+.mp3)$!; | |
$file = $1; | |
next if ( -f "$Download/$file" ); | |
system ("wget -O $Download/$file $url"); | |
} | |
} | |
MAIN:{ | |
my $s; | |
my (@rss_list, @url_list); | |
@rss_list = get_rss($ARGV[0]); | |
@url_list = list_urls(@rss_list); | |
get_files(@url_list); | |
clean_files(); | |
} | |
##### | |
$ cat bbcnews.txt | |
http://downloads.bbc.co.uk/podcasts/radio4/wtonight/rss.xml | |
http://downloads.bbc.co.uk/podcasts/radio4/fooc/rss.xml | |
http://downloads.bbc.co.uk/podcasts/radio4/ta/rss.xml | |
http://downloads.bbc.co.uk/podcasts/worldservice/newshour/rss.xml | |
http://downloads.bbc.co.uk/podcasts/worldservice/docarchive/rss.xml | |
http://downloads.bbc.co.uk/podcasts/radio4/today/rss.xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment