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