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

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

1.1     ! andrew      1: #!/usr/bin/perl -w
        !             2: ########################################################################
        !             3: # showall.pl *** Displays all MP3's in all subdirs
        !             4: #
        !             5: # 04-27-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 $playlist = '/var/www/mp3/playlist/playlist.lst';
        !            17:
        !            18: my $countdir = 0;
        !            19: my $countmp3 = 0;
        !            20: #######################################################################
        !            21: # *** MAIN ***
        !            22: #######################################################################
        !            23:
        !            24: if ($subdir) {
        !            25:        $subdir =~ s/%(..)/pack("c",hex($1))/ge;
        !            26: } else {
        !            27:        $subdir ='';
        !            28: }
        !            29:
        !            30: #$basedir = $basedir . "/" . $subdir;
        !            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: print "<h3>";
        !            38: SplitDir($subdir);
        !            39: print "</h3>\n<hr>\n";
        !            40:
        !            41: #@mp3s = GetMP3Listing("$basedir/$subdir");
        !            42:
        !            43: GetDirListing("$subdir");
        !            44: #GetDirListing("");
        !            45:
        !            46: print "<hr>\n<h3>Total Directories displayed: $countdir<br>\n";
        !            47: print "Total MP3's Added: $countmp3</h3>\n";
        !            48:
        !            49: #if (%dirs) {
        !            50: #      my $count;
        !            51: #      my @sorted = sort { lc($dirs{$a}) cmp lc($dirs{$b}) } (keys %dirs);
        !            52: #      foreach my $dir (@sorted) {
        !            53: #              print "<a href=\"showfiles.pl\?" . EncodeURL("$dir/$dirs{$dir}") . "\">$dirs{$dir}</a><br>";
        !            54: #              $count++;
        !            55: #      }
        !            56: #      print "Total dirs displayed: $count<p>\n";
        !            57: #}
        !            58:
        !            59: #if (%mp3s) {
        !            60: #      print "<ul>\n";
        !            61: #      my @sorted = sort { lc($mp3s{$a}) cmp lc($mp3s{$b}) } (keys %mp3s);
        !            62: #      my $count;
        !            63: #      foreach my $mp3dir (sort keys %mp3s) {
        !            64: #              foreach $mp3 (@{ $mp3s{$mp3dir} }) {
        !            65: #                      print "\t<li>Song: <a href=\"$addurl\?" . EncodeURL("$mp3dir/$mp3") . "\" target=\"bottom\">$mp3</a></li>\n";
        !            66: #                      $count++;
        !            67: #              }
        !            68: #      }
        !            69: #      print "</ul>\n";
        !            70: #      print "Total MP3's Displayed: $count<p>\n";
        !            71: #}
        !            72:
        !            73: print "</body>\n</head>\n</html>\n";
        !            74:
        !            75:
        !            76:
        !            77:
        !            78: ########################################################################
        !            79: # *** SUBS ***
        !            80: ########################################################################
        !            81:
        !            82: ########################################################################
        !            83: # *** GetDirListing: reads the list of directories
        !            84: sub GetDirListing {
        !            85:        my $DIR = shift;
        !            86:        $DIR ||= '';
        !            87:        my @directories;
        !            88:        my @songs;
        !            89:
        !            90:
        !            91:        opendir DIR, "$basedir/$DIR" or bail("Unable to opendir $$!");
        !            92:                @directories = grep { !/^\./ && -d "$basedir$DIR/$_" } readdir(DIR);
        !            93:        closedir DIR;
        !            94:
        !            95:        print "<ul>\n";
        !            96:        foreach my $directory (@directories) {
        !            97: #              $dirs{"$DIR/$directory"} = $directory;
        !            98:                print "<li><a href=\"showfiles.pl\?" . EncodeURL("$DIR/$directory") . "\">$directory</a>";
        !            99:                print " - <font size=\"-1\"><a href=\"" . "showall.pl" . "\?" . EncodeURL("$DIR/$directory") . "\">All</a></font></li>";
        !           100:
        !           101:                $countdir++;
        !           102:                my @curdir = GetDirListing("$DIR/$directory");
        !           103:
        !           104:
        !           105:
        !           106: #              push @dirs, @curdir;
        !           107: #              print "<a href=\"showfiles.pl\?" . EncodeURL("$DIR/$directory") . "\">$directory</a><br>";
        !           108:        }
        !           109:        opendir DIR, "$basedir/$DIR" or bail("Unable to opendir $$!");
        !           110:                my @temp = grep /\.(mp3|ogg)$/i, readdir(DIR);
        !           111: #              $mp3s{"$DIR/$directory"} = [ @temp ];
        !           112:                print "<ul>\n";
        !           113:                foreach my $mp3 (@temp) {
        !           114: #                      print "\t<li>Song: <a href=\"$addurl\?" . EncodeURL("$DIR/$mp3") . "\" target=\"bottom\">$mp3</a></li><br>\n";
        !           115:                        print "\t<li>";
        !           116:                        AddSong("$DIR/$mp3");
        !           117:                        print "</li>\n";
        !           118:                        $countmp3++;
        !           119:                }
        !           120:                print "</ul>\n";
        !           121:        closedir DIR;
        !           122:
        !           123:        print "</ul>\n";
        !           124:
        !           125:
        !           126:        return @directories;
        !           127: }
        !           128:
        !           129:
        !           130:
        !           131:
        !           132: ########################################################################
        !           133: # *** GetMP3Listing: reads the list of directories
        !           134: sub GetMP3Listing {
        !           135:        my $DIR = shift;
        !           136:        my @songs;
        !           137:        opendir DIR, $DIR or bail("Unable to opendir $$!");
        !           138:                @songs = grep /\.(mp3|ogg)$/i, readdir(DIR);
        !           139:        closedir DIR;
        !           140:
        !           141:        return @songs;
        !           142: }
        !           143:
        !           144:
        !           145:
        !           146:
        !           147:
        !           148: ########################################################################
        !           149: # *** EncodeURL: %encodes the parameters of the URL
        !           150: sub EncodeURL {
        !           151:        my $strURL = shift;
        !           152:        $strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
        !           153:        return $strURL;
        !           154: }
        !           155:
        !           156:
        !           157:
        !           158:
        !           159: #########################################################################
        !           160: # My name
        !           161: sub MyName {
        !           162:        my @filename = (split /\//, $0);
        !           163:        pop @filename;
        !           164: }
        !           165:
        !           166:
        !           167:
        !           168: #########################################################################
        !           169: # SplitDir
        !           170: sub SplitDir {
        !           171:        my $dir = shift;
        !           172:        substr($dir, 0, 1) = "";
        !           173:        my @filename = (split /\//, $dir);
        !           174:        my $file;
        !           175:        my $returntext;
        !           176:        my $i;
        !           177:
        !           178:
        !           179:        if ($dir ne " ") {
        !           180:                for ($i=0;$i<=$#filename;$i++) {
        !           181:                        my $j;
        !           182:                        my $url = "";
        !           183:                        for ($j=0;$j<=$i;$j++) {
        !           184:                                $url = "$url/$filename[$j]";
        !           185:                        }
        !           186:                print "/<a href=\"showfiles.pl\?" . EncodeURL($url) . "\">$filename[$i]</a>";
        !           187:
        !           188:                }
        !           189:        } else {
        !           190:                print "/";
        !           191:        }
        !           192: }
        !           193:
        !           194:
        !           195:
        !           196:
        !           197: #######################################################################
        !           198: # Bail: this subrouting dies and displays the error to the browser.
        !           199: # gotten from the example in the O'Reilly
        !           200: # _Learning_Perl_on_Win32_Systems_
        !           201: sub bail {
        !           202:        my $error = shift;
        !           203:        print "\n\nUnexpected Error: <b>$error</b><br>\n";
        !           204:        print "In " . __FILE__ . "\n";
        !           205:        die;
        !           206:        exit;
        !           207: }
        !           208:
        !           209:
        !           210: sub AddSong
        !           211: {
        !           212:        my $filename = shift;
        !           213:        $filename =~ s/%(..)/pack("c",hex($1))/ge;
        !           214:
        !           215:        if ($filename) {
        !           216:                print "Added $filename<br>\n";
        !           217:                open  PLAYLIST, ">>$playlist" or bail("unable to open PLAYLIST: $!");
        !           218:                print PLAYLIST "$filename\n";
        !           219:                close PLAYLIST or bail("unable to close PLAYLIST: $!");
        !           220:        } else {
        !           221:                print "<h1>You need to pass a song</h1>";
        !           222:        }
        !           223: }
        !           224:
        !           225: sub Print_Nav
        !           226: {
        !           227:        open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
        !           228:        while (<FILE>) {
        !           229:                print;
        !           230:        }
        !           231:        close FILE;
        !           232: }

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