Automatically renaming files downloaded with Coast to Coast AM Media Center
I’m an insomniac.
I have trouble falling asleep at night because thoughts never seem to stop racing through my head – Did I do X yet? What if I did Y? Ooh, I could make it easier to do Y … Hey, I have a cool idea for an awesome invention… and I’m starting to get excited about it … (!)
Listening to radio shows helps me quiet my mind & get to sleep. To that end, I use the Coast to Coast AM Media Center to automatically download shows from the Coast to Coast AM website. (Sure, the content can be really nutty from time to time, but I treat it as entertainment & fiction, not a show to be taken seriously.) One of the limitations of the downloader is that the downloaded mp3s don’t have very useful names – instead of being named something useful, like “03-20 John Titor and Time Travel,” the files instead carry largely useless names such as “Coast to Coast – Mar 20 2008 – Hour 2.mp3.”
I wrote the below perl script to make the downloaded file names more useful. It connects to the Coast to Coast AM website, looks up the file name, creates a directory with that filename, then moves the downloaded mp3s to that directory. To run the script on your Windows machine, you’ll need to have the Cygwin suite installed, along with the cygwin perl module.
#!/usr/bin/perl
$DIR="/cygdrive/c/Documents and Settings/eddie/My Documents/coast to coast am media center";
opendir (DIR, $DIR) or die "cant open dir $DIR";
while (my $file = readdir (DIR)){
if ($file=~/mp3$/){
#process mp3 file
#print "$file \n";
$file=~/(\w+) ([0-9]+) ([0-9]+)/;
#$file=~/^.+? (\w) ([0-9]+) ([0-9]+) .+$/;
#print "mon $1 \n day $2 \n year $3\n";
$y=$3;
$month=$1;
$d=$2;
print "month is $month\n";
if ($month eq "Feb"){
$m="02";
}elsif($month eq "Mar"){
$m="03";
}elsif($month eq "Apr"){
$m="04";
}elsif($month eq "May"){
$m="05";
}elsif($month eq "Jun"){
$m="06";
}elsif($month eq "Jul"){
$m="07";
}elsif($month eq "Aug"){
$m="08";
}elsif($month eq "Sep"){
$m="09";
}elsif($month eq "Oct"){
$m="10";
}elsif($month eq "Nov"){
$m="11";
}elsif($month eq "Dec"){
$m="12";
}elsif($month eq "Jan"){
$m="01";
}else{
$m="XX";
}
$date_string = $m . "-" . $d;
if ($SHOWS[$y-$m-$d]){
$title=$SHOWS[$y-$m-$d];
}else{
$url = "http://www.coasttocoastam.com/shows/$y/$m/$d.html";
#print "getting URL $url...\n";
#<h2><a name="recap"></a>Title</h2>
system("wget -q -O /tmp/temp.$$ $url");
$file_contents = `cat /tmp/temp.$$`;
$file_contents=~/<h2><a name\s*=\s*"recap"><\/a>(.+?)<\/h2>/;
$title = $1;
$title=~s/\&/and/gis;
$title=~s/\:/-/gis;
$title=~s/\!//gis;
$desc=~s/<.+?>//gis;
print "TITLE: $title\n";
$SHOWS[$y-$m-$d] = $title;
system("rm /tmp/temp.$$");
}
## move the file to the proper directory
$dirname = $date_string . " " . $title;
if (-e "$DIR/$dirname"){
#dir already exists
}else{
$cmd="mkdir \"$DIR/$dirname\"";
system ($cmd);
}
$cmd = "mv \"$DIR/$file\" \"$DIR/$dirname\"";
#print "want to $cmd\n";
system ($cmd);
}
}
close DIR;


October 11th, 2010 at 10:10 pm
Hello Eddie,
I am a huge fan of coast to coast am, and I have streamlink. How long have you been recording the shows? I wish we were able to go back more than 3 years to get recordings, because some of those shows are really fascinating. Do you have any of the shows that are more than 3 years ago?
January 30th, 2012 at 6:05 pm
Does this still work? If so, is there a guide somewhere that walks a user how to set it up with Cygwin?