| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Audio::MPD q{0.19.0}; |
| 5 |
use MpdToys; |
| 6 |
use Encode; |
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
mpinsert - insert song after currently playing song |
| 11 |
|
| 12 |
=head1 SYNOPSIS |
| 13 |
|
| 14 |
mpinsert [-n] [song ...] |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
B<mpinsert> inserts a song (or songs) into the playlist directly after |
| 19 |
the currently playing song. |
| 20 |
|
| 21 |
If no songs are specified on the command line, it will read a list from |
| 22 |
stdin. |
| 23 |
|
| 24 |
Songs may be specified the same as they would be to mpc add: As paths to |
| 25 |
files in the music database, or urls to stream. |
| 26 |
|
| 27 |
You can also enter the name of a playlist, or part of the name of an |
| 28 |
album, artist, or song. Matching items will be added to the playlist. |
| 29 |
|
| 30 |
(If the perl String::Approx module is available, it will be used to handle |
| 31 |
typos, etc in the names you enter.) |
| 32 |
|
| 33 |
=head1 OPTIONS |
| 34 |
|
| 35 |
=over 4 |
| 36 |
|
| 37 |
=item -n |
| 38 |
|
| 39 |
Print the playlist position number that the first song was inserted at. |
| 40 |
|
| 41 |
=back |
| 42 |
|
| 43 |
=item -p |
| 44 |
|
| 45 |
Play the song after inserting it. |
| 46 |
|
| 47 |
=back |
| 48 |
|
| 49 |
=head1 AUTHOR |
| 50 |
|
| 51 |
Copyright 2007-2009 Joey Hess <[email protected]> |
| 52 |
|
| 53 |
Licensed under the GNU GPL version 2 or higher. |
| 54 |
|
| 55 |
http://kitenet.net/~joey/code/mpdtoys |
| 56 |
|
| 57 |
=cut |
| 58 |
|
| 59 |
use Getopt::Long; |
| 60 |
my $shownum=0; |
| 61 |
my $playnow=0; |
| 62 |
GetOptions( |
| 63 |
"n" => \$shownum, |
| 64 |
"p" => \$playnow, |
| 65 |
) || usage(); |
| 66 |
|
| 67 |
sub usage { |
| 68 |
die "Usage: mpinsert [-n] [song ...]\n"; |
| 69 |
} |
| 70 |
|
| 71 |
my @list=@ARGV; |
| 72 |
if (! @list) { |
| 73 |
while (<>) { |
| 74 |
chomp; |
| 75 |
push @list, $_; |
| 76 |
} |
| 77 |
} |
| 78 |
@list=map { decode_utf8($_) } @list; |
| 79 |
|
| 80 |
my $mpd=Audio::MPD->new(conntype => "reuse"); |
| 81 |
my $pl=$mpd->playlist; |
| 82 |
|
| 83 |
@list=reverse @list if $mpd->current; |
| 84 |
|
| 85 |
my @out; |
| 86 |
my $firstsong; |
| 87 |
foreach my $item (@list) { |
| 88 |
if (! length $item) { |
| 89 |
die "no item specified to insert\n"; |
| 90 |
} |
| 91 |
|
| 92 |
my @matches=MpdToys::findmatchingsongs($item, $mpd); |
| 93 |
if (! @matches && MpdToys::canmatch_fuzzy()) { |
| 94 |
@matches=MpdToys::findmatchingsongs_fuzzy($item, $mpd); |
| 95 |
} |
| 96 |
|
| 97 |
foreach my $song (@matches) { |
| 98 |
$pl->add($song->file); |
| 99 |
my @items=$pl->as_items; |
| 100 |
if (! @items) { |
| 101 |
die "failed!"; |
| 102 |
} |
| 103 |
my $pos=$#items; |
| 104 |
|
| 105 |
# move from end to just after current |
| 106 |
my $current=$mpd->current; |
| 107 |
if ($current) { |
| 108 |
$pos=$current->pos+1; |
| 109 |
$pl->move($#items, $pos); |
| 110 |
} |
| 111 |
|
| 112 |
$firstsong=$pos unless defined $firstsong; |
| 113 |
push @out, ($pos+1) if $shownum; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ($shownum) { |
| 118 |
@out=reverse @out if $mpd->current; |
| 119 |
print $out[0]."\n" if @out; |
| 120 |
} |
| 121 |
if ($playnow && defined $firstsong) { |
| 122 |
$mpd->play($firstsong); |
| 123 |
} |