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.
This commit is contained in:
Henrik Lissner 2020-04-06 15:41:35 -04:00
parent d5fdda309d
commit 1eeb7af2bb
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 79 additions and 47 deletions

View file

@ -113,11 +113,11 @@ XF86AudioRaiseVolume
amixer -q set Master 10%+ unmute amixer -q set Master 10%+ unmute
XF86Audio{Play,Pause} XF86Audio{Play,Pause}
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause spt-send toggle
XF86AudioNext XF86AudioNext
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next spt-send next
XF86AudioPrev XF86AudioPrev
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous spt-send prev
# X86Search # X86Search

View file

@ -12,56 +12,88 @@ with lib;
}; };
config = mkIf config.modules.media.spotify.enable { 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; [ my.packages = with pkgs; [
# unstable.spotifyd # services.spotifyd doesn't work so we'll have to roll our own spotifyd +
# (writeScriptBin "spt" '' # spotify-tui solution. The dbus interface doesn't work, though, so we
# #!${stdenv.shell} # have to wait for spotify-tui to implement CLI command for controlling
# if ! systemctl --user is-active spotifyd >/dev/null; then # spotify playback. See
# systemctl --user start spotifyd # https://github.com/Rigellute/spotify-tui/issues/147 or for spotifyd to
# fi # get fixed.
# echo $TMUX >$XDG_DATA_HOME/spt.tmux unstable.spotifyd
# exec ${unstable.spotify-tui}/bin/spt (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 # spotify-tui is fine for selecting and playing music, but incomplete. We
# occasionally still need the official client for more sophisticated search # still occasionally need the official client for more sophisticated
# and the "made for you" playlists. # search and the "made for you" playlists.
spotify spotify
# Since dbus doesn't work with spotifyd/spotify-tui, and I always use tmux # Since dbus doesn't work with Nix's spotifyd/spotify-tui, and I always
# for my terminal, may as well exploit tmux to control spotify. # use tmux for my terminal, may as well exploit tmux to control spotify.
# (writeScriptBin "spt-send" '' playerctl
# #!${stdenv.shell} (writeScriptBin "spt-send" ''
# dbus_cmd='dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player' #!${stdenv.shell}
# tmux_cmd="tmux send-keys -t $(cat $XDG_DATA_HOME/spt.tmux | cut -d, -f3)" dbus_cmd='playerctl -p spotify'
# case "$1" in tmux_cmd="tmux send-keys -t $(cat /tmp/spt.tmux | cut -d, -f3)"
# toggle) $dbus_cmd.PlayPause 2>/dev/null || $tmux_cmd " " ;; case "$1" in
# next) $dbus_cmd.Next 2>/dev/null || $tmux_cmd n ;; toggle) $dbus_cmd play-pause 2>/dev/null || $tmux_cmd " " ;;
# prev) $dbus_cmd.Previous 2>/dev/null || $tmux_cmd p ;; next) $dbus_cmd next 2>/dev/null || $tmux_cmd n ;;
# esac 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 = systemd.user.services.spotifyd.serviceConfig =
# let spotifydConf = pkgs.writeText "spotifyd.conf" '' let spotifydConf = pkgs.writeText "spotifyd.conf" ''
# [global] [global]
# username = g2qwjcs6334oacg1zg9wrihnn username = g2qwjcs6334oacg1zg9wrihnn
# password_cmd = ${pkgs.pass}/bin/pass www/spotify.com | head -n1 password_cmd = ${pkgs.pass}/bin/pass www/spotify.com | head -n1
# backend = pulseaudio backend = pulseaudio
# ''; on_song_change_hook = spt-send-notify
# in { cache_path = /home/${config.my.username}/.cache/spotifyd
# ExecStart = '' '';
# ${pkgs.unstable.spotifyd}/bin/spotifyd --no-daemon \ in {
# --cache-path /tmp/spotifyd \ ExecStart = ''
# --config-path ${spotifydConf} ${pkgs.unstable.spotifyd}/bin/spotifyd --no-daemon \
# ''; --cache-path /tmp/spotifyd \
# Restart = "always"; --config-path ${spotifydConf}
# RestartSec = 6; '';
# }; Restart = "always";
RestartSec = 6;
};
}; };
} }