Skip to content

Instantly share code, notes, and snippets.

@webuti
Created April 10, 2018 12:54
Show Gist options
  • Save webuti/63d931a983a13e64fc687b2c3f624f7c to your computer and use it in GitHub Desktop.
Save webuti/63d931a983a13e64fc687b2c3f624f7c to your computer and use it in GitHub Desktop.
#!/bin/bash
LC_ALL=C
## Spotify Launch ##
spotify &>/dev/null &
spotiPid=$!
wait=1
printf "Spotify pid is $spotiPid\n"
while [ $wait = 1 ]; do
printf "Waiting for spotify...\n"
sleep 5
icon=$(xprop -name 'Spotify' WM_ICON_NAME &>/dev/null)
if [ $? == 0 ]; then
wait=0
fi
done
##/Spotify Launch ##
lastIcon=""
ad=0
xprop -spy -name 'Spotify' WM_ICON_NAME |
while read -r line ; do
if [[ ! -d "/proc/$spotiPid" ]]; then
printf "Spotify exited. Quitting.\n"
break
fi
waitPulse=1
## Waiting fo pulseaudio
while [ $waitPulse -eq 1 ]; do
num=$(pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | awk '/Spotify/ {print a} {a = $0}' | cut -c 13-)
if [[ ! -z "$num" ]]; then
waitPulse=0
else
sleep 0.1
fi
done
icon_name=$(echo "$line" | awk -F\" '{ print $2; }' );
if [[ "$lastIcon" = "$icon_name" ]]; then
continue;
fi
META=$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.GetMetadata)
song_rating=`echo "$META" | grep xesam:autoRating -A 1 | grep variant | awk '{ printf $3; }'`
song_name=$(echo -n "$META" | grep xesam:title -A 1 | grep variant | cut -d \" -f 2)
song_artist=$(echo -n "$META" | grep xesam:artist -A 2 | grep string | grep -v xesam | cut -d \" -f 2)
printf "Rating: $song_rating, $icon_name\n"
mute=1
if [[ "$song_name" && "$icon_name" =~ "$song_name" ]]; then
mute=0
fi
if [[ $mute -eq 0 ]]; then
if [ $ad -eq 1 ]; then #Post-ad sleep
sleep 0.5
fi
pactl set-sink-input-mute $num no
ad=0
else
pactl set-sink-input-mute $num yes
ad=1
fi
done
test -z "`jobs -p`" || kill `jobs -p` 2>/dev/null
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment