All pastes #755582 Raw Edit

Someone

public text v1 · immutable
#755582 ·published 2007-10-30 19:52 UTC
rendered paste body
#!/usr/bin/perl -w
use strict;
my $host = shift || "localhost"; # change if you don't have localhost/other host default
my $port = shift || 6600;
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(PeerHost => $host, PeerPort => $port)
	or die "mpd is not running/failed to connect to $host:$port ($!)\n";
print $sock "currentsong\n";
my $buf;
my $length;
my $currlen;
my $title;
my $artist;
my $album;
my $id;

while ($buf = <$sock>) {
	$length  = (split(/\s/, $buf))[1] if ($buf =~ /Time: /);
	$artist  = substr($buf, 8) if ($buf =~ /Artist: /);
	$title   = substr($buf, 7) if ($buf =~ /Title: /);
	$currlen = (split(/\s/, $buf))[1] if ($buf =~ /Pos: /);
	$id      = (split(/\s/, $buf))[1] if ($buf =~ /Id: /);
	if ($buf =~ /OK\n/) {
		print $sock "close\n";
		last();
	}
}

my $bold = chr(2); my $reset = chr(15); my $red = chr(3)."4";
chomp($artist); chomp($title); chomp($currlen); chomp($length);
print "np: $bold$artist$reset - $bold$title$reset $red(".conv($currlen)."/".conv($length).")$reset\n";

# The following is from Violet4's Conversion module
sub conv {
        my $time = shift;

        # Xs
        if ($time < 60) {
                return($time."s");
        }

        # Xm Xs
        if ($time < 3600 && $time > 59) {
                my $minutes = int($time / 60);
                my $seconds = $time - ($minutes * 60);
                return($minutes."m ".$seconds."s");
        }

        # Xh Xm Xs
        if ($time < 86400 && $time > 3599) {
                my $hours   = int($time / (60 ** 2));
                my $temp    = $time - ($hours * (60 ** 2));
                my $minutes = int($temp / 60);
                my $seconds = $temp - ($minutes * 60);
                return($hours."h ".$minutes."m ".$seconds."s");
        }
}