#!/usr/bin/perl -w ######################################################################## # showall.pl *** Displays all MP3's in all subdirs # # 04-27-00 # Written by andrew fresh ######################################################################## use strict; use diagnostics; #use MP3::Info; #my qw/ $basedir %mp3s %dirs $countdir $countmp3 $fulllist /; my $basedir = '/home/mp3/Sorted'; my $subdir = $ENV{'QUERY_STRING'}; my $playlist = '/var/www/mp3/playlist/playlist.lst'; my $fulllist = '/var/www/mp3/playlist/fulllist.lst'; my $newlist = '/var/www/mp3/playlist/newlist.lst'; my $countdir = 0; my $countmp3 = 0; ####################################################################### # *** MAIN *** ####################################################################### if ($subdir) { $subdir =~ s/%(..)/pack("c",hex($1))/ge; } else { $subdir =''; } #$basedir = $basedir . "/" . $subdir; print "Content-Type: text/html\n\n"; print "\n\n\t Generating new full list of mp3's\n\n\n"; #if (defined $ENV{'REMOTE_ADDR'}) { # Print_Nav(); #} print "

"; SplitDir($subdir); print "

\n
\n"; #@mp3s = GetMP3Listing("$basedir/$subdir"); Remove_Old_Lists(); GetDirListing("$subdir"); Move_Old_List($fulllist); Get_New_MP3s($fulllist); #GetDirListing(""); print "
\n

Total Directories displayed: $countdir
\n"; print "Total MP3's Added: $countmp3

\n"; #if (%dirs) { # my $count; # my @sorted = sort { lc($dirs{$a}) cmp lc($dirs{$b}) } (keys %dirs); # foreach my $dir (@sorted) { # print "$dirs{$dir}
"; # $count++; # } # print "Total dirs displayed: $count

\n"; #} #if (%mp3s) { # print "

\n"; # print "Total MP3's Displayed: $count

\n"; #} print "\n\n\n"; ######################################################################## # *** SUBS *** ######################################################################## ######################################################################## # *** GetDirListing: reads the list of directories sub GetDirListing { my $DIR = shift; $DIR ||= ''; my @directories; my @songs; opendir DIR, "$basedir/$DIR" or bail("unable to opendir $$!"); @directories = sort grep { !/^\./ && -d "$basedir$DIR/$_" } readdir(DIR); closedir DIR; print "

\n"; return @directories; } ######################################################################## # *** GetMP3Listing: reads the list of directories sub GetMP3Listing { my $DIR = shift; my @songs; opendir DIR, $DIR or bail("Unable to opendir $$!"); @songs = sort grep /\.(mp3|ogg)$/i, readdir(DIR); closedir DIR; return @songs; } ######################################################################## # *** EncodeURL: %encodes the parameters of the URL sub EncodeURL { my $strURL = shift; $strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg; return $strURL; } ######################################################################### # My name sub MyName { my @filename = (split /\//, $0); pop @filename; } ######################################################################### # SplitDir sub SplitDir { my $dir = shift; substr($dir, 0, 1) = ""; my @filename = (split /\//, $dir); my $file; my $returntext; my $i; if ($dir ne " ") { for ($i=0;$i<=$#filename;$i++) { my $j; my $url = ""; for ($j=0;$j<=$i;$j++) { $url = "$url/$filename[$j]"; } print "/$filename[$i]"; } } else { print "/"; } } ####################################################################### # Bail: this subrouting dies and displays the error to the browser. # gotten from the example in the O'Reilly # _Learning_Perl_on_Win32_Systems_ sub bail { my $error = shift; print "\n\nUnexpected Error: $error
\n"; print "In " . __FILE__ . "\n"; die; exit; } sub AddSong { my $filename = shift; my $list = shift; if ($filename) { # $filename =~ s/%(..)/pack("c",hex($1))/ge; chomp $filename; print "Added $filename
\n"; open PLAYLIST, ">>$list" or bail("unable to open PLAYLIST: $!"); print PLAYLIST "$filename\n"; close PLAYLIST or bail("unable to close PLAYLIST: $!"); } else { print "

You need to pass a song

"; } } sub Move_Old_List { my $file = shift; if (-e $file) { if (-e "$file.old") { unlink "$file.old" or die "Couldn't delete old list $file.old: $!"; } rename($file, "$file.old") or die "Couldn't rename file $file: $!"; } } sub Get_New_MP3s { my $file = shift; if (-e "$file.new") { if (-e $file) { unlink($file) or die "couldn't unlink $newlist: $!"; } rename("$file.new", $file) or die "Couldn't rename $file: $!"; if (-e "$file.old") { my %seen; open FILE, "$file.old" or die "Couldn't open file $file.old: $!"; @seen{ } = (); close FILE; open FILE, $file or die "couldn't open FILE $file: $!"; while () { unless (exists $seen{$_}) { AddSong($_, $playlist); print "to the playlist
\n"; AddSong($_, $newlist); print "to the list of new files
\n"; } } close FILE; } } } sub Remove_Old_Lists { if (-e $newlist) { open FILE, ">$newlist" or die "couldn't unlink $newlist: $!"; close FILE; } if (-e "$fulllist.new") { unlink("$fulllist.new") or die "couldn't unlink $fulllist.new: $!"; } } sub Print_Nav { open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!"; while () { print; } close FILE; }