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