All pastes #898241 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-02-10 04:48 UTC
rendered paste body
use IO::Socket::INET;
my $host = "76.252.211.91"; # change if you don't have localhost/other host default
my $port = shift || 6600;
        my $sock = IO::Socket::INET->new(PeerHost => $host, PeerPort => $port, Timeout => 3)
                or weechat::command("/say mpd is not running/failed to connect ($!)");
        if (!$sock) { return weechat::PLUGIN_RC_OK; }
        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: /);
                # $album   = (split(/\s/, $buf))[1] if ($buf =~ /Album: /);
                $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($length).")$reset";

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");
        }
}