- 0 Talk
-
Hack:random-album.pl
random-album.pl is yet another program to play random album. Unlike others, befeore actually adding it to playlist, it asks user for confirmation if generated album is ok. User may then confirm it or change artis or just album.
Perhaps it will only work if your database is in form MPDROOT/ARTIST/YEAR-ALBUM (which I find useful anyway).
Newest version can always be found at http://minio.xt.pl/pliki/random-album.txt.
Source code
Edit
#!/usr/bin/perl
# Prosty skrypt do kolejkowania losowego albumu na playliście MPD. Potrafi od
# razu zacząć odtwarzanie jeżeli MPD jest bezczynne. Można podać preferowanego
# wykonawcę jako argument.
# Autor: Mirosław "Minio" Zalewski <http://minio.xt.pl>
use strict;
use warnings;
use utf8;
use Encode;
use POSIX qw(locale_h);
use Audio::MPD;
my $FINISH;
my $CHANGE;
my $ARTIST_FROM_ARG;
my $NOT_PLAYING;
my $read;
my $pl_length;
my $mpd = Audio::MPD->new(
#--------------------------------------------------
# Modify to your needs
# hostname => "",
# port => "",
# password => "",
#--------------------------------------------------
);
#--------------------------------------------------
# Let's call it i18n
#--------------------------------------------------
my %msg;
my $locale = setlocale(LC_MESSAGES);
if ($locale =~ m/pl_PL/i) {
$msg{'what_to_change'} = "Co chcesz zmienić?\n1.Album\n2.Wykonawcę\n";
$msg{'chosen'} = "Wylosowano";
$msg{'question'} = ".Pasi?\n1.Yup.\n2.Niet.\n";
} else {
$msg{'what_to_change'} = "What do you want to change?\n1.Album\n2.Artist\n";
$msg{'chosen'} = '';
$msg{'question'} = ". Is it ok?\n1.Yes.\n2.No.\n";
}
#--------------------------------------------------
# Taken from
# http://search.cpan.org/src/JQUELIN/Audio-MPD-0.19.2/examples/mpc.pl
#--------------------------------------------------
sub get_dirs {
my $dir = $_[0] if $_[0];
my @a;
foreach my $tmp ($mpd->collection->items_in_dir($dir)) {
my %hash = %{$tmp};
if($hash{'directory'}) {
push(@a, $hash{'directory'});
}
}
return @a;
}
sub rand_dir {
return $_[int(rand(@_))];
}
my @dir = &get_dirs;
my $artist;
if (defined $ARGV[0]) {
$artist = $ARGV[0];
$ARTIST_FROM_ARG = 1;
}
my ($album, $album_mpd);
until ($FINISH) {
if (! $CHANGE) {
$artist = &rand_dir(@dir) unless $ARTIST_FROM_ARG;
} else {
print encode("UTF-8", $msg{'what_to_change'});
$read=<STDIN>;
if ($read !~ m/^(1|Album|A)$/i) {
undef $CHANGE;
redo;
}
}
$album = $album_mpd = &rand_dir(&get_dirs($artist));
if ($album) {
if ($album =~ m:.+/(.+?)-(.+):i ) {
$album = $2 .' ('.$1.')';
} else {
$album =~ s:.+/(.+):$1:i
}
} else {
$album_mpd = $artist;
}
print encode("UTF-8", "$msg{'chosen'} $artist");
print encode("UTF-8", ' - '. $album) if ($album);
print encode("UTF-8", $msg{'question'});
$read=<STDIN>;
if ($read =~ m/^(y|yes|t|tak|1|yup)$/i) {
$FINISH=1;
} else {
undef $ARTIST_FROM_ARG if $ARTIST_FROM_ARG;
$CHANGE=1;
}
}
my $stan = $mpd->status->state();
if ($stan =~ m/stop/i) {
$NOT_PLAYING=1;
$pl_length = $mpd->status->playlistlength;
} elsif ($stan =~ m/pause/i) {
$NOT_PLAYING=1;
}
foreach my $tmp ($mpd->collection->all_items_simple($album_mpd)) {
my %hash = %{$tmp};
$mpd->playlist->add("$tmp") unless $hash{'directory'};
}
if ($NOT_PLAYING) {
$mpd->play($pl_length);
}