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

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

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