dotfiles/.config/waybar/spotify.sh

31 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
playstatus=$(gdbus call --session --dest org.mpris.MediaPlayer2.spotify --object-path /org/mpris/MediaPlayer2 --method org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player PlaybackStatus | cut -d "'" -f2)
#NOTE: This makes the text imediately dissapear when the track is not playing. This is intended behaviour however it could be confusing or annoying. Might add option??
if [ "$playstatus" != "Playing" ]; then
exit
fi
media=$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata)
echo -n " "
#FIXME: Note that this makes entries containing the character " cut off early
echo -n "$(echo "$media" | sed -n '/title/{n;p}' | cut -d '"' -f 2)"
echo -n " by $(echo "$media" | sed -n '/artist/{n;n;p}' | cut -d '"' -f 2)"
ttot=$(echo "$media" | sed -n '/length/{n;p}' | awk '{print $3}')
ttot=$((ttot / 1000000)) # ns to s
ttotm=$((ttot / 60)) # s to m
ttots=$((ttot % 60)) # remainder s
tot=$(printf "%02d:%02d" "$ttotm" "$ttots")
tcurr=$(gdbus call --session --dest org.mpris.MediaPlayer2.spotify --object-path /org/mpris/MediaPlayer2 --method org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player Position | awk -F '[<>, ]+' '{print $3}')
tcurr=$((tcurr / 1000000))
tcurrm=$((tcurr / 60))
tcurrs=$((tcurr % 60))
curr=$(printf "%02d:%02d" "$tcurrm" "$tcurrs")
echo " ($curr/$tot)"