[BACK]Return to removemp3playlist.pl CVS log [TXT][DIR] Up to [local] / mp3 / bin

File: [local] / mp3 / bin / removemp3playlist.pl (download)

Revision 1.1, Thu Mar 2 23:20:47 2006 UTC (18 years, 2 months ago) by andrew
Branch: MAIN

Initial revision

#!/usr/bin/perl -w
########################################################################
# RemoveMP3Playlist.pl *** Removed MP3's from the playlist
#
# 04-19-00
# Written by andrew fresh <andrew@mad-techies.org>
########################################################################
use strict;
use diagnostics;


#my qw/ $removed $filename $list /;
########################################################################
# *** Variables ***
my $filename = $ENV{'QUERY_STRING'};
my $list = '/var/www/mp3/playlist/playlist.lst';



########################################################################
# *** MAIN ***
########################################################################

my @playlist = get_playlist($list);
$filename =~ s/%(..)/pack("c",hex($1))/ge;
my $removed = 0;
my $oldfilename;

print "Content-Type: text/html\n\n";
print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> <title>Added $filename</title>\n<body>\n\n";

# Print_Nav();
my $i;
for ($i=0;$i<=$#playlist;$i++) {
	if ($filename eq $playlist[$i]) {
		$oldfilename = splice(@playlist,$i,1);
		$removed++;
	}
}

save_playlist($list,@playlist) || bail("Unable to save playlist!: $!");

if ($removed == 1) {
	print "Removed $removed instance of $filename";
} elsif ($removed > 1) {
	print "Removed $removed instances of $filename";
} else {
	print "Not Removed!<br>\n";
	print "$filename not found"
}

print "</body>\n</head>\n</html>\n";










########################################################################
# *** SUBS ***
########################################################################




#######################################################################
# writes back the new playlist
sub save_playlist {
	my $FILE = shift;
	my @lines = @_;
	open (FILE, ">$FILE") || bail ("Couldn\'t open $FILE: $!");
	foreach (@lines) {
		print FILE "$_\n";
	}
	close (FILE) || bail ("Couldn't close $FILE: $!");
	return 1;
}




#######################################################################
# read in the Playlist
sub get_playlist {
	my $FILE = shift;
	my @lines;
	while (@lines == 0) {
		open (FILE, $FILE) || bail ("Couldn\'t open $FILE: $!");
		chomp (@lines = <FILE>);
		close (FILE) || bail ("Couldn't close $FILE: $!");
	}
	return @lines;
}





########################################################################
# Bail: this subrouting dies and displays the error to the browser.
# gotten from the example in the O'Reilly 
# _Learning_Perl_on_Win32_Systems_
sub bail { 
	my $error = "@_";
	print "Error! $error";
	exit;
}
########################################################################


sub Print_Nav
{
	open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
	while (<FILE>) {
		print;
	}
	close FILE;
}