From 4fec499ce726a2dad91c05e2cd69566b31d9d15a Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Tue, 5 May 2020 00:41:14 -0400 Subject: [PATCH] Factor out mpd_exec from some of the perl scripts. The simple mpd_exec function can be easily moved into the MPDHacks module. Leave out the one in mpdexec.pl as it is more sophisticated to handle things like binary downloads and probably not needed for common code. --- MPDHacks.pm | 8 ++++++++ mpdmenu.pl | 24 ++++++++---------------- mpdreload.pl | 22 +++++++--------------- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/MPDHacks.pm b/MPDHacks.pm index 94c0acc..0f5026e 100644 --- a/MPDHacks.pm +++ b/MPDHacks.pm @@ -107,4 +107,12 @@ sub escape { return $s; } +# Submit a command to the MPD server; each argument to this function +# is quoted and sent as a single argument to MPD. +sub exec { + my $cmd = join(' ', map { MPD::escape } @_); + + print $sock "$cmd\n"; +} + 1; diff --git a/mpdmenu.pl b/mpdmenu.pl index 4916d2a..526772a 100755 --- a/mpdmenu.pl +++ b/mpdmenu.pl @@ -42,14 +42,6 @@ my ($topmenu, $menu); my $mode = "top"; my %artistids; -# 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"; -} - sub fvwm_cmd_unquoted { print join(' ', @_), "\n"; } @@ -281,7 +273,7 @@ sub get_tracks_by_work_mbid { my $entry; foreach my $mbid (@_) { - mpd_exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -310,13 +302,13 @@ sub get_tracks_by_work_mbid { # Currently tracks are considered "related" if their associated recordings # have at least one work in common. sub get_tracks_by_track_mbid { - my ($mbid) = @_; + my ($mbid, $tagname) = (@_, "MUSICBRAINZ_RELEASETRACKID"); my %source; my %matches; my $entry; return \%matches unless ($mbid); - mpd_exec("search", "(MUSICBRAINZ_RELEASETRACKID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_RELEASETRACKID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -348,7 +340,7 @@ sub get_tracks_by_release_mbid { my $entry; return \%matches unless ($mbid); - mpd_exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -380,7 +372,7 @@ sub get_releases_by_artist_mbid { my $entry; foreach my $mbid (@_) { - mpd_exec("search", "(MUSICBRAINZ_ARTISTID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_ARTISTID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -406,7 +398,7 @@ sub get_ids_by_filename { my ($file) = @_; my @results = (); - mpd_exec("playlistfind", "file", $file); + MPD::exec("playlistfind", "file", $file); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -516,7 +508,7 @@ if ($mode eq "top") { $menu //= "MenuMPD"; - mpd_exec("status"); + MPD::exec("status"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -526,7 +518,7 @@ if ($mode eq "top") { } } - mpd_exec("currentsong"); + MPD::exec("currentsong"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); diff --git a/mpdreload.pl b/mpdreload.pl index ba822ae..6305783 100755 --- a/mpdreload.pl +++ b/mpdreload.pl @@ -26,21 +26,13 @@ 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. sub get_tracks_in_play_queue { my %matches; my $entry; - mpd_exec("playlistinfo"); + MPD::exec("playlistinfo"); while (<$sock>) { last if /^OK/; die($_) if /^ACK/; @@ -72,7 +64,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/; @@ -140,7 +132,7 @@ $sock = MPD::connect(); 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}; @@ -152,9 +144,9 @@ for (my $i = 0; $i < @$target; $i++) { delete $current->{$f} unless (keys %$ids > 0); if (defined $id) { - mpd_exec("moveid", $id, $i); + MPD::exec("moveid", $id, $i); } else { - mpd_exec("addid", $f, $i); + MPD::exec("addid", $f, $i); } } @@ -162,11 +154,11 @@ for (my $i = 0; $i < @$target; $i++) { foreach (keys %$current) { my $ids = $current->{$_}->{Id}; foreach (keys %$ids) { - mpd_exec("deleteid", $_); + MPD::exec("deleteid", $_); } } -mpd_exec("command_list_end"); +MPD::exec("command_list_end"); while (<$sock>) { last if /^OK$/; die($_) if /^ACK/; -- 2.43.0