Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Perl by minoru ( 17 years ago )
# MPD Now-Playing Script for irssi
# Copyright (C) 2005 Erik Scharwaechter
# <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# The full version of the license can be found at
# http://www.gnu.org/copyleft/gpl.html.
#
#
#######################################################################
# I'd like to thank Bumby <[email protected]> for his impc script,  #
# which helped me a lot with making this script.                      #
#######################################################################
# Type "/np help" for a help page!                                    #
#######################################################################
# CHANGELOG:                                                          #
#  0.4: First official release                                        #
#  0.5: Info message if no song is playing                            #
#       Display alternative text if artist and title are not set      #
#       Some minor changes                                            #
#  0.6: (Minoru)                                                      #
#       Format of output changed to '/me is now listening to'         #
#       Script now delete file extension when show filename           #
#       I added myself to authors :D                                  #
#       License changed to CPLv3                                      #
#       Added %ALBUM                                                  #
#       Colors in format message                                      #
#######################################################################

use strict;
use IO::Socket;
use Irssi;

use vars qw{$VERSION %IRSSI %MPD};

$VERSION = "0.6";
%IRSSI = (
          name        => 'mpd',
          authors     => 'original by Erik Scharwaechter, modifyed by Minoru',
          contact     => '[email protected], [email protected]',
          license     => 'GPLv3',
          description => 'print the song you are listening to',
         );

sub my_status_print {
    my($msg,$witem) = @_;

    if ($witem) {
        $witem->print($msg);
    } else {
        Irssi::print($msg);
    }
}

sub np {
    my($data,$server,$witem) = @_;

    if ($data =~ /^help/) {
        help();
        return;
    }

    $MPD{'port'}    = Irssi::settings_get_str('mpd_port');
    $MPD{'host'}    = Irssi::settings_get_str('mpd_host');
    $MPD{'timeout'} = Irssi::settings_get_str('mpd_timeout');
    $MPD{'format'}  = Irssi::settings_get_str('mpd_format');
    $MPD{'alt_text'} = Irssi::settings_get_str('mpd_alt_text');

    my $socket = IO::Socket::INET->new(
                          Proto    => 'tcp',
                          PeerPort => $MPD{'port'},
                          PeerAddr => $MPD{'host'},
                          timeout  => $MPD{'timeout'}
                          );

    if (not $socket) {
        my_status_print('No MPD listening at '.$MPD{'host'}.':'.$MPD{'port'}.'.', $witem);
        return;
    }

    $MPD{'status'}   = "";
    $MPD{'artist'}   = "";
    $MPD{'title'}    = "";
    # Added by Minoru
    $MPD{'album'} = "";
    $MPD{'filename'} = "";

    my $ans = "";
    my $str = "";

    # NOTE by Minoru: it's better for you to follow this link before reading code:
    # http://mpd.wikia.com/wiki/MusicPlayerDaemonCommands

    print $socket "status
";
    while (not $ans =~ /^(OK$|ACK)/) {
        $ans = <$socket>;
        if ($ans =~ /state: (.+)$/) {
            $MPD{'status'} = $1;
        }
    }

    if ($MPD{'status'} eq "stop") {
        my_status_print("No song playing in MPD.", $witem);
        close $socket;
        return;
    }

    print $socket "currentsong
";
    $ans = "";
    while (not $ans =~ /^(OK$|ACK)/) {
        $ans = <$socket>;
        if ($ans =~ /file: (.+)$/) {
            my $filename = $1;
            $filename =~ s/.*///;
	    $filename =~ s/..*//e;
            $MPD{'filename'} = $filename;
        } elsif ($ans =~ /Artist: (.+)$/) {
            $MPD{'artist'} = $1;
        } elsif ($ans =~ /Title: (.+)$/) {
            $MPD{'title'} = $1;
        }
        # Added by Minoru
        elsif ($ans =~ /Album: (.+)$/) {
            $MPD{'album'} = $1;
        }
    }

    close $socket;

    if ($MPD{'artist'} eq "" and $MPD{'title'} eq "") {
        $str = $MPD{'alt_text'};
    } else {
        $str = $MPD{'format'};
    }

    # Added by Minoru
    # Unknown Trek by Unknown Artist from Unknown Album THE BEST!!!
    if ($MPD{'artist'} eq "") { $MPD{'artist'} = "Unknown Artist" };
    if ($MPD{'title'} eq "") { $MPD{'title'} = "Unknown Trek" };
    if ($MPD{'album'} eq "") { $MPD{'album'} = "Unknown Album" };

    $str =~ s/\%ARTIST/$MPD{'artist'}/g;
    $str =~ s/\%TITLE/$MPD{'title'}/g;
    $str =~ s/\%FILENAME/$MPD{'filename'}/g;
    # Added by Minoru
    $str =~ s/\%ALBUM/$MPD{'album'}/g;

    if ($witem && ($witem->{type} eq "CHANNEL" ||
                   $witem->{type} eq "QUERY")) {
        if($MPD{'format'} =~ /^/me /) {
            $witem->command($str);
        } else {
            # Changed by Minoru
            #$witem->command("ME ".$witem->{name}." is now listening to $str");
            $witem->command("ME is now listening to $str");
        }
    } else {
        Irssi::print("You're not in a channel.");
    }
}


sub help {
   print '
 MPD Now-Playing Script
========================

by Erik Scharwaechter ([email protected])
modifyed by Minoru ([email protected])

VARIABLES
  mpd_host      The host that runs MPD (localhost)
  mpd_port      The port MPD is bound to (6600)
  mpd_timeout   Connection timeout in seconds (5)
  mpd_format    The text to display (np: %%ARTIST - %%TITLE)
  mpd_alt_text  The Text to display, if %%ARTIST and %%TITLE are empty (np: %%FILENAME)

USAGE
  /np           Print the song you are listening to
  /np help      Print this text
';
}


Irssi::settings_add_str('mpd', 'mpd_host', 'localhost');
Irssi::settings_add_str('mpd', 'mpd_port', '6600');
Irssi::settings_add_str('mpd', 'mpd_timeout', '5');
# Irssi::settings_add_str('mpd', 'mpd_format', '%TITLE by %ARTIST from %ALBUM');
# Here I used colors, you may need this table
# 02 mean bold (Usage: 02Here is bold text02)
# 37 mean underlined text (Usage: 37Here is underlined text37)
# 03fg[,bg] &acirc;€” set foreground and background colors (Usage: 033Here is green text03  or 038,1Here is yellow text at black background03)
# Table of mIRC colors (it's standard de-facto in IRC world):
#  0  white
#  1  black
#  2  blue
#  3  green
#  4  lightred
#  5  brown
#  6  purple
#  7  orange
#  8  yellow
#  9  lightgreen
# 10  cyan
# 11  lightcyan
# 12  lightblue
# 13  pink
# 14  grey
# 15  lightgrey
Irssi::settings_add_str('mpd', 'mpd_format', "0305%TITLE03 by 0303%ARTIST03 from 0310%ALBUM03");
Irssi::settings_add_str('mpd', 'mpd_alt_text', "020314%FILENAME0302");

Irssi::command_bind np        => &np;
Irssi::command_bind 'np help' => &help;

 

Revise this Paste

Your Name: Code Language: