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