package NameGen; use strict; use Etree::InfoFile; my $debug = 0; sub getName() { my $file = ""; my $title = ""; my $artist = ""; my $album = ""; my $year = ""; my $comment = ""; my $genre = ""; my $tracknum = ""; my $disc = ""; my $filespec = @_[1]; my @parts = split("/",$filespec); $file = @parts[$#parts]; $artist = "Grateful Dead"; $genre = "Jam Band"; if ( $file =~ m/d\d{1,2}t\d{1,3}/ or $file =~ m/d\d{1}-t\d{1,2}/ ) { my $temp = $&; $temp =~ m/d\d{1}/i; $disc = $&; $disc =~ s/d//; $temp =~ m/t\d{1,3}/i; $tracknum = $&; $tracknum =~ s/t//; $tracknum = substr($tracknum, 1, 2), if ( length($tracknum) > 2 ); } elsif ( $file =~ m/\d{1}_\d{2}\./ ) { my $temp = $&; $temp =~ m/\d{1}/; $disc = $&; $temp =~ m/\d{2}/; $tracknum = $&; } my $dir = $filespec; $dir =~ s/$file//; my $info = new Etree::InfoFile (Directory => $dir, Debug => $debug) or die "Unable to create Etree::InfoFile object"; $info->parse or warn "Unable to handle $dir"; $album = $info->album; $year = $info->year; $comment = $info->source; $title = findTrack(\$info, $disc, $tracknum)->{Title}; #my @songs = $info->songs; #$title = @songs[$tracknum-1]->{Title}; return ($title, $artist, $album, $year, $comment, $genre, $tracknum, $disc); } sub findTrack() { my ($info, $disc, $tracknum) = @_; print "DEBUG:: Finding track $tracknum disc $disc...\n", if $debug; my @songs = $$info->songs; foreach my $song (@songs) { print "DEBUG:: Song is\n", if $debug; foreach my $key ( keys %$song ) { print "\t$key => $$song{$key}\n", if $debug; if ( $$song{Disc} == $disc && $$song{Track} == $tracknum ) { print "DEBUG:: Match!! $$song{Title}\n", if $debug; return $song; } } } print "DEBUG:: No match\n", if $debug; } 1;