commit b9ecabef988643fa86dc2ee52d621c9d21153286
parent b04c1dacc8b2f13c363974d0d40ba0d0929520ae
Author: Alex Balgavy <alex@balgavy.eu>
Date: Sun, 9 Oct 2022 14:22:23 +0200
linkhandler: set title when adding to mpd
You can communicate with MPD using a protocol [1]. A commenter outlined
how to do that in fish [2], this commit adds a POSIX-compliant
implementation to handling of youtube links. In short, when you queue a
youtube link in MPD, it'll now have a title and artist.
[1] https://mpd.readthedocs.io/en/latest/protocol.html
[2] https://github.com/MusicPlayerDaemon/MPD/issues/332#issuecomment-1121600677
Diffstat:
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/scripts/linkhandler b/scripts/linkhandler
@@ -5,6 +5,8 @@ use warnings;
use 5.006; # checked with `perlver`
use constant EXIT_USER_CANCELLED => 130;
+use constant MPD_HOST => 'localhost';
+use constant MPD_PORT => 6600;
if ( @ARGV != 1 ) {
die 'Link necessary.';
@@ -166,8 +168,15 @@ sub play_audio_mpd {
system(qq(
title=\$(youtube-dl --ignore-config --get-title '$song' 2>/dev/null);
printf "%b\n" "\$title\t$song" >> "$HOME/.cache/mpd/linkarchive";
- mpc add "\$(youtube-dl -x -g "$song")";
- done));
+
+ send_mpd_command() {
+ (printf '%s\n' "\$1"; sleep 1) | telnet ${\MPD_HOST} ${\MPD_PORT} 2>/dev/null
+ }
+
+ url="\$(youtube-dl -x -g "$song")"
+ res="\$(send_mpd_command "addid \$url")"
+ song_id="\$(printf '%s' "\$res" | awk -F': ' '/^Id: / { print \$2 }')"
+ send_mpd_command "\$(printf 'addtagid %s Artist "%s"\naddtagid %s Title "%s"' "\$song_id" "Youtube" "\$song_id" "\$title")" >/dev/null));
}
close($playlist) or die "Could not close handle.";
@@ -176,8 +185,16 @@ sub play_audio_mpd {
else {
system(qq([ -d "$HOME/.cache/mpd" ] || mkdir -p "$HOME/.cache/mpd";
title=\$(youtube-dl --ignore-config --get-title "$link" 2>/dev/null);
- printf "%b\n" "\$title\t$link" >> "$HOME/.cache/mpd/linkarchive";));
- system(qq(mpc add "\$(youtube-dl -x -g '$link')"));
+ printf "%b\n" "\$title\t$link" >> "$HOME/.cache/mpd/linkarchive";
+
+ send_mpd_command() {
+ (printf '%s\n' "\$1"; sleep 1) | telnet ${\MPD_HOST} ${\MPD_PORT} 2>/dev/null
+ }
+
+ url="\$(youtube-dl -x -g "$link")"
+ res="\$(send_mpd_command "addid \$url")"
+ song_id="\$(printf '%s' "\$res" | awk -F': ' '/^Id: / { print \$2 }')"
+ send_mpd_command "\$(printf 'addtagid %s Artist "%s"\naddtagid %s Title "%s"' "\$song_id" "Youtube" "\$song_id" "\$title")" >/dev/null));
};
}
}