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