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

Annotation of mp3/bin/showfiles.pl, Revision 1.1

1.1     ! andrew      1: #!/usr/bin/perl -w
        !             2: ########################################################################
        !             3: # GenerateMP3HTML.pl *** Generates the HTML files for the playlist.
        !             4: #
        !             5: # 04-14-00
        !             6: # Written by andrew fresh <andrew@mad-techies.org>
        !             7: ########################################################################
        !             8: use strict;
        !             9: use diagnostics;
        !            10:
        !            11: #use MP3::Info;
        !            12: #my qw/ @mp3s @dirs /;
        !            13:
        !            14: my $basedir = '/home/mp3/Sorted';
        !            15: my $subdir = $ENV{'QUERY_STRING'};
        !            16: my $addurl = 'addmp3playlist.pl';
        !            17:
        !            18: #######################################################################
        !            19: # *** MAIN ***
        !            20: #######################################################################
        !            21:
        !            22: if ($subdir) {
        !            23:        $subdir =~ s/%(..)/pack("c",hex($1))/ge;
        !            24: } else {
        !            25:        $subdir ='';
        !            26: }
        !            27:
        !            28: my @dirs = GetDirListing("$basedir/$subdir");
        !            29: my @mp3s = GetMP3Listing("$basedir/$subdir");
        !            30:
        !            31:
        !            32: print "Content-Type: text/html\n\n";
        !            33: print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> <title>' . $subdir . '</title>\n<body>\n\n";
        !            34:
        !            35: #Print_Nav();
        !            36:
        !            37: if ($subdir) {
        !            38:        print "<h3>";
        !            39:        SplitDir($subdir);
        !            40:        print " - <font size=\"-2\"><a href=\"" . "showall.pl" . "\?" . EncodeURL("$subdir") . "\">All</a></font>";
        !            41:        print " - <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL("$subdir") . "\">Add</a></font><br>";
        !            42:        print "</h3>\n";
        !            43:        print "<hr>\n";
        !            44: }
        !            45:
        !            46: if (@dirs) {
        !            47:        foreach my $dir (@dirs) {
        !            48:                print "<a href=\"" . MyName() . "\?" . EncodeURL("$subdir/$dir") . "\">$dir</a> ";
        !            49:                print "- <font size=\"-2\"><a href=\"" . "showall.pl" . "\?" . EncodeURL("$subdir/$dir") . "\">All</a></font>";
        !            50:                print "- <font size=\"-2\"><a href=\"" . "addall.pl" . "\?" . EncodeURL("$subdir/$dir") . "\">Add</a></font><br>";
        !            51:        }
        !            52: }
        !            53:
        !            54: if (@mp3s) {
        !            55:        print "<ul>\n";
        !            56:        foreach my $mp3 (@mp3s) {
        !            57:                my $songtitle = $mp3;
        !            58:                my $directory = $subdir;
        !            59:                $directory =~ s#.*/(.*)$#$1#;
        !            60: #              print "dir ", $directory;
        !            61: #              print "song ", $songtitle;
        !            62:                if ($songtitle =~ /\Q$directory\E\s/i) {
        !            63:                        $songtitle =~ s/^.*- (\d{2}(\.| -) .*)/$1/i;
        !            64:                }
        !            65:
        !            66:                print "\t<li>";
        !            67:                if ($songtitle =~ /\.mp3$/i) {
        !            68:                        print "MP3: ";
        !            69:                } elsif ($songtitle =~ /\.ogg$/i) {
        !            70:                        print "OGG: ";
        !            71:                }
        !            72:
        !            73:                $songtitle =~ s/\.(mp3|ogg)$//i;
        !            74:                print "<a href=\"$addurl\?" . EncodeURL("$subdir/$mp3") . "\" target=\"bottom\">$songtitle</a></li>\n";
        !            75:        }
        !            76:        print "</ul>\n";
        !            77: }
        !            78:
        !            79: print "</body>\n</head>\n</html>\n";
        !            80:
        !            81:
        !            82:
        !            83:
        !            84: ########################################################################
        !            85: # *** SUBS ***
        !            86: ########################################################################
        !            87:
        !            88: ########################################################################
        !            89: # *** GetDirListing: reads the list of directories
        !            90: sub GetDirListing {
        !            91:        my $DIR = shift;
        !            92:        my @directories;
        !            93:        my @songs;
        !            94:        opendir DIR, $DIR or bail("Unable to opendir $$!");
        !            95:                @directories = grep { !/^\./ && -d "$DIR/$_" } readdir(DIR);
        !            96:        closedir DIR;
        !            97:
        !            98:        return sort @directories;
        !            99: }
        !           100:
        !           101:
        !           102:
        !           103:
        !           104: ########################################################################
        !           105: # *** GetMP3Listing: reads the list of directories
        !           106: sub GetMP3Listing {
        !           107:        my $DIR = shift;
        !           108:        my @songs;
        !           109:        opendir DIR, $DIR or bail("Unable to opendir $$!");
        !           110:                @songs = grep /\.(mp3|ogg)$/i, readdir(DIR);
        !           111:        closedir DIR;
        !           112:
        !           113:        return sort @songs;
        !           114: }
        !           115:
        !           116:
        !           117:
        !           118:
        !           119:
        !           120: ########################################################################
        !           121: # *** EncodeURL: %encodes the parameters of the URL
        !           122: sub EncodeURL {
        !           123:        my $strURL = shift;
        !           124:        $strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
        !           125:        return $strURL;
        !           126: }
        !           127:
        !           128:
        !           129:
        !           130:
        !           131: #########################################################################
        !           132: # My name
        !           133: sub MyName {
        !           134:        my @filename = (split /\//, $0);
        !           135:        pop @filename;
        !           136: }
        !           137:
        !           138:
        !           139:
        !           140: #########################################################################
        !           141: # SplitDir
        !           142: sub SplitDir {
        !           143:        my $dir = shift;
        !           144:        substr($dir, 0, 1) = "";
        !           145:        my @filename = (split /\//, $dir);
        !           146:        my $file;
        !           147:        my $returntext;
        !           148:        my $i;
        !           149:
        !           150:
        !           151:        if ($dir ne " ") {
        !           152:                for ($i=0;$i<=$#filename;$i++) {
        !           153:                        my $j;
        !           154:                        my $url = "";
        !           155:                        for ($j=0;$j<=$i;$j++) {
        !           156:                                $url = "$url/$filename[$j]";
        !           157:                        }
        !           158:                print "/<a href=\"showfiles.pl\?" . EncodeURL($url) . "\">$filename[$i]</a>";
        !           159:
        !           160:                }
        !           161:        } else {
        !           162:                print "/";
        !           163:        }
        !           164: }
        !           165:
        !           166:
        !           167:
        !           168:
        !           169: #######################################################################
        !           170: # Bail: this subrouting dies and displays the error to the browser.
        !           171: # gotten from the example in the O'Reilly
        !           172: # _Learning_Perl_on_Win32_Systems_
        !           173: sub bail {
        !           174:        my $error = shift;
        !           175:        print "\n\nUnexpected Error: <b>$error</b><br>\n";
        !           176:        print "In " . __FILE__ . "\n";
        !           177:        die;
        !           178:        exit;
        !           179: }
        !           180: sub Print_Nav
        !           181: {
        !           182:        open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
        !           183:        while (<FILE>) {
        !           184:                print;
        !           185:        }
        !           186:        close FILE;
        !           187: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>