Created
March 11, 2009 20:56
-
-
Save Daenyth/77731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment