Annotation of mp3/bin/createmp3list.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: #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 "dir: <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 $DIR: $!");
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>dir: <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 = grep /\.mp3/i, readdir(DIR);
118: # $mp3s{"$DIR/$directory"} = [ @temp ];
119: print "<ul>\n";
120: foreach $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 sort @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 $DIR: $!");
145: @songs = grep /\.(mp3|ogg)/i, readdir(DIR);
146: closedir DIR;
147:
148: return sort @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 "$fullist.new") {
283: unlink("$fullist.new") or die "couldn't unlink $fullist.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>