File: [local] / mp3 / bin / showfiles.pl (download)
Revision 1.3, Thu Feb 8 20:04:03 2007 UTC (17 years, 5 months ago) by andrew
Branch: MAIN
Changes since 1.2: +21 -9 lines
First shot at making multiple jukeboxes from the same files.
|
#!/usr/bin/perl -w
# $RedRiver: showfiles.pl,v 1.2 2006/08/12 00:14:53 andrew Exp $
########################################################################
# GenerateMP3HTML.pl *** Generates the HTML files for the playlist.
#
# 04-14-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 $addurl = 'addmp3playlist.pl';
#######################################################################
# *** MAIN ***
#######################################################################
my ($prefix, $subdir) = split /\&/, $ENV{'QUERY_STRING'};
if ($subdir) {
$subdir =~ s/%(..)/pack("c",hex($1))/ge;
} else {
$subdir ='';
}
if ($prefix) {
$prefix =~ s/%(..)/pack("c",hex($1))/ge;
} else {
$prefix ='';
}
my @dirs = GetDirListing("$basedir/$subdir");
my @mp3s = GetMP3Listing("$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($prefix) . '&' . EncodeURL("$subdir") . "\">Show</a></font>";
print " - <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir") . "\">Play</a></font>";
print " - <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir") . '&' . EncodeURL('fulllist.lst') . "\">Approve</a></font>" if $prefix;
print "<br>\n";
print "</h3>\n";
print "<hr>\n";
}
if (@dirs) {
foreach my $dir (@dirs) {
print "<a href=\"" . MyName() . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir/$dir") . "\">$dir</a> ";
print "- <font size=\"-2\"><a href=\"" . "showall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir/$dir") . "\">Show</a></font>";
print "- <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir/$dir") . "\">Play</a></font>";
print "- <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir/$dir") . '&' . EncodeURL('fulllist.lst') . "\">Approve</a></font>" if $prefix;
print "<br>\n";
}
}
if (@mp3s) {
print "<ul>\n";
foreach my $mp3 (@mp3s) {
my $songtitle = $mp3;
my $directory = $subdir;
$directory =~ s#.*/(.*)$#$1#;
# print "dir ", $directory;
# print "song ", $songtitle;
if ($songtitle =~ /\Q$directory\E\s/i) {
$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($prefix) . '&' . EncodeURL("$subdir/$mp3") . "\" target=\"bottom\">$songtitle</a>";
print " - <a href=\"$addurl\?" . EncodeURL($prefix) . '&' . EncodeURL("$subdir/$mp3") . '&' . EncodeURL('fulllist.lst') . "\" target=\"bottom\">Approve</a>" if $prefix;
print "</li>\n";
}
print "</ul>\n";
}
print "</body>\n</head>\n</html>\n";
########################################################################
# *** SUBS ***
########################################################################
########################################################################
# *** GetDirListing: reads the list of directories
sub GetDirListing {
my $DIR = shift;
my @directories;
my @songs;
opendir DIR, $DIR or bail("Unable to opendir $$!");
@directories = grep { !/^\./ && -d "$DIR/$_" } readdir(DIR);
closedir DIR;
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($prefix) . '&' . 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;
}