From 1eeb7af2bb7f49fa46bb9a880eb787da9454cafa Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 6 Apr 2020 15:41:35 -0400 Subject: [PATCH] Rewrite spotify+spotify-tui layer Very roundabout, I know, but there is so much wrong with nixpkgs.spotifyd that its less trouble to do it myself. --- config/sxhkd/sxhkdrc | 6 +- modules/media/spotify.nix | 120 ++++++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 47 deletions(-) diff --git a/config/sxhkd/sxhkdrc b/config/sxhkd/sxhkdrc index 9b6d7a7f..9c4bf036 100644 --- a/config/sxhkd/sxhkdrc +++ b/config/sxhkd/sxhkdrc @@ -113,11 +113,11 @@ XF86AudioRaiseVolume amixer -q set Master 10%+ unmute XF86Audio{Play,Pause} - dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause + spt-send toggle XF86AudioNext - dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next + spt-send next XF86AudioPrev - dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous + spt-send prev # X86Search diff --git a/modules/media/spotify.nix b/modules/media/spotify.nix index 73afcd1e..74b76aac 100644 --- a/modules/media/spotify.nix +++ b/modules/media/spotify.nix @@ -12,56 +12,88 @@ with lib; }; config = mkIf config.modules.media.spotify.enable { - # services.spotifyd doesn't work so we'll have to roll our own spotifyd + - # spotify-tui solution. The dbus interface doesn't work, though, so we have to - # wait for spotify-tui to implement CLI command for controlling spotify - # playback. See https://github.com/Rigellute/spotify-tui/issues/147 or for - # spotifyd to get fixed. my.packages = with pkgs; [ - # unstable.spotifyd - # (writeScriptBin "spt" '' - # #!${stdenv.shell} - # if ! systemctl --user is-active spotifyd >/dev/null; then - # systemctl --user start spotifyd - # fi - # echo $TMUX >$XDG_DATA_HOME/spt.tmux - # exec ${unstable.spotify-tui}/bin/spt - # '') + # services.spotifyd doesn't work so we'll have to roll our own spotifyd + + # spotify-tui solution. The dbus interface doesn't work, though, so we + # have to wait for spotify-tui to implement CLI command for controlling + # spotify playback. See + # https://github.com/Rigellute/spotify-tui/issues/147 or for spotifyd to + # get fixed. + unstable.spotifyd + (writeScriptBin "spt" '' + #!${stdenv.shell} + if ! systemctl --user is-active spotifyd >/dev/null; then + systemctl --user start spotifyd + fi + echo $TMUX >/tmp/spt.tmux + exec ${unstable.spotify-tui}/bin/spt + '') # spotify-tui is fine for selecting and playing music, but incomplete. We - # occasionally still need the official client for more sophisticated search - # and the "made for you" playlists. + # still occasionally need the official client for more sophisticated + # search and the "made for you" playlists. spotify - # Since dbus doesn't work with spotifyd/spotify-tui, and I always use tmux - # for my terminal, may as well exploit tmux to control spotify. - # (writeScriptBin "spt-send" '' - # #!${stdenv.shell} - # dbus_cmd='dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player' - # tmux_cmd="tmux send-keys -t $(cat $XDG_DATA_HOME/spt.tmux | cut -d, -f3)" - # case "$1" in - # toggle) $dbus_cmd.PlayPause 2>/dev/null || $tmux_cmd " " ;; - # next) $dbus_cmd.Next 2>/dev/null || $tmux_cmd n ;; - # prev) $dbus_cmd.Previous 2>/dev/null || $tmux_cmd p ;; - # esac - # '') + # Since dbus doesn't work with Nix's spotifyd/spotify-tui, and I always + # use tmux for my terminal, may as well exploit tmux to control spotify. + playerctl + (writeScriptBin "spt-send" '' + #!${stdenv.shell} + dbus_cmd='playerctl -p spotify' + tmux_cmd="tmux send-keys -t $(cat /tmp/spt.tmux | cut -d, -f3)" + case "$1" in + toggle) $dbus_cmd play-pause 2>/dev/null || $tmux_cmd " " ;; + next) $dbus_cmd next 2>/dev/null || $tmux_cmd n ;; + prev) $dbus_cmd previous 2>/dev/null || $tmux_cmd p ;; + esac + '') + + (writeScriptBin "spt-send-notify" '' + #!${stdenv.shell} + client_id=$(pass www/spotify.com | grep -E '^client_id:' | awk '{print $2}') + client_secretd=$(pass www/spotify.com | grep -E '^client_secret:' | awk '{print $2}') + jq="${pkgs.jq}/bin/jq" + + token=$(curl -s -X 'POST' -u $client_id:$client_secretd -d grant_type=client_credentials https://accounts.spotify.com/api/token | $jq '.access_token' | cut -d\" -f2) + result=$? + if [[ $result != 0 ]]; then + notify-send "spt-send failure" "Could not acquire token" + exit 1 + fi + case "$PLAYER_EVENT" in + start) + curl -s -X 'GET' https://api.spotify.com/v1/tracks/$TRACK_ID -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization:\"Bearer $token\"" | $jq '.name, .artists[].name, .album.name, .album.release_date, .track_number, .album.total_tracks' | xargs printf "\"Playing '%s' from '%s' (album: '%s' in %s (%s/%s))\"" | xargs notify-send --urgency=low --expire-time=3000 --icon=/usr/share/icons/gnome/32x32/actions/player_play.png --app-name=spotifyd spotifyd + ;; + stop) + curl -s -X 'GET' https://api.spotify.com/v1/tracks/$TRACK_ID -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization:\"Bearer $token\"" | $jq '.name, .artists[].name, .album.name, .album.release_date, .track_number, .album.total_tracks' | xargs printf "Stoping music (Last song: '%s' from '%s' (album: '%s' in %s (%s/%s)))\"" | xargs notify-send --urgency=low --expire-time=3000 --icon=/usr/share/icons/gnome/32x32/actions/player_stop.png --app-name=spotifyd spotifyd + ;; + change) + curl -s -X 'GET' https://api.spotify.com/v1/tracks/$TRACK_ID -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization:\"Bearer $token\"" | $jq '.name, .artists[].name, .album.name, .album.release_date, .track_number, .album.total_tracks' | xargs printf "\"Music changed to '%s' from '%s' (album: '%s' in %s (%s/%s))\"" | xargs notify-send --urgency=low --expire-time=3000 --icon=/usr/share/icons/gnome/32x32/actions/player_fwd.png --app-name=spotifyd spotifyd + ;; + *) 2>&1 echo "Unkown event." + exit 2 + ;; + esac + '') ]; - # systemd.user.services.spotifyd.serviceConfig = - # let spotifydConf = pkgs.writeText "spotifyd.conf" '' - # [global] - # username = g2qwjcs6334oacg1zg9wrihnn - # password_cmd = ${pkgs.pass}/bin/pass www/spotify.com | head -n1 - # backend = pulseaudio - # ''; - # in { - # ExecStart = '' - # ${pkgs.unstable.spotifyd}/bin/spotifyd --no-daemon \ - # --cache-path /tmp/spotifyd \ - # --config-path ${spotifydConf} - # ''; - # Restart = "always"; - # RestartSec = 6; - # }; + systemd.user.services.spotifyd.serviceConfig = + let spotifydConf = pkgs.writeText "spotifyd.conf" '' + [global] + username = g2qwjcs6334oacg1zg9wrihnn + password_cmd = ${pkgs.pass}/bin/pass www/spotify.com | head -n1 + backend = pulseaudio + on_song_change_hook = spt-send-notify + cache_path = /home/${config.my.username}/.cache/spotifyd + ''; + in { + ExecStart = '' + ${pkgs.unstable.spotifyd}/bin/spotifyd --no-daemon \ + --cache-path /tmp/spotifyd \ + --config-path ${spotifydConf} + ''; + Restart = "always"; + RestartSec = 6; + }; }; }