All pastes #716129 Raw Edit

K0brik

public text v1 · immutable
#716129 ·published 2007-09-26 15:43 UTC
rendered paste body
#!/usr/bin/perl -w
use IO::Handle;
use POSIX ":sys_wait_h";
pipe PLAYER_RD, DJ_WR;
pipe DJ_RD, PLAYER_WR;
DJ_WR->autoflush(1);
PLAYER_WR->autoflush(1);
my $player = fork;
print "fork:$!\n" if not defined $player;
if(!$player) {
        # The player
        close DJ_WR;
        print "Starting player...\n";
        my $gst = undef;
        my $playing = undef;
        while(1) {
                my $stream = <PLAYER_RD>;
                if ($stream =~ /^what'?s playing?/) {
                        print PLAYER_WR "$playing is playing!\n";
                        next;
                } 
                chomp $stream;
                kill 15, $gst if defined $gst && waitpid(-1, WNOHANG) != -1;
                $gst = fork;
                exec "gst-launch-0.10 playbin uri=$stream" if !$gst;
                $playing = $stream;
        }
        print "Closing player...\n";

} else {
        # The DJ
        print "Hey! I'm the DJ!!!\n";
        my @stream = (  "http://eclipse.desync.com:8090\n",
                        "http://divbyzero.de:8100\n",
                        "http://scfire-ntc-aa04.stream.aol.com:80/stream/1021\n");

        my @queue; 

        while(<>) {
                kill 19, $1 if(/stop (\d+)/);
                kill 18, $1 if(/start (\d+)/);
                if (/what'?s playing?/) {
                        print DJ_WR "$&\n";
                        my $fb = <DJ_RD>;
                        print "fb: $fb\n";
                } elsif(/rand/) {

                        print DJ_WR $stream[int rand @stream];

                } elsif($_ =~ /agrep (.*?)( (\d+)|$)/) {
                        my $pattern = $1;
                        my $interval = $3;
                        print my @str = grep /$pattern/, @stream;
                        if($interval) {
                                push @queue, my $pid = fork;
                                if(!$pid){
                                    sleep int rand $interval*60;
                                    while(1) {
                                                my $next_stream = $str[int rand @str];
                                                print "$$ switching channel to $next_stream";
                                                print DJ_WR $next_stream;
                                                sleep $interval*60;
                                    }
                                }
                        } else {
                                print DJ_WR $str[int rand @str];
                        }
                } else {
                        print <<USAGE
rand                    - Switch to a random stream
agrep pattern [n]       - Random select from streams matching pattern [every n minutes]
whats playing           - Get the address of the current stream
USAGE
                } 
        }
        print "Have a nice day. Bye!\n";
} 
# (c) dion.rasmussen@gmail.com