[BACK]Return to showall.pl CVS log [TXT][DIR] Up to [local] / mp3 / bin

File: [local] / mp3 / bin / showall.pl (download)

Revision 1.2, Sat Aug 12 00:14:53 2006 UTC (17 years, 9 months ago) by andrew
Branch: MAIN
Changes since 1.1: +1 -0 lines

add RCS Ids.  I am not sure why this wasn't committed when those were actually done, but it is now.

#!/usr/bin/perl -w
# $RedRiver$
########################################################################
# showall.pl *** Displays all MP3's in all subdirs
#
# 04-27-00
# Written by andrew fresh <andrew@mad-techies.org>
########################################################################
use strict;
use diagnostics;

#use MP3::Info;
#my qw/ %mp3s %dirs /;

my $basedir = '/home/mp3/Sorted';
my $subdir = $ENV{'QUERY_STRING'};
my $addurl = 'addmp3playlist.pl';

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 "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> <title>' . $subdir . '</title>\n<body>\n\n";


#Print_Nav();

if ($subdir) {
	print "<h3>";
	SplitDir($subdir);
	print " - <font size=\"-2\"><a href=\"" . "showall.pl" . "\?" . EncodeURL("$subdir") . "\">All</a></font>";
	print " - <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL("$subdir") . "\">Add</a></font><br>";
	print "</h3>\n";
	print "<hr>\n";
}

#print "<h3>";
#SplitDir($subdir);
#print "</h3>\n<hr>\n";

#@mp3s = GetMP3Listing("$basedir/$subdir");

GetDirListing("$subdir");
#GetDirListing("");

print "<hr>\n<h3>Total Directories displayed: $countdir<br>\n";
print "Total Songs displayed: $countmp3</h3>\n";

#if (%dirs) {
#	my $count;
#	my @sorted = sort { lc($dirs{$a}) cmp lc($dirs{$b}) } (keys %dirs);
#	foreach my $dir (@sorted) {
#		print "<a href=\"showfiles.pl\?" . EncodeURL("$dir/$dirs{$dir}") . "\">$dirs{$dir}</a><br>";
#		$count++;
#	}
#	print "Total dirs displayed: $count<p>\n";
#}

#if (%mp3s) {
#	print "<ul>\n";
#	my @sorted = sort { lc($mp3s{$a}) cmp lc($mp3s{$b}) } (keys %mp3s);
#	my $count;
#	foreach my $mp3dir (sort keys %mp3s) {
#		foreach $mp3 (@{ $mp3s{$mp3dir} }) {
#			print "\t<li>Song: <a href=\"$addurl\?" . EncodeURL("$mp3dir/$mp3") . "\" target=\"bottom\">$mp3</a></li>\n";
#			$count++;
#		}
#	}
#	print "</ul>\n";
#	print "Total MP3's Displayed: $count<p>\n";
#}

print "</body>\n</head>\n</html>\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 "<ul>\n";
	foreach my $directory (@directories) {
#		$dirs{"$DIR/$directory"} = $directory;
		print "<li><a href=\"showfiles.pl\?" . EncodeURL("$DIR/$directory") . "\">$directory</a>";
		print " - <font size=\"-1\"><a href=\"" . "showall.pl" . "\?" . EncodeURL("$DIR/$directory") . "\">All</a></font>";
		print " - <font size=\"-1\"><a href=\"" . "addall.pl" . "\?" . EncodeURL("$DIR/$directory") . "\">Add</a></font></li>";
		
		$countdir++;
		my @curdir = GetDirListing("$DIR/$directory");
		
		

#		push @dirs, @curdir;
#		print "<a href=\"showfiles.pl\?" . EncodeURL("$DIR/$directory") . "\">$directory</a><br>";
	}
	opendir DIR, "$basedir/$DIR" or bail("Unable to opendir $$!");
		my @temp = grep /\.(mp3|ogg)$/i, readdir(DIR);
#		$mp3s{"$DIR/$directory"} = [ @temp ];
		print "<ul>\n";
		foreach my $mp3 (sort @temp) {
			my $songtitle = $mp3;
			my $directory = $DIR;
			$directory =~ s#.*/(.*)$#$1#;
#			print "dir ", $directory;
#			print "song ", $songtitle;
			if ($songtitle =~ /\Q$directory\E\s/i) {
#				print "MATCHED";
				$songtitle =~ s/^.*- (\d{2}(\.| -) .*)/$1/i;
			}
			print "\t<li>";
			if ($songtitle =~ /\.mp3$/i) {
				print "MP3: ";
			} elsif ($songtitle =~ /\.ogg$/i) {
				print "OGG: ";
			}
			$songtitle =~ s/\.(mp3|ogg)$//i;
			print "<a href=\"$addurl\?" . EncodeURL("$DIR/$mp3") . "\" target=\"bottom\">$songtitle</a></li>\n";
			$countmp3++;
		}
		print "</ul>\n";
	closedir DIR;

	print "</ul>\n";
	
	
	return sort @directories;
}




########################################################################
# *** GetMP3Listing: reads the list of directories
sub GetMP3Listing {
	my $DIR = shift;
	my @songs;
	opendir DIR, $DIR or bail("Unable to opendir $$!");
		@songs = grep /\.(mp3|ogg)$/i, readdir(DIR);
	closedir DIR;
	
	return sort @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 "/<a href=\"showfiles.pl\?" . EncodeURL($url) . "\">$filename[$i]</a>";
		
		}
	} 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: <b>$error</b><br>\n";
	print "In " . __FILE__ . "\n";
	die;
	exit;
}
sub Print_Nav
{
	open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
	while (<FILE>) {
		print;
	}
	close FILE;
}