last_mpd_info version 0.2

RSS
Recomendar

Ago 6 2008, 21h58

Hi, a new release after there is a guy asking about getting specific infos about the current song. Look --help for informations

Changelog:
-a help text
-format the output with different informations

#!/usr/bin/perl

# Author: GagaPete
# Date: 2008-08-07
# License: Use as you wish at your own risk.

use Audio::MPD;
use WebService::LastFM;

# settings
$last_username="username";
$last_password="password";
$mpd_host="localhost";
$mpd_port=6600;
$mpd_password="";

# usage text
$help = 'usage: last_mpd_info [options] [--] format
options:
-h, --helpDisplay this help text
-v, --versionDisplay the version
format:
%aartist of the current track
%ttitle of the current track
%dalbum containing the track
%hlength of track in full hours
%mlength of track in full minutes (<60)
%slength of track in seconds (<60)
';
$version = 'last_mpd_info v0.2 by GagaPete
';
# parse options
if($#ARGV) {
print $help;
exit 0;
}

foreach $num (0 .. $#ARGV) {
if($ARGV[$arg] eq '--') {
$format = '';
foreach $num2 (($num+1) .. $#ARGV) {
$format .= $ARGV[$num2];
}
last;
} elsif($ARGV[$arg] =~ m/^-(.)$/) {
if($1 eq 'h') {
print $help;
exit 0;
} elsif($1 eq 'v') {
print $version;
exit 0;
}
} elsif($ARGV[$arg] =~ m/^--(.*)$/) {
if($1 eq 'help') {
print $help;
exit 0;
} elsif($1 eq 'version') {
print $version;
exit 0;
}
} else {
foreach $num2 (0 .. $#ARGV) {
$format .= $ARGV[$num2];
last;
}
}
}

# new last.fm object
my $lastfm = WebService::LastFM->new(
username => $last_username,
password => $last_password,
);
my $stream_info = $lastfm->get_session();

# new mpd object
my $mpd = Audio::MPD->new(
hostname => $mpd_hostname,
port => $mpd_port,
password => $mpd_password,
);

# get the song information you are now listening
my $lastfm_nowplaying = $lastfm->get_nowplaying();
my $mpd_nowplaying = $mpd->current();

# split the song informations
if( $lastfm_nowplaying->streaming eq true ) {
$artist = $lastfm_nowplaying->artist;
$title = $lastfm_nowplaying->track;
$album = $lastfm_nowplaying->album;
$hours = int($lastfm_nowplaying->trackduration / 360);
$minutes = int($lastfm_nowplaying->trackduration / 60);
$seconds = $lastfm_nowplaying->trackduration % 60;
} else {
$artist = $mpd_nowplaying->artist();
$title = $mpd_nowplaying->title();
$album = $mpd_nowplaying->album();
$hours = int($mpd_nowplaying->time() / 3600);
$minutes = int($mpd_nowplaying->time() / 60);
$seconds = $mpd_nowplaying->time() % 60;
}

# proceed format string
$format =~ s/%a/$artist/;
$format =~ s/%t/$title/;
$format =~ s/%d/$album/;
$format =~ s/%h/$hours/;
$format =~ s/%m/$minutes/;
$format =~ s/%s/$seconds/;

# output string
print $format;


Requirements:
-Perl
-CPAN Module Audio::MPD
-CPAN Module WebService::LastFM

Comentários

  • cyberdurand

    Thanks a lot! Is there any way to change the song information in MPD to show the correct last.fm stuff? I'm using the mcf bash script to play last.fm streams but i'm not sure how to change the title and artist that is displayed in sonata/mpc. Is it possible? There is a function in Music::Audioscrobbler::MPD for changing the song information: song_change($cinfo) - Run on song change I don't know perl but maybe you understand it? Danke Sehr!

    Ago 27 2008, 16h25
  • GagaPete

    Sorry for that long time without answering. song_change($cinfo) is just a handler, that is called whenever the song is changed. The only way to change the title and artist transmitted to the clients is to change the mpd source. As far as I know is support for Last.fm Stream build in MPD now, but I have no clue if it displays the correct artist and title using the that native way.

    Nov 25 2009, 22h41
  • cyberdurand

    Mmm, yeah, it does apparently have support but I have no idea how to use it. Thanks for your help anyway :)

    Nov 25 2009, 22h47
  • GagaPete

    You could compile MPD yourself with --enable-lastfm as ./configure option. Then you can just add the lastfm:// url as normal http streams (mpc add lastfm://artist/Oomph!). This should do this job.

    Nov 26 2009, 4h43
Ver todos os 4 comentários
Deixe um comentário. Faça login na Last.fm ou cadastre-se agora (é gratuito).