#! C:/perl/bin/perl use MP3::Tag; use WWW::Lyrics; use CGI::Carp qw{fatalsToBrowser}; @dirs; $s; %numbers; %filenames; $o; $dupes=$files=$folders=$dele=$fld=0; @sizes = ('bytes','KB','MB','GB'); $outto = "D:\\Documents\\Lyrics\\%a\\%c\\%n - %t.txt"; $dirslash = "\\"; #directory separation character $indexfile = "D:\\Documents\\Lyrics\\index.txt"; &recurse("X:\\Music"); my $t = localtime; sub recurse { my ($f); my ($d) = @_; opendir(D,$d); @FILES = readdir(D); closedir(D); foreach $f (@FILES) { if (length($f) > 2) { if ($f !~ /\./) { $dirs[$s++] = "$d/$f"; $folders++} elsif ($f =~ /\.mp3/) { $files++; $numbers{$day} = $numbers{$day} + 1; $filenames{$day} = $filenames{$day} . $f . ';'; my $mp3 = MP3::Tag->new($d . '/' . $f); my ($title, $track, $artist, $album, $comment, $year, $genre) = $mp3->autoinfo(); my $outfile = $outto; $artist =~ s/(\/|\\|\"|\'|\;|\[|\]|\?|\:\*|\>|\<)//g; $album =~ s/(\/|\\|\"|\'|\;|\[|\]|\?|\:\*|\>|\<)//g; $track =~ s/(\/|\\|\"|\'|\;|\[|\]|\?|\:\*|\>|\<)//g; $outfile =~ s/(\%a)/$artist/g; $outfile =~ s/(\%c)/$album/g; $outfile =~ s/(\%n)/$track/g; $outfile =~ s/(\%t)/$title/g; $outfile =~ s/(\%y)/$year/g; $outfile =~ s/(\%g)/$genre/g; if (-e $outfile) { print "Skipping $artist $title (file exists)\n"; } else { &checkdir($outfile); my $lyr = WWW::Lyrics->new( "$artist $title" ); my $count = $lyr->count(); if ($count == 0) { print "No lyrics found for $artist $title. Moving on.\n"; } else { my $found = 0; my $source,$url,$lyrics; while ( ! $found && ($page = $lyr->fetch())) { if ( $page->code() != 200 ) { print "Fetch failed (HTTP status code " . $page->code() . ") for $artist $title\n"; next; } if ($page) { $found = 1; $source = $page->source(); $url = $page->url(); $lyrics = $page->lyrics(); } # $page = $lyr->fetch(); } if ($found) { my $t = localtime; open(FIL,">>$indexfile"); print FIL "[$t] [$track] [$title] [$artist] [$album] [$year] [$count] [$genre] [$source] [$url]\n"; close(FIL); open(FIL,">$outfile"); print FIL $lyrics; close(FIL); print "Lyrics found for $artist $title. Saved.\n"; } else { print "No good sources found for $artist $title. Moving on."; } } } } } } while ($s != $o) { &recurse ($dirs[$o++]); } } print "Done!\n"; ; exit; sub checkdir { my ($file) = @_[0]; my $f; my @parts = split(/\\/,$file); for ($i=0;$i<(scalar @parts) - 1;$i++) { $f = $f . $parts[$i] . $dirslash; if (! -d $f) { print "$f does not exist. Creating.\n"; mkdir $f; } else { } } }