Ago 7 2008, 11:25
Shit, I can't stop coding :). Here is a new version with some bug fixes and improvements.
Changelog:
-a better last.fm stream detection
-lower traffic usage (connect to last.fm only when playing last.fm stream)
-a new source tag (%c)
-disable whitespace after output (fe. for use with conky)
#!/usr/bin/perl
# Author: GagaPete
# Date: 2008-08-08
# 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
-n, --nonewlineDon\'t draw a newline after output
format:
%csource of the current song (last.fm, mpd or radio)
%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.3 by GagaPete
';
# parse options
if($ARGV) {
print $help;
exit 0;
}
my $nonewline = 0;
my $format = '';
foreach $arg (0 .. $#ARGV) {
if($ARGV[$arg] eq '--') {
foreach $num2 (($arg+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($1 eq 'n') {
$nonewline = 1;
}
} elsif($ARGV[$arg] =~ m/^--(.*)$/) {
if($1 eq 'help') {
print $help;
exit 0;
} elsif($1 eq 'version') {
print $version;
exit 0;
} elsif($1 eq 'nonewline') {
$nonewline = 1;
}
} else {
foreach $arg2 ($arg .. $#ARGV) {
$format .= $ARGV[$arg2];
}
last;
}
}
# new mpd object
my $mpd = Audio::MPD->new(
hostname => $mpd_hostname,
port => $mpd_port,
password => $mpd_password,
);
# get the playing song by mpd
my $mpd_nowplaying = $mpd->current();
# split the song informations
if( $mpd_nowplaying->file() =~ m/^http:\/\/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):80\/last\.mp3\?Session=([a-z][0-9])*/ ) {
my $lastfm = WebService::LastFM->new(
username => $last_username,
password => $last_password,
);
$lastfm->get_session();
my $lastfm_nowplaying = $lastfm->get_nowplaying();
$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;
$source = 'last.fm';
} 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;
if( $mpd_nowplaying->file() =~ m/^http:\/\// ) {
$source = 'radio';
} else {
$source = 'mpd';
}
}
# proceed format string
$format =~ s/%c/$source/;
$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;
if($nonewline eq 0) {
print '
';
}