File: [local] / mp3 / bin / showplaylist.pl (download)
Revision 1.3, Thu Feb 8 20:04:03 2007 UTC (17 years, 7 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD Changes since 1.2: +30 -16 lines
First shot at making multiple jukeboxes from the same files.
|
#!/usr/bin/perl -w
# $RedRiver: showplaylist.pl,v 1.2 2006/08/12 00:14:53 andrew Exp $
########################################################################
# ShowPlaylist.pl *** Displays our current playlist on a web page
#
# 04-14-00
# Written by andrew fresh <andrew@mad-techies.org>
########################################################################
use strict;
use diagnostics;
#use MP3::Info;
#my qw/ $list @playlist $maplay $filename $basedir $htmldir $add /;
my $args = shift;
my $exe = 'removemp3playlist.pl';
#######################################################################
# *** MAIN ***
#######################################################################
my ($prefix, $listfile, $add) = split /\&/, $ENV{'QUERY_STRING'};
if ($listfile) {
$listfile =~ s/%(..)/pack("c",hex($1))/ge;
} else {
$listfile ='';
}
if ($add) {
$add =~ s/%(..)/pack("c",hex($1))/ge;
} else {
$add ='';
}
if ($prefix) {
$prefix =~ s/%(..)/pack("c",hex($1))/ge;
} else {
$prefix ='';
}
my $playlist = '/var/www/mp3/playlist/' . $prefix . 'playlist.lst';
$listfile ||= 'playlist.lst';
my $list = '/var/www/mp3/playlist/' . $prefix . $listfile;
if ($listfile eq 'playlist.lst' || $listfile eq 'fulllist.lst') {
$add = undef;
} else {
$exe = 'addmp3playlist.pl';
}
my @playlist = get_playlist($list);
print "Content-Type: text/html\n\n";
print "\n\n<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> <title>$listfile</title>\n";
print "<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"300\">\n";
print "<body>\n\n";
use CGI::Carp qw/ fatalsToBrowser /;
#Print_Nav();
#print "args: $args\n";
if ($listfile eq 'playlist.lst') {
print '<a href="deleteplaylist.pl">Delete Playlist</a>';
}
unless ($add) {
print "<h1>Currently in $listfile as of " . GetTime() . "</h1><hr>\n";
my $file = $0;
$file =~ s#^.*[//]##;
print "<a href='$file?$listfile\&ADD'>Add All</A><br>\n";
} else {
print "<h1>Added to playlist from $listfile as of " . GetTime() . "</h1><hr>\n";
}
if (@playlist) {
my $counter;
foreach my $filename (@playlist) {
if ($add) {
AddSong($filename);
} else {
print "\t<li><a href=\"$exe\?" . EncodeURL($prefix) . '&' . EncodeURL($filename) . '&' . EncodeURL($listfile) . "\" target=\"bottom\">$filename</a></li>\n";
}
$counter++;
}
print "<H3>Total displayed: $counter</h3>\n";
} else {
print "\t<li>Nothing in list</li>\n";
}
print "</UL>\n</body>\n</head>\n</html>\n";
#########################################################################
# GetTime
sub GetTime {
my $hours = shift;
my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime(); # 86400 seconds is one day
if ($min < 10) { $min = "0$min"}
if ($sec < 10) { $sec = "0$sec"}
if ($hour < 10) { $hour = "0$hour"}
if ($mday < 10) { $mday = "0$mday"}
if ($mon < 10) { $mon = "0$mon"}
my $time = ($year + 1900) . '-' . ++$mon . '-' . $mday . ' ' . $hour . ':' . $min . ':' . $sec;
return $time;
}
#########################################################################
#######################################################################
# read in the Playlist
sub get_playlist {
my $FILE = shift;
my @lines;
if (-e $FILE) {
open (FILE, $FILE) || bail ("Couldn\'t open $FILE: $!");
chomp (@lines = <FILE>);
close (FILE) || bail ("Couldn't close $FILE: $!");
}
return @lines;
}
########################################################################
# *** EncodeURL: %encodes the parameters of the URL
sub EncodeURL {
my $strURL = shift;
$strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
return $strURL;
}
#######################################################################
# 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 "Unexpected Error: $error";
exit;
}
sub Print_Nav
{
open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
while (<FILE>) {
print;
}
close FILE;
}
sub AddSong
{
my $filename = shift;
$filename =~ s/%(..)/pack("c",hex($1))/ge;
if ($filename) {
open PLAYLIST, ">>$prefix$playlist" or bail("unable to open PLAYLIST: $!");
print PLAYLIST "$filename\n";
close PLAYLIST or bail("unable to close PLAYLIST: $!");
print "Added $filename<br>\n";
} else {
print "<h1>You need to pass a song</h1>";
}
}