#!/usr/bin/perl # # DECrename.pl v1.2 # # Author: Ron Luman II # Date: 10/14/2002 # Purpose: This script is intended to read the poorly named files # from the DEC's ripped folder, rename them according to # Artist/Album/Title into the slurped directory. It then # updates the DEC's db accordingly. # Caveat: Use at your own risk! Since we're mucking with the DEC # database directly, there is a small chance that this # script could corrupt things. Always have a backup # available. # Revision History: See end of file BEGIN { @INC = (@INC, "/opt/odin/perl5/lib/site_perl/i386-linux", "/opt/odin/perl5/lib/site_perl"); } use File::Copy; use File::Basename; use Getopt::Std; use MP3::Info; use DBI; my $dbdirectory = "/content/db"; my $basepathname = "/content/music/slurped"; my $targetpathname = "/content/music/ripped"; my $dbtargetpathname = "tracks/ripped"; my $file; my $dbh; my $sth; # First, parse arguments getopts("tv") or exit usage(); unless ($opt_t) { # initialize database $dbh = DBI->connect("DBI:XBase:$dbdirectory"); if (not defined $dbh) { die "FAIL:$DBI::errstr\n"; } # initialize query $sth = $dbh->prepare("update LibraryTrack set FILE=? where FILE=?"); } use_winamp_genres(); foreach $file (<$targetpathname/*>) { # first, read mp3 info from file my $info = get_mp3tag($file, 1); # if we have ID3 tags, then move file into slurped, updating db if($info){ my $Artist = $info->{ARTIST}; my $Album = $info->{ALBUM}; my $Title = $info->{TITLE}; #BEGIN TEMPHACK # help out those entries that were corrupted by a previous run my $oldnewfile = "$basepathname/$Artist/$Album/${Title}.mp3"; #END TEMPHACK # Replace '/'s with '-'s $Artist =~ s#/#-#g; $Album =~ s#/#-#g; $Title =~ s#/#-#g; if ($opt_t) { print "Artist: <$Artist> \tAlbum: <$Album}> \tTitle: <$Title>\n"; } else { # update file mkdir "$basepathname/$Artist/" unless -e "$basepathname/$Artist/"; mkdir "$basepathname/$Artist/$Album/" unless -e "$basepathname/$Artist/$Album/"; my $newfile = "$basepathname/$Artist/$Album/$Title"; # if file already exists, then increment the filename while (-e "${newfile}.mp3") { $newfile = "${newfile}x"; } $newfile = "${newfile}.mp3"; if ($opt_v) { print "$newfile\n"; } move($file, $newfile); # update db entry my $qfname = $dbtargetpathname . "/" . basename($file, ""); #BEGIN TEMPHACK # just in case real entry was munged by previous bad run $sth->execute($newfile, $oldnewfile); #END TEMPHACK # now the real (expected) update $sth->execute($newfile, $qfname); if (defined $sth->err) { die $sth->errstr(); } } } else { print "No ID3 tags for file: $file\n"; } } unless ($opt_t) { $sth->finish(); $dbh->disconnect(); } sub usage { print "usage: $0 [-v] [-t]\n"; print " -v = verbose; print file being moved (to)\n"; print " -t = test; print the ID3 tags of the files in the ripped directory\n"; return 1; } # Revision History # # version 1.2 # - added code to deal with '/' in Album/Artist/Title fields # - added a temporary hack to fix db entries corrupted by '/' entries # # version 1.1 # - added '-t' (test) option for displaying ID3 tags of files to be converted # - added '-v' (verbose) option for displaying the file currently being moved # - added an error message for files without ID3 tags # - added logic to attempt to handle multiple songs with the same tag # - added database disconnect statement # # version 1.0 # - initial version