From 9b02bdda42565b687ecefe63b106118088f1c505 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 28 Jul 2021 20:14:00 -0400 Subject: [PATCH] mpdmenu: Add MusicBrainz links for the current track. Having a feature to directly open relevant track and artist pages on the web seems quite handy. --- mpdmenu.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/mpdmenu.pl b/mpdmenu.pl index 2b5fcad..15a49b2 100755 --- a/mpdmenu.pl +++ b/mpdmenu.pl @@ -282,6 +282,53 @@ sub top_track_album { @submenu); } +# Generate the "MusicBrainz:" entry in the top menu. +sub top_track_musicbrainz { + my ($entry) = @_; + my ($track_mbid, $recording_mbid, $release_mbid); + my @artist_mbids; + my $label = "MB:"; + my %idmap; + + ($track_mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID"); + ($recording_mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID"); + ($release_mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID"); + @artist_mbids = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID"); + return unless $track_mbid // $recording_mbid + // $release_mbid // @artist_mbids; + + foreach (get_track_metadata($entry, "Comment")) { + $idmap{$1} = $2 if /^([^=]*)=(.*) \(idmap\)$/; + } + + fvwm_cmd("AddToMenu", $menu, "", "Nop"); + if ($track_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow track", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/track/$track_mbid"); + $label = ""; + } elsif ($recording_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow recording", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/recording/$recording_mbid"); + $label = ""; + } elsif ($release_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/release/$release_mbid"); + $label = ""; + } + + foreach my $mbid (@artist_mbids) { + my $name = " $idmap{$mbid}" if $idmap{$mbid}; + + fvwm_cmd("AddToMenu", $menu, "$label\tShow artist$name", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/artist/$mbid"); + $label = ""; + } +} + # Given a work MBID, return a hash reference containing all tracks # linked to that work. The hash keys are filenames. sub get_tracks_by_work_mbid { @@ -603,6 +650,7 @@ if ($mode eq "top") { top_track_title(\%current); top_track_artist(\%current); top_track_album(\%current); + top_track_musicbrainz(\%current); } else { fvwm_cmd("AddToMenu", $menu, "[current track unavailable]"); } -- 2.43.0