]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdreload.pl
mpdreload: Simplify script operation.
[mpdhacks.git] / mpdreload.pl
index ba822ae9de8cf05946038d43472726e59b5bc4d5..c82353eb3e91f435a7bc0103b00e2e5fa3de6691 100755 (executable)
@@ -26,39 +26,22 @@ use MPDHacks;
 
 my $sock;
 
-# Submit a command to the MPD server; each argument to this function
-# is quoted and sent as a single argument to MPD.
-sub mpd_exec {
-       my $cmd = join(' ', map { MPD::escape } @_);
-
-       print $sock "$cmd\n";
-}
-
-# Returns a hash reference containing all tracks in the current play queue.
-# The hash keys are filenames.
+# Returns a hash reference mapping filenames to an array reference listing
+# the queue IDs for that file in the current play queue.
 sub get_tracks_in_play_queue {
        my %matches;
        my $entry;
 
-       mpd_exec("playlistinfo");
+       MPD::exec("playlistinfo");
        while (<$sock>) {
                last if /^OK/;
                die($_) if /^ACK/;
 
                if (/^(\w+): (.*)$/) {
                        if ($1 eq "file") {
-                               if (exists($matches{$2})) {
-                                       $entry = $matches{$2};
-                               } else {
-                                       $entry = {};
-                                       $matches{$2} = $entry;
-                               }
-                       }
-
-                       if (exists($entry->{$1})) {
-                               $entry->{$1}->{$2} = 1;
-                       } else {
-                               $entry->{$1} = { $2 => 1 }
+                               $entry = $matches{$2} //= [];
+                       } elsif ($1 eq "Id") {
+                               push @$entry, $2;
                        }
                }
        }
@@ -72,7 +55,7 @@ sub get_playlist_files {
        my ($plname) = @_;
        my @files;
 
-       mpd_exec("listplaylist", $plname);
+       MPD::exec("listplaylist", $plname);
        while (<$sock>) {
                last if /^OK/;
                die($_) if /^ACK/;
@@ -98,10 +81,10 @@ EOF
 }
 
 sub print_usage {
-       my $fh = $_[1] // *STDERR;
+       my ($fh) = (@_, *STDERR);
 
        print $fh "Usage: $0 [options] playlist\n";
-       print "Try $0 --help for more information.\n" unless (@_ > 0);
+       print $fh "Try $0 --help for more information.\n" unless (@_ > 0);
 }
 
 sub print_help {
@@ -137,37 +120,22 @@ if (@ARGV != 1) {
 $sock = MPD::connect();
 
 # Retrieve the current play queue and target play queue.
+MPD::run("tagtypes", "clear");
 my $current = get_tracks_in_play_queue();
 my $target = get_playlist_files($ARGV[0]);
 
-mpd_exec("command_list_begin");
+MPD::exec("command_list_begin");
 for (my $i = 0; $i < @$target; $i++) {
        my $f = $target->[$i];
-       my $ids = $current->{$f}->{Id};
-
-       my $id = (keys %$ids)[0];
-       delete $ids->{$id};
-
-       # Remove tracks with no unused queue IDs
-       delete $current->{$f} unless (keys %$ids > 0);
+       my $id = shift @{ $current->{$f} };
 
        if (defined $id) {
-               mpd_exec("moveid", $id, $i);
+               MPD::exec("moveid", $id, $i);
        } else {
-               mpd_exec("addid", $f, $i);
+               MPD::exec("addid", $f, $i);
        }
 }
 
 # Remove any tracks left from the old play queue.
-foreach (keys %$current) {
-       my $ids = $current->{$_}->{Id};
-       foreach (keys %$ids) {
-               mpd_exec("deleteid", $_);
-       }
-}
-
-mpd_exec("command_list_end");
-while (<$sock>) {
-       last if /^OK$/;
-       die($_) if /^ACK/;
-}
+MPD::exec("deleteid", $_) foreach (map { @$_ } values %$current);
+MPD::run("command_list_end");