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

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

1.1       andrew      1: #!/usr/bin/perl -w
1.3     ! andrew      2: # $RedRiver: createmp3list.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/ $basedir %mp3s %dirs $countdir $countmp3 $fulllist /;
                     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) = 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 ='';
1.1       andrew     28: }
                     29:
1.3     ! andrew     30: if ($prefix) {
        !            31:         $prefix =~ s/%(..)/pack("c",hex($1))/ge;
        !            32: } else {
        !            33:         $prefix ='';
        !            34: }
        !            35:
        !            36: my $playlist = '/var/www/mp3/playlist/' . $prefix . 'playlist.lst';
        !            37: my $fulllist = '/var/www/mp3/playlist/' . $prefix . 'fulllist.lst';
        !            38: my $newlist = '/var/www/mp3/playlist/' . $prefix . 'newlist.lst';
1.1       andrew     39: #$basedir = $basedir . "/" . $subdir;
                     40:
                     41: print "Content-Type: text/html\n\n";
                     42: print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> <title>Generating new full list of mp3's</title>\n<body>\n\n";
                     43:
                     44: #Print_Nav();
                     45:
                     46:
                     47:
                     48: print "<h3>";
                     49: SplitDir($subdir);
                     50: print "</h3>\n<hr>\n";
                     51:
                     52: #@mp3s = GetMP3Listing("$basedir/$subdir");
                     53: Remove_Old_Lists();
                     54: GetDirListing("$subdir");
                     55: Move_Old_List($fulllist);
                     56: Get_New_MP3s($fulllist);
                     57: #GetDirListing("");
                     58:
                     59: print "<hr>\n<h3>Total Directories displayed: $countdir<br>\n";
                     60: print "Total MP3's Added: $countmp3</h3>\n";
                     61:
                     62: #if (%dirs) {
                     63: #      my $count;
                     64: #      my @sorted = sort { lc($dirs{$a}) cmp lc($dirs{$b}) } (keys %dirs);
                     65: #      foreach my $dir (@sorted) {
1.3     ! andrew     66: #              print "dir: <a href=\"showfiles.pl\?" . EncodeURL($prefix) . '&' . EncodeURL("$dir/$dirs{$dir}") . "\">$dirs{$dir}</a><br>";
1.1       andrew     67: #              $count++;
                     68: #      }
                     69: #      print "Total dirs displayed: $count<p>\n";
                     70: #}
                     71:
                     72: #if (%mp3s) {
                     73: #      print "<ul>\n";
                     74: #      my @sorted = sort { lc($mp3s{$a}) cmp lc($mp3s{$b}) } (keys %mp3s);
                     75: #      my $count;
                     76: #      foreach my $mp3dir (sort keys %mp3s) {
                     77: #              foreach $mp3 (@{ $mp3s{$mp3dir} }) {
1.3     ! andrew     78: #                      print "\t<li>Song: <a href=\"$addurl\?" . EncodeURL($prefix) . '&' . EncodeURL("$mp3dir/$mp3") . "\" target=\"bottom\">$mp3</a></li>\n";
1.1       andrew     79: #                      $count++;
                     80: #              }
                     81: #      }
                     82: #      print "</ul>\n";
                     83: #      print "Total MP3's Displayed: $count<p>\n";
                     84: #}
                     85:
                     86: print "</body>\n</head>\n</html>\n";
                     87:
                     88:
                     89:
                     90:
                     91: ########################################################################
                     92: # *** SUBS ***
                     93: ########################################################################
                     94:
                     95: ########################################################################
                     96: # *** GetDirListing: reads the list of directories
                     97: sub GetDirListing {
                     98:        my $DIR = shift;
                     99:        $DIR ||= '';
                    100:        my @directories;
                    101:        my @songs;
                    102:
                    103:
                    104:        opendir DIR, "$basedir/$DIR" or bail("Unable to opendir $DIR: $!");
                    105:                @directories = sort grep { !/^\./ && -d "$basedir$DIR/$_" } readdir(DIR);
                    106:        closedir DIR;
                    107:
                    108:        print "<ul>\n";
                    109:        foreach my $directory (@directories) {
                    110: #              $dirs{"$DIR/$directory"} = $directory;
1.3     ! andrew    111:                print "<li>dir: <a href=\"showfiles.pl\?" . EncodeURL($prefix) . '&' . EncodeURL("$DIR/$directory") . "\">$directory</a>";
        !           112:                print " - <font size=\"-1\"><a href=\"" . "showall.pl" . "\?" . EncodeURL($prefix) . '&' . EncodeURL("$DIR/$directory") . "\">All</a></font></li>";
1.1       andrew    113:
                    114:                $countdir++;
                    115:                my @curdir = GetDirListing("$DIR/$directory");
                    116:
                    117:
                    118:
                    119: #              push @dirs, @curdir;
1.3     ! andrew    120: #              print "<a href=\"showfiles.pl\?" . EncodeURL($prefix) . '&' . EncodeURL("$DIR/$directory") . "\">$directory</a><br>";
1.1       andrew    121:        }
                    122:        opendir DIR, "$basedir/$DIR" or bail("Unable to opendir $DIR: $!");
                    123:                my @temp = grep /\.mp3/i, readdir(DIR);
                    124: #              $mp3s{"$DIR/$directory"} = [ @temp ];
                    125:                print "<ul>\n";
                    126:                foreach $mp3 (@temp) {
1.3     ! andrew    127: #                      print "\t<li>Song: <a href=\"$addurl\?" . EncodeURL($prefix) . '&' . EncodeURL("$DIR/$mp3") . "\" target=\"bottom\">$mp3</a></li><br>\n";
1.1       andrew    128:                        print "\t<li>";
                    129:                        AddSong("$DIR/$mp3", "$fulllist.new");
                    130:                        print "</li>\n";
                    131:                        $countmp3++;
                    132:                }
                    133:                print "</ul>\n";
                    134:        closedir DIR;
                    135:
                    136:        print "</ul>\n";
                    137:
                    138:
                    139:        return sort @directories;
                    140: }
                    141:
                    142:
                    143:
                    144:
                    145: ########################################################################
                    146: # *** GetMP3Listing: reads the list of directories
                    147: sub GetMP3Listing {
                    148:        my $DIR = shift;
                    149:        my @songs;
                    150:        opendir DIR, $DIR or bail("Unable to opendir $DIR: $!");
                    151:                @songs = grep /\.(mp3|ogg)/i, readdir(DIR);
                    152:        closedir DIR;
                    153:
                    154:        return sort @songs;
                    155: }
                    156:
                    157:
                    158:
                    159:
                    160:
                    161: ########################################################################
                    162: # *** EncodeURL: %encodes the parameters of the URL
                    163: sub EncodeURL {
                    164:        my $strURL = shift;
                    165:        $strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
                    166:        return $strURL;
                    167: }
                    168:
                    169:
                    170:
                    171:
                    172: #########################################################################
                    173: # My name
                    174: sub MyName {
                    175:        my @filename = (split /\//, $0);
                    176:        pop @filename;
                    177: }
                    178:
                    179:
                    180:
                    181: #########################################################################
                    182: # SplitDir
                    183: sub SplitDir {
                    184:        my $dir = shift;
                    185:        substr($dir, 0, 1) = "";
                    186:        my @filename = (split /\//, $dir);
                    187:        my $file;
                    188:        my $returntext;
                    189:        my $i;
                    190:
                    191:
                    192:        if ($dir ne " ") {
                    193:                for ($i=0;$i<=$#filename;$i++) {
                    194:                        my $j;
                    195:                        my $url = "";
                    196:                        for ($j=0;$j<=$i;$j++) {
                    197:                                $url = "$url/$filename[$j]";
                    198:                        }
1.3     ! andrew    199:                print "/<a href=\"showfiles.pl\?" . EncodeURL($prefix) . '&' . EncodeURL($url) . "\">$filename[$i]</a>";
1.1       andrew    200:
                    201:                }
                    202:        } else {
                    203:                print "/";
                    204:        }
                    205: }
                    206:
                    207:
                    208:
                    209:
                    210: #######################################################################
                    211: # Bail: this subrouting dies and displays the error to the browser.
                    212: # gotten from the example in the O'Reilly
                    213: # _Learning_Perl_on_Win32_Systems_
                    214: sub bail {
                    215:        my $error = shift;
                    216:        print "\n\nUnexpected Error: <b>$error</b><br>\n";
                    217:        print "In " . __FILE__ . "\n";
                    218:        die;
                    219:        exit;
                    220: }
                    221:
                    222:
                    223: sub AddSong
                    224: {
                    225:        my $filename = shift;
                    226:        my $list = shift;
                    227:
                    228:        if ($filename) {
                    229: #              $filename =~ s/%(..)/pack("c",hex($1))/ge;
                    230:                chomp $filename;
                    231:                print "Added $filename<br>\n";
                    232:                open  PLAYLIST, ">>$list" or bail("unable to open PLAYLIST: $!");
                    233:                print PLAYLIST "$filename\n";
                    234:                close PLAYLIST or bail("unable to close PLAYLIST: $!");
                    235:        } else {
                    236:                print "<h1>You need to pass a song</h1>";
                    237:        }
                    238: }
                    239:
                    240: sub Move_Old_List
                    241: {
                    242:        my $file = shift;
                    243:
                    244:        if (-e $file) {
                    245:                if (-e "$file.old") {
                    246:                        unlink "$file.old" or die "Couldn't delete old list $file.old: $!";
                    247:                }
                    248:                rename($file, "$file.old") or die "Couldn't rename file $file: $!";
                    249:        }
                    250: }
                    251:
                    252: sub Get_New_MP3s
                    253: {
                    254:        my $file = shift;
                    255:
                    256:        if (-e "$file.new") {
                    257:                if (-e $file) {
                    258:                        unlink($file) or die "couldn't unlink $newlist: $!";
                    259:                }
                    260:                rename("$file.new", $file) or die "Couldn't rename $file: $!";
                    261:
                    262:                if (-e "$file.old") {
                    263:                        my %seen;
                    264:                        open FILE, "$file.old" or die "Couldn't open file $file.old: $!";
                    265:                        @seen{ <FILE> } = ();
                    266:                        close FILE;
                    267:
                    268:                        open FILE, $file or die "couldn't open FILE $file: $!";
                    269:                        while (<FILE>) {
                    270:                                unless (exists $seen{$_}) {
                    271:                                        AddSong($_, $playlist);
                    272:                                        print "to the playlist<br>\n";
                    273:                                        AddSong($_, $newlist);
                    274:                                        print "to the list of new files<br>\n";
                    275:                                }
                    276:                        }
                    277:                        close FILE;
                    278:                }
                    279:        }
                    280: }
                    281:
                    282: sub Remove_Old_Lists
                    283: {
                    284:        if (-e $newlist) {
                    285:                open FILE, ">$newlist" or die "couldn't unlink $newlist: $!";
                    286:                close FILE;
                    287:        }
                    288:        if (-e "$fullist.new") {
                    289:                unlink("$fullist.new") or die "couldn't unlink $fullist.new: $!";
                    290:        }
                    291: }
                    292:
                    293: sub Print_Nav
                    294: {
                    295:        open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
                    296:        while (<FILE>) {
                    297:                print;
                    298:        }
                    299:        close FILE;
                    300: }

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