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

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

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

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