Automatically renaming files downloaded with Coast to Coast AM Media Center
April 7, 2008 on 9:07 pm | In perl, productivity |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 $3n";
$y=$3;
$month=$1;
$d=$2;
print "month is $monthn";
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 names*=s*"recap"></a>(.+?)</h2>/;
$title = $1;
$title=~s/&/and/gis;
$title=~s/:/-/gis;
$title=~s/!//gis;
$desc=~s/<.+?>//gis;
print "TITLE: $titlen";
$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 $cmdn";
system ($cmd);
}
}
close DIR;
Like this story? Share with others on:
 
del.icio.us
digg
reddit
furl
No Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
35 queries. 0.186 seconds.
