| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use strict; |
| 5 |
use File::Spec; |
| 6 |
use File::Temp; |
| 7 |
use Audio::MPD q{0.19.0}; |
| 8 |
use MpdToys; |
| 9 |
use Encode; |
| 10 |
|
| 11 |
=head1 NAME |
| 12 |
|
| 13 |
vipl - edit mpd playlist |
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
B<vipl> [host] |
| 18 |
|
| 19 |
=head1 DESCRIPTION |
| 20 |
|
| 21 |
vipl allows editing the mpd playlist using your text editor. The current |
| 22 |
playlist will be brought up in the editor. Delete or rearrange songs as |
| 23 |
desired using the editor. |
| 24 |
|
| 25 |
You can also enter the name of a playlist, or part of the name of an |
| 26 |
album, artist, or song. Matching items will be added to the playlist. |
| 27 |
Streaming urls can also be entered. |
| 28 |
|
| 29 |
(If the perl String::Approx module is available, it will be used to handle |
| 30 |
typos, etc in the names you enter.) |
| 31 |
|
| 32 |
The currently playing song is marked with a ">" at the front. To change |
| 33 |
which song is playing, just move the ">" to a different song. |
| 34 |
|
| 35 |
If the hostname is omitted, the MPD_HOST environment variable will be used. |
| 36 |
|
| 37 |
=head1 AUTHOR |
| 38 |
|
| 39 |
Copyright 2008 by Joey Hess <[email protected]> |
| 40 |
|
| 41 |
Licensed under the GNU GPL. |
| 42 |
|
| 43 |
=cut |
| 44 |
|
| 45 |
if (@ARGV) { |
| 46 |
$ENV{MPD_HOST}=shift; |
| 47 |
} |
| 48 |
my $mpd=Audio::MPD->new(conntype => "reuse"); |
| 49 |
my $current=$mpd->current; |
| 50 |
my $pl=$mpd->playlist; |
| 51 |
my %names=map { $_->as_string => $_ } $pl->as_items; |
| 52 |
my @origlist=$pl->as_items; |
| 53 |
|
| 54 |
my $tmp=File::Temp->new(TEMPLATE => "plXXXXX", DIR => File::Spec->tmpdir); |
| 55 |
open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n"; |
| 56 |
foreach my $song (@origlist) { |
| 57 |
print OUT ">" if defined $current && $song->id eq $current->id; |
| 58 |
print OUT encode_utf8($song->as_string); |
| 59 |
print OUT "\n"; |
| 60 |
} |
| 61 |
close OUT || die "$0: cannot write ".$tmp->filename.": $!\n"; |
| 62 |
|
| 63 |
my @editor="vi"; |
| 64 |
if (-x "/usr/bin/editor") { |
| 65 |
@editor="/usr/bin/editor"; |
| 66 |
} |
| 67 |
if (exists $ENV{EDITOR}) { |
| 68 |
@editor=split(' ', $ENV{EDITOR}); |
| 69 |
} |
| 70 |
if (exists $ENV{VISUAL}) { |
| 71 |
@editor=split(' ', $ENV{VISUAL}); |
| 72 |
} |
| 73 |
my $ret=system(@editor, $tmp); |
| 74 |
if ($ret != 0) { |
| 75 |
die "@editor exited nonzero, aborting\n"; |
| 76 |
} |
| 77 |
|
| 78 |
my $changed=0; |
| 79 |
my @list; |
| 80 |
open (IN, "<".$tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n"; |
| 81 |
my $playing; |
| 82 |
my $num=0; |
| 83 |
while (<IN>) { |
| 84 |
chomp; |
| 85 |
$_=decode_utf8($_); |
| 86 |
|
| 87 |
if (s/^>\s*//) { |
| 88 |
$playing=$num; |
| 89 |
} |
| 90 |
|
| 91 |
if (exists $names{$_}) { |
| 92 |
push @list, $names{$_}; |
| 93 |
if (! $changed && !($origlist[$#list] && |
| 94 |
$names{$_}->file eq $origlist[$#list]->file)) { |
| 95 |
$changed=1; |
| 96 |
} |
| 97 |
$num++; |
| 98 |
} |
| 99 |
else { |
| 100 |
next if /^\s*$/; |
| 101 |
|
| 102 |
my @matches=MpdToys::findmatchingsongs($_, $mpd); |
| 103 |
if (! @matches && MpdToys::canmatch_fuzzy()) { |
| 104 |
@matches=MpdToys::findmatchingsongs_fuzzy($_, $mpd); |
| 105 |
} |
| 106 |
|
| 107 |
if (! @matches) { |
| 108 |
print STDERR "$0: no matches for \"$_\"\n"; |
| 109 |
} |
| 110 |
else { |
| 111 |
foreach (@matches) { |
| 112 |
push @list, $_; |
| 113 |
} |
| 114 |
$changed=1; |
| 115 |
$num+=@matches; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
close IN; |
| 120 |
if ($#list != $#origlist) { |
| 121 |
$changed=1; |
| 122 |
} |
| 123 |
|
| 124 |
if (! $changed) { |
| 125 |
# yay for optimisation! |
| 126 |
} |
| 127 |
elsif ($current && grep { $_->file eq $current->file } @list) { |
| 128 |
# Avoid touching the currently playing song, while changing |
| 129 |
# the playlist around it. |
| 130 |
$pl->crop; |
| 131 |
my $pos=0; |
| 132 |
my $past=0; |
| 133 |
foreach my $song (@list) { |
| 134 |
if (! $past && $song->file eq $current->file) { |
| 135 |
$past=1; |
| 136 |
# move current song into right location |
| 137 |
$pl->move(0, $pos); |
| 138 |
} |
| 139 |
else { |
| 140 |
$pl->add($song->file); |
| 141 |
} |
| 142 |
$pos++; |
| 143 |
} |
| 144 |
$mpd->play if @list; |
| 145 |
} |
| 146 |
else { |
| 147 |
$pl->clear; |
| 148 |
foreach my $song (@list) { |
| 149 |
$pl->add($song->file); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (defined $playing) { |
| 154 |
if (! defined $mpd->status->song || $playing ne $mpd->status->song || |
| 155 |
$mpd->status->state ne 'play') { |
| 156 |
$mpd->play($playing); |
| 157 |
} |
| 158 |
} |
| 159 |
else { |
| 160 |
$mpd->play(0) if @list; |
| 161 |
} |