You are not logged in.

#1 2008-08-08 09:45:09

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

MPD & Artist Name

Hello all.
How to get current playing artist\ song from mpd.
In Python\Perl or ruby?)

Offline

#2 2008-08-08 12:10:33

patogen
Member
Registered: 2008-05-11
Posts: 86

Re: MPD & Artist Name

Dimon-z wrote:

Hello all.
How to get current playing artist\ song from mpd.
In Python\Perl or ruby?)

There is no need for python/perl/ruby to do this, the easiest way to do this is via mpc (which is small! and shouldn't be a problem), just running "mpc" gives something like:

Infected Mushroom - Disco Mushroom
[playing] #12/12   4:13/8:47 (48%)
volume: 90%   repeat: off   random: off

And you can use head to get the first line (Infected Mushroom - Disco Mushroom in this case):

mpc | head -1

Will gives you the first line. I do not know about Perl or Ruby but Python has built in so you can run programs from the shell within a python program.

Last edited by patogen (2008-08-08 12:11:34)


We met up with the aliens and guess what? They have no word for fluffy!

Offline

#3 2008-08-08 12:32:14

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

Re: MPD & Artist Name

Hm...Thank.
Me need clean programm\script with don't installed other programms wink

Offline

#4 2008-08-08 13:41:50

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: MPD & Artist Name

telnet, the commands are on the mpd website


-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'

Offline

#5 2008-08-08 13:57:27

nogoma
Member
From: Cranston, RI
Registered: 2006-03-01
Posts: 217

Re: MPD & Artist Name

There is ruby-mpd in community, although I think it's outdated. It still seems to work for me:

[nogoma@alfheim:~/src/3rdparty/wmii]% irb
ruby>> require 'mpd'
# => true  
ruby>> m = MPD.new('localhost')
# => #<MPD:0xb78f82a4 @debug_socket=false, @mpd_version=nil, @allow_toggle_states=true, @socket=nil, @mpd_port=6600, @overwrite_playlist=true, @mpd_host="localhost", @password=nil, @error=nil>  
ruby>> p "#{m.currentsong.artist} - #{m.currentsong.title}"
"The Kooks - Hateful Of Love"

-nogoma
---
Code Happy, Code Ruby!
http://www.last.fm/user/nogoma/

Offline

#6 2008-08-08 14:04:38

Kane
Member
Registered: 2006-10-08
Posts: 220

Re: MPD & Artist Name

you can do it easily with python and python-mpd

import mpd

## Connect to mpd client ###
client = mpd.MPDClient()
client.connect("localhost", 6600)

## Get current song info ###
curSongInfo = client.currentsong()
print "\n\n", curSongInfo['artist'] + " - " + curSongInfo['title'] + "\n"

Offline

#7 2008-08-08 20:16:21

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

Re: MPD & Artist Name

Me showed this error
AttributeError: 'module' object has no attribute 'MPDClient' :'(
how to fix?

Last edited by Dimon-z (2008-08-08 20:24:42)

Offline

#8 2008-08-08 21:03:37

Kane
Member
Registered: 2006-10-08
Posts: 220

Re: MPD & Artist Name

have you got python-mpd installed?

Offline

#9 2008-08-09 10:29:22

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

Re: MPD & Artist Name

yes

Offline

#10 2008-08-09 15:10:32

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: MPD & Artist Name

This is how I do it:

file getsong:

#!/bin/bash
echo -e "status\\nclose" | nc $MPD_HOST 6600 | is_mpd_playing.pl && echo -e "currentsong\\nclose" | nc $MPD_HOST 6600 | parse_song_info.pl

file is_mpd_playing.pl:

#!/usr/bin/env perl
use strict;

my $exit = -1;
while(<>) {
    chomp;
    if(/state: (.*)/) {
        my $state = $1;
        if($state =~ /stop/ or $state =~ /pause/) {
            $exit = 1;
        } elsif($state =~ /play/) {
            $exit = 0;
        }
        last;
    }
}
exit $exit;

file parse_song_info.pl:

#!/usr/bin/perl
use strict;

my($artist, $title);
while(<>) {
    chomp;
    /Artist: (.*)/ and $artist = $1;
    /Title: (.*)/ and $title = $1;
}
$artist and $title and print "$artist :: $title\n";

Offline

#11 2008-08-13 10:29:15

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

Re: MPD & Artist Name

thx, i reinstall python-mpd and all worked smile
How to make: if current song changed, print new song\artist name?)
Thanks smile

Offline

#12 2008-08-13 11:41:14

bavardage
Member
Registered: 2008-02-17
Posts: 160

Re: MPD & Artist Name

Just do a loop:

import time
lastSong = None
while True:
   currentSong = client.currentsong()
   if currentSong != lastSong:
      print "\n\n", currentSong['artist'] + " - " + currentSong['title'] + "\n"
      lastSong = currentSong
   time.sleep(2) #change this to however often you wanna poll.

Offline

#13 2008-08-13 12:28:30

Dimon-z
Member
Registered: 2008-06-22
Posts: 92

Re: MPD & Artist Name

Hm...
If i using u code my irc client is freezing(((
Client xChat

Last edited by Dimon-z (2008-08-13 13:02:11)

Offline

#14 2008-08-14 16:04:30

bavardage
Member
Registered: 2008-02-17
Posts: 160

Re: MPD & Artist Name

Hmm works fine here... No idea what it has to do with irc client.

Offline

Board footer

Powered by FluxBB