public
Last active

  • Download Gist
mdctl.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#!/usr/bin/perl -w
use strict;
use feature ':5.10';
use Audio::MPD;
 
my $mpd = Audio::MPD->new;
 
my $command = shift;
given ($command) {
when ('add') {
my $playlist = shift;
my @paths = @ARGV || ($mpd->current->file);
add_to_playlist( $playlist, @paths );
}
when ('change') {
my $playlist = shift;
smooth_playlist_change( $playlist );
}
when ('del') {
$mpd->playlist->deleteid( $mpd->current->id );
}
when ('save') {
my $playlist = shift;
update_playlist( $playlist );
}
when ('prune') {
my $needle = shift;
my $type = $ARGV[0] || 'file';
prune_playlist( $needle, $type );
}
 
default {
die "Illegal command. Phoning the police...\n";
}
}
 
sub update_playlist {
my $plname = shift;
$mpd->playlist->rm( $plname );
$mpd->playlist->save( $plname );
}
 
sub smooth_playlist_change {
my $plname = shift;
 
my $pl_length = $mpd->status->playlistlength - 1;
my $cur = $mpd->status->song;
my $cur_id = $mpd->current->id;
 
my $command =
"command_list_begin\n" .
qq[load "$plname"\n] .
join( '', map { $_ != $cur ? "delete $_\n" : '' } reverse 0..$pl_length ) .
"deleteid $cur_id\n" .
"command_list_end\n";
$mpd->_send_command( $command );
}
 
sub add_to_playlist {
my ($plname, @paths) = @_;
 
my $command =
"command_list_begin\n"
. join( '', map { s/"/\\"/g; qq[playlistadd $plname "$_"\n] } @paths )
. "command_list_end\n";
$mpd->_send_command( $command );
}
 
sub playlist_as_items {
my $plname = shift;
return $mpd->_cooked_command_as_items("listplaylistinfo $plname\n");
}
 
sub prune_playlist {
my ($needle, $type) = @_;
 
my @playlist = $mpd->playlist->as_items;
my @bad_tracks = map { $_->id } grep { $_->$type ~~ /$needle/ } @playlist;
$mpd->playlist->deleteid( @bad_tracks );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.