muzack.cgi 755 52 52 12046 10276147674 10771 0 ustar user dos #!/opt/lampp/bin/perl
###############################################################################
# Author: Francis Turner Date: 9 August 2005
# (c) Francis Turner 2005
# Version 1.0
#
# Author contact information at http://www.di2.nu/content.htm
# Latest versions etc. of this files at http://www.di2.nu/software/index.htm
#
# These files are released under the Creative Commons Attribution-NonCommercial
# ShareAlike License. http://creativecommons.org/licenses/by-nc-sa/2.5/
#
# This script is used to run a daemon that controls realplayer
# The basic assumptions are that
# 1) realplayer in the path.
# 2) you have a webserver running with perl & CGI enabled
# 3) you music is based on a single root, is arranged by Artist/Album
# under that and is of types mp3, rmj or m4a (lower case)
# the whole of (2) other is optional if you write some other controller.
# Effectively no checking of permissions or authenication is performed. You may wish to
# change this if your webserver is publically accessable.
# Under no circumstances will the author accept any liablity for data loss
# caused by the execution of this program.
#
# Version 1.0 Initial public release
###############################################################################
use CGI ':standard';
use CGI::Carp qw/fatalsToBrowser/;
use IO::Socket;
my $PORT = 9000;
my $musicdir = '/opt/My Music';
# Query the daemon to see what is happening and what is queued up
# and put it on the web page
sub get_status {
my $remote = IO::Socket::INET->new( Proto => "tcp",
PeerAddr => 'localhost',
PeerPort => $PORT,
);
unless ($remote) { die "cannot connect to player daemon" }
$remote->autoflush(1);
print $remote "STATUS$/";
my $statusline = <$remote>;
close $remote;
# print $statusline;
my @tracks = split ('@@', $statusline);
print h3("Current status: ".(shift @tracks));
if ($#tracks>0) {
print $#tracks--," track(s) in the queue",
(($#tracks<0)?"":ul(li([map {substr($_,0,rindex($_,'.'))} @tracks])));
}
}
#load the available tracks from the file
sub get_tracks {
local $_ = $/;
local $/;
open (FILE, "<$musicdir/tracks.txt") or die "No info";
my $tracks = ;
close (FILE);
$/=$_;
return (split($/,$tracks));
}
#if there was a POST action then we need to do something so do it
if (param()) {
my $remote = IO::Socket::INET->new( Proto => "tcp",
PeerAddr => 'localhost',
PeerPort => $PORT,
);
unless ($remote) { die "cannot connect to player daemon" }
$remote->autoflush(1);
if (param('tracks')) {
print $remote $_.$/ foreach (param('tracks'));
}
print $remote 'QUIT'.$/.$/ if (param('action') eq 'Stop Daemon');
print $remote 'RELOAD'.$/.$/ if (param('action') eq 'Reload Daemon');
print $remote 'CLEAR'.$/ if (param('action') eq 'Clear Playlist');
print $remote 'SEARCH'.$/ if (param('action') eq 'Rescan Music');
close $remote;
# sleep 10 sec to let server finish music scanning so that
# we will use the updated track list
sleep 10 if (param('action') eq 'Rescan Music');
}
# note need to this after the param check incase the command was to rescan
my @tracks = get_tracks();
#start producing page
print header;
#simple security check just in case system is accidentally open to outside world
unless (($ENV{'REMOTE_ADDR'} eq '127.0.0.1')||($ENV{'REMOTE_ADDR'}=~/10.0.1.2/)) {
print start_html('Access Denied'),h1('Access Denied'),end_html;
exit;
}
print start_html(-title=>'Francis\' Realplayer Controller',
# refresh set to 60 secs by default comment out if it annoys
-head=>meta({-http_equiv => 'Refresh',-content => '60'}),
# comment out stylesheet or change it if required
-STYLE=>'/images/player.css' ),
h1('Realplayer Controller');
#output current daemon status
get_status();
print hr;
#the following comented out bit may be of interest for a more advanced
#track selection method
#my $last_cd;
#my %tracksinnameonly;
#for (@tracks) {#
# my $slash = rindex($_,'/');
# my $this_cd = substr($_,0,$slash); $this_cd=~s'/'
';
# my $track = substr($_,$slash+1);
#print "$this_cd ne $last_cd $slash.$_".br;
# print (($last_cd?'
':'').''.$this_cd.'
') if ($this_cd ne $last_cd);
# $tracksinnameonly{$track}=substr($track,0,rindex($track,'.'));
# print li($tracksinnameonly{$track});
# $last_cd = $this_cd;
#}
print #'
'.
# now create multiselect list of tracks
start_form.scrolling_list(-name=>'tracks',-values=>\@tracks,
-labels=>{map {($_,substr($_,0,rindex($_,'.')))} @tracks},
-size=>20,-multiple=>'true').
# create the action buttons and end the page
p(submit('action','Add to Playlist').' '.
submit('action','Clear Playlist').' '.
button(-name=>'jsaction',-value=>'Current Status',
-onClick=>(param()?'location.replace("'.url.'")':'location.reload(true)'))).hr.
'Only use these if you know what you are doing'.
p(submit('action','Stop Daemon').' '.
submit('action','Reload Daemon').' '.
submit('action','Rescan Music')),end_form.end_html;
muzacksvr.pl 755 52 52 11172 10276145312 11357 0 ustar user dos #!/opt/lampp/bin/perl
###############################################################################
# Author: Francis Turner Date: 9 August 2005
# (c) Francis Turner 2005
# Version 1.0
#
# Author contact information at http://www.di2.nu/content.htm
# Latest versions etc. of this files at http://www.di2.nu/software/index.htm
#
# These files are released under the Creative Commons Attribution-NonCommercial
# ShareAlike License. http://creativecommons.org/licenses/by-nc-sa/2.5/
#
# This script is used to run a daemon that controls realplayer
# The basic assumptions are that
# 1) realplayer in the path.
# 2) you have a webserver running with perl & CGI enabled
# 3) you music is based on a single root, is arranged by Artist/Album
# under that and is of types mp3, rmj or m4a (lower case)
# the whole of (2) other is optional if you write some other controller.
# Effectively no checking of permissions or authenication is performed. You may wish to
# change this if your webserver is publically accessable.
# Under no circumstances will the author accept any liablity for data loss
# caused by the execution of this program.
#
# Version 1.0 Initial public release
###############################################################################
use IO::Socket;
use IO::Select;
my $PORT = 9000;
my $musicdir = '/opt/My Music';
# set up port for receiving commands - limited to localhost by default
my $ui = IO::Socket::INET->new( Proto => 'tcp',
LocalAddr => 'localhost',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1 ) or die "Couldn't open the Player daemon port $PORT";
# This is used to perform select calls on the $server socket so that we can decide when there is
# a call waiting an accept it without having to block
my $uiselector = new IO::Select( $ui );
sub globby {
# magic glob extension that takes a directory with spaces in it
# escapes the space(s) and add a /* at the end just in case too
local $_ = shift;
s/ /\\ /g;
return (glob($_.'/*'));
}
# search for music to store in tracks.txt file
sub redotracklist {
open (FILE, ">$musicdir/tracks.txt");
my @m = globby($musicdir);
for (@m) {
#print " $_\n";
push @m, globby($_) if (-d($_));
if (/\.(?:mp3|m4a|rmj)$/) {
print FILE substr($_,length($musicdir)+1).$/;
my @f=split ('/',substr($_,length($musicdir)+1));
print join(':',@f).$/;
}
}
close FILE;
}
my $prev = "Server Started"; #server status
print $prev.$/;
my @files; # list of tracks queued to be played
my $current_track; # track that we have launched (if any)
my $alive = 1; # keep looping until told otherwise
while ( $alive > 0 ) {
my $client;
select(undef, undef, undef, 0.5); # half sec wait
# this is a bit of hack to see if real player is running
# if you installed realplayer somewhere funny then change it
my $now = ((`ps -aux` =~m!RealPlayer/realplay.bin!s)?"Playing":"Quiet");
print $now.$/ if ($now ne $prev);
$prev = $now;
if (($#files >=0) && ($now eq "Quiet")) {
my $q= $#files;
$_ = shift (@files);
print qq(Executing [realplay -q "$_" & ] $q left in Q\n);
system (qq(realplay -q "$musicdir/$_" &));
$current_track = ' - '.substr($_,0,rindex($_,'.'));
}
# See if there's a connection waiting on the $server by getting the list of handles with data to
# read, if the handle is the server then we're off. Note the 0.1 second delay here when waiting
# around. This means that we don't hog the processor while waiting for connections.
my ($uiready) = $uiselector->can_read(0.1);
# Handle player requests for the UI
if ( ( defined($uiready) ) && ( $uiready == $ui ) ) {
if ( $client = $ui->accept() ) {
my ( $remote_port, $remote_host ) = sockaddr_in( $client->peername() );
$remote_host= join(".", unpack('C4',$remote_host));
print "Connection from $remote_host\n";
close $client and next unless ($remote_host eq '127.0.0.1');
while (<$client>) {
chomp;
print qq(rx $_\n);
$alive = 0 if /QUIT/;
$alive = -1 if /RELOAD/;
$#files = -1 if /CLEAR/;
redotracklist() if /SEARCH/;
print $client join('@@',($now.(($now eq "Quiet")?'':$current_track)),@files,$/) if /STATUS/;
push @files, $_ if (-f "$musicdir/$_"); # only play tracks that exist
}
close $client;
print "Finished Transaction\n"
}
# Check for a possible abort
last if ( $alive <= 0 );
}
}
print 'Quitting'.(($alive <0)?' and Reloading':'').$/;
exec ($0) if ($alive <0);
exit (($alive==0)?0:42);